How to change Conv2d.weights.data shape?

code:
class cnn(nn.Module):
def init(self):
super(cnn, self).init()
self.conv1 = nn.Conv2d(3, 5, 3, 1, 1)
def forward(self, x):
x = self.conv1(x)
return x

model = cnn()

model.conv1.weight.data = torch.cat([model.conv1.weight.data, torch.rand([1, 3, 3, 3])], dim=0)

model.conv1.bias.data = torch.cat([model.conv1.bias.data, torch.ones(1, dtype=torch.float32)], dim=0)
model.conv1.out_channels = 6

I can get the correct output shape by y=model(x),
but I meet some error in backward:RuntimeError: Function ThnnConv2DBackward returned an invalid gradient at index 1 - got [6, 3, 3, 3] but expected shape compatible with [5, 3, 3, 3]
I also find some relative Q&A, it’s seem like autograd can’t get the modify in “weights.data” and “bias.data”, but I want to keep the original information, only add convolution core.What should I do?

Try this:

cnn=nn.Conv2d(3,5,3,1,1)
print(cnn.weight.data.size())
with torch.no_grad():
    cnn.weight.data =torch.rand(6,3,3,3)
    cnn.bias.data=torch.rand(6)

x=cnn(torch.rand(32,3,32,32))
criterion=nn.MSELoss()
opt=torch.optim.Adam(cnn.parameters())
opt.zero_grad()

loss=criterion(x, torch.rand_like(x))

loss.backward()
opt.step()

You can also set the output size, but it doesn’t check for that.

Note that you need to reinitialize the optimizer.