TypeError: cannot assign 'torch.FloatTensor' as parameter 'layer_weights' (torch.nn.Parameter or None expected)

#43835 Issue description
Traceback (most recent call last):
File “run_techqa_layer.py”, line 605, in
main()
File “run_techqa_layer.py”, line 599, in main
model = train(args, train_dataset, model, optimizer, tokenizer, model_evaluator)
File “run_techqa_layer.py”, line 211, in train
outputs = model(**inputs)
File “C:\Users\Rudraksh\Anaconda3\envs\bert_pre\lib\site-packages\torch\nn\modules\module.py”, line 547, in call
result = self.forward(*input, **kwargs)
File “D:\techqa-layer\modeling.py”, line 228, in forward
self.layer_weights = F.softmax(self.layer_weights,dim=0)
File “C:\Users\Rudraksh\Anaconda3\envs\bert_pre\lib\site-packages\torch\nn\modules\module.py”, line 610, in setattr
.format(torch.typename(value), name))
TypeError: cannot assign ‘torch.FloatTensor’ as parameter ‘layer_weights’ (torch.nn.Parameter or None expected)

System info

PyTorch version: 1.2.0
Is debug build: False
CUDA used to build PyTorch: 10.0

OS: Microsoft Windows 10 Education
GCC version: (i686-posix-dwarf-rev0, Built by MinGW-W64 project) 8.1.0
Clang version: Could not collect
CMake version: Could not collect

Python version: 3.6 (64-bit runtime)
Is CUDA available: False
CUDA runtime version: No CUDA
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA

    def __init__(self, config):
	super(LayeredBertforQA, self).__init__(config)
	self.num_labels = config.num_labels
	self.bert = BertModel(config)
	self.n_weights = config.num_hidden_layers + 1
	self.layer_weights = nn.Parameter(torch.zeros(self.n_weights,dtype=torch.float,device="cpu"))
	self.qa_outputs = nn.Linear(config.hidden_size, config.num_labels)
	self.options = None
	self.init_weights()	

    def forward(self, input_ids=None, attention_mask=None, token_type_ids=None,start_positions=None,end_positions=None):		
	outputs = self.bert(
		input_ids,
		attention_mask=attention_mask,
		token_type_ids=token_type_ids,
	)

	hidden_states = outputs[2]
	stacked_hidden_states = torch.stack(hidden_states)

	self.layer_weights = F.softmax(self.layer_weights,dim=0)
1 Like

self.layer_weights is not a tensor but a Parameter that is registered as an attribute of your module. That’s why when you assign it a tensor it throws an error. Instead, try
self.layer_weights = nn.Parameter(F.softmax(self.layer_weights,dim=0))

2 Likes

If I make a self.layer_weights a Tensor, and then later on make it a Paramater, will it work? Just for information

Yep, should work. Parameter is literally a Tensor with the convenience of automatically registering that tensor as a trainable parameter. If you leave it as a tensor, it won’t show up in your module.parameters().