Expected stride to be a single integer value or a list of 1 values to match the convolution dimensions,

This error has appeared on the forum many times, but it doesn’t seem to be the same as my question.

I’m trying to learn how to use the convolution transpose API,and this is my code:

I use the transpose convolution and its kernel size is (1,1),stride is (2,2). And I then customized the weights so that I could see the end result

Input=np.asarray([[1,2],[3,4]])
Input=Input.reshape((1,1,2,2))
Input=torch.from_numpy(Input).float()
print(Input.shape)

model=torch.nn.Sequential()
transpose=torch.nn.ConvTranspose2d(1,1,kernel_size=1,stride=2)
weight=np.asarray([[[1]]])
bias=np.asarray([1])
weight=torch.Tensor(weight)
bias=torch.Tensor(bias)
transpose.weight=torch.nn.Parameter(weight)
transpose.bias=torch.nn.Parameter(bias)




model.add_module('transpose',transpose)
yhat=model(Input)
yhat=yhat.reshape((4,4))
print(yhat)

And the error is:

Hi,

Why do you try to change the weights?
You can check just after creating the layer what are the correct sizes:

transpose=torch.nn.ConvTranspose2d(1,1,kernel_size=1,stride=2)
print(transpose.weight.size())
print(transpose.bias.size())

The weight and bias Tensors you create are not of the right size.

Thanks, You help me solve this question!