【发布时间】:2021-08-02 04:48:58
【问题描述】:
我是神经网络领域的新手,遇到了一个问题。
我正在尝试为隐藏的全连接层创建一个丢失概率为 0.1 的 NN。
当我像下面这样编码时:
class ConvNet(torch.nn.Module):
def __init__(self):
super().__init__()
self.layers = torch.nn.Sequential(
#layer1
torch.nn.Conv2d(in_channels=3, out_channels=16, kernel_size=3, stride=1),
torch.nn.MaxPool2d(kernel_size=2, stride=3),
torch.nn.BatchNorm2d(num_features=16),
torch.nn.ReLU(),
#layer2
torch.nn.Conv2d(in_channels=16, out_channels=32, kernel_size=3, stride=1),
torch.nn.MaxPool2d(kernel_size=2, stride=3),
torch.nn.BatchNorm2d(num_features=32),
torch.nn.ReLU(),
#layer3
torch.nn.Conv2d(in_channels=32, out_channels=32, kernel_size=3, stride=1),
torch.nn.MaxPool2d(kernel_size=2, stride=3),
torch.nn.BatchNorm2d(num_features=32),
torch.nn.Flatten(),
torch.nn.Linear(32,16),
torch.nn.Dropout2d(p=0.1),
torch.nn.Linear(16, 2)
)
def forward(self, x):
return self.layers(x)
test_convnet = ConvNet().to('cuda')
test_input = torch.randn(16, 3, 100, 100, device='cuda')
test_output = test_convnet(test_input)
print(test_output.shape)
然后我得到了错误:
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-27-b5ce1c300266> in <module>()
48 test_convnet = ConvNet().to('cuda')
49 test_input = torch.randn(16, 3, 100, 100, device='cuda')
---> 50 test_output = test_convnet(test_input)
51 print(test_output.shape)
6 frames
/usr/local/lib/python3.7/dist-packages/torch/nn/functional.py in linear(input, weight, bias)
1751 if has_torch_function_variadic(input, weight):
1752 return handle_torch_function(linear, (input, weight), input, weight, bias=bias)
-> 1753 return torch._C._nn.linear(input, weight, bias)
1754
1755
RuntimeError: mat1 dim 1 必须匹配 mat2 dim 0
提前感谢您的所有帮助
【问题讨论】: