You could use copy_
instead of fill_
or assign an nn.Parameter
.
Also, the usage of the .data
attribute is discouraged. Wrap the assignment in a torch.no_grad()
block instead:
numpy_data= np.random.randn(6, 1, 3, 3)
conv = nn.Conv2d(1, 6, 3, 1, 1, bias=False)
with torch.no_grad():
conv.weight = nn.Parameter(torch.from_numpy(numpy_data).float())
# or
conv.weight.copy_(torch.from_numpy(numpy_data).float())