0

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
Vitaliy
  • 31
  • 1
  • 1
  • 4
  • 1
    look at [`torch.nn.init`](https://pytorch.org/docs/0.4.1/nn.html#torch-nn-init) for examples how to initialize parameters – Shai Mar 05 '19 at 06:13
  • 1
    Possible duplicate of [How to initialize weights in PyTorch?](https://stackoverflow.com/questions/49433936/how-to-initialize-weights-in-pytorch) – kHarshit Mar 05 '19 at 07:54
  • So far this worked: m.weight = torch.nn.Parameter(paramsNP) – Vitaliy Mar 07 '19 at 00:11

0 Answers0