【发布时间】:2021-04-14 15:06:06
【问题描述】:
我正在尝试使用 Batch Normalization 实现 Conv1d 模型,但出现错误:
RuntimeError Traceback (most recent call last)
<ipython-input-117-ef6e122ea50c> in <module>()
----> 1 test()
2 for epoch in range(1, n_epochs + 1):
3 train(epoch)
4 test()
7 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 and mat2 shapes cannot be multiplied (32x140 and 100x10)
我使用 32 的批量大小,数据的特征数为 40。我一直在尝试计算 32 x 140 的来源,但我无法做到。这是我尝试使用的 CNN 架构:
class MyModel(nn.Module):
def __init__(self):
super(MyModel, self).__init__()
#self.flatten=nn.Flatten()
self.net_stack=nn.Sequential(
nn.Conv1d(in_channels=1, out_channels=25, kernel_size=5, stride=2), #applying batch norm
nn.ReLU(),
nn.BatchNorm1d(25, affine=True),
nn.Conv1d(in_channels=25, out_channels=20, kernel_size=5, stride=2), #applying batch norm
nn.ReLU(),
nn.BatchNorm1d(20, affine=True),
nn.Flatten()
nn.Linear(20*5, 10),
nn.Softmax(dim=1))
def forward(self,x):
# result=self.net_stack(x[None])
result=self.net_stack(x[:, None, :])
return result
【问题讨论】:
标签: python pytorch conv-neural-network batch-normalization