How do I pass numpy array to conv2d weight for initialization?

How do I pass numpy array to conv2d weight for initialization?

I tried using fill_ but apprarently it only support for 0-dimension value.
My numpy_data is 4-dimension array.

Here’s what I tried:

myModel = Net()
layers = [x.data for x in myModel.parameters()]
layers[idx].data.fill_( numpy_data )

1 Like

Hi @han324solo,

You can simply do this:

layers[idx].data = torch.FloatTensor(numpy_data)
1 Like

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())
4 Likes

Thanks. This is exactly what I needed.

What happens if we don’t wrap with torch.no_grad()? Thanks!

Then the intermediate activations, which are needed to calculate the gradients during the backward pass will be stored and will use memory, which is not needed during inference/testing.