I am looking into torch.nn.Conv1D class that I would like to initialize with custom weights.
I would like these weights to be "differentiable" as they will be derived based on some other parameters.
Here is an example of what I would like to do:
# Initialize random convolution of size 3:
m = nn.Conv1d(1, 1, 3, stride=1,bias=False)
# Define custom weights
w1 = 1.
w2 =-2.
w3 = 1.
paramsNP = np.array([w1, w2, w3]).reshape((1,1,3))
# Assign weights to m.weight (THIS PART FAILS)
m.weight = torch.tensor(paramsNP, requires_grad=True)
This last assignment fails with
TypeError: cannot assign 'torch.DoubleTensor' as parameter 'weight' (torch.nn.Parameter or None expected)
This seams to be due to m.weight being of the type torch.nn.parameter.Parameter What would be a good ways of performing the assignment/initialization of the Conv1D object m?
Instead of that last line I can do this assignment:
m.weight[0,0,0] = w1
m.weight[0,0,1] = w2
m.weight[0,0,2] = w3