带权重的pytorch conv2d

pytorch conv2d with weights

我有这两个变量:

result- tensor 1 X 251 X 20
kernel - tensor 1 X 10 X 10

当我 运行 命令时:

from torch.nn import functional as F
result = F.conv2d(result, kernel)

我收到错误:

RuntimeError: expected stride to be a single integer value or a list of 1 values to match the convolution dimensions, but got stride=[1, 1]

我没有任何进步,我做错了什么?

import torch
import torch.nn.functional as F
image = torch.rand(16, 3, 32, 32)
filter = torch.rand(1, 3, 5, 5)
out_feat_F = F.conv2d(image, filter,stride=1, padding=0)
print(out_feat_F.shape)

输出:

torch.Size([16, 1, 28, 28])

相当于:

import torch
import torch.nn
image = torch.rand(16, 3, 32, 32)
conv_filter = torch.nn.Conv2d(in_channels=3, out_channels=1, kernel_size=5, stride=1, padding=0)
output_feature = conv_filter(image)
print(output_feature.shape)

输出:

torch.Size([16, 1, 28, 28])

Padding默认为0,stride默认为1。 第一个示例中的过滤器最后两个维度对应于 第二个例子中的内核大小。

kernel_size=5 等同于 kernel_size=(5,5).