Copy value from tensor to nn.parameter()

Hi, How to copy value from tensor to nn.Parameter()
since v_parameter.copy_(v_tensor) requires same type.

It requires same type in the sense of float/double/long right? You can use type_as() on v_tensor before giving it to copy. You should enclose this into a with torch.no_grad(): block to avoid errors because you modified inplace a leaf tensor.

2 Likes