如何解决显示 "shapes cannot be multiplied" 的错误
How to resolve the error that says "shapes cannot be multiplied"
我尝试了 google colab 中文章中提到的代码。
https://theaisummer.com/spiking-neural-networks/
我收到如下所示的错误...
Test loss: 8.86368179321289
Test loss: 5.338221073150635
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-9-646cb112ccb7> in <module>()
15 # forward pass
16 net.train()
---> 17 spk_rec, mem_rec = net(data.view(batch_size, -1))
18
19 # initialize the loss & sum over time
4 frames
/usr/local/lib/python3.7/dist-packages/torch/nn/functional.py in linear(input, weight, bias)
1846 if has_torch_function_variadic(input, weight, bias):
1847 return handle_torch_function(linear, (input, weight, bias), input, weight, bias=bias)
-> 1848 return torch._C._nn.linear(input, weight, bias)
1849
1850
RuntimeError: mat1 and mat2 shapes cannot be multiplied (128x588 and 784x1000)
我不知道如何解决它。
我刚刚 运行 他们的 Colab 笔记本和 运行 进入了同样的错误。发生这种情况是因为最终迭代没有 128 个数据样本,因为总数据集大小(训练集和测试集为 60000 和 10000)不能被 128 整除。所以还有一些剩余,并将其重塑为 128 x 。 ..导致输入数据与输入层神经元数量的维度不匹配。
有两个可能的修复方法。
- 只需放下最后一批:
train_loader = DataLoader(mnist_train, batch_size=batch_size, shuffle=True, drop_last=True)
test_loader = DataLoader(mnist_test, batch_size=batch_size, shuffle=True, drop_last=True)
- 不要放弃最后一批。但是以保留原始 batch_size 的方式展平张量,而不是将其强制为 128:
spk_rec, mem_rec = net(data.flatten(1))
我尝试了 google colab 中文章中提到的代码。
https://theaisummer.com/spiking-neural-networks/
我收到如下所示的错误...
Test loss: 8.86368179321289
Test loss: 5.338221073150635
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-9-646cb112ccb7> in <module>()
15 # forward pass
16 net.train()
---> 17 spk_rec, mem_rec = net(data.view(batch_size, -1))
18
19 # initialize the loss & sum over time
4 frames
/usr/local/lib/python3.7/dist-packages/torch/nn/functional.py in linear(input, weight, bias)
1846 if has_torch_function_variadic(input, weight, bias):
1847 return handle_torch_function(linear, (input, weight, bias), input, weight, bias=bias)
-> 1848 return torch._C._nn.linear(input, weight, bias)
1849
1850
RuntimeError: mat1 and mat2 shapes cannot be multiplied (128x588 and 784x1000)
我不知道如何解决它。
我刚刚 运行 他们的 Colab 笔记本和 运行 进入了同样的错误。发生这种情况是因为最终迭代没有 128 个数据样本,因为总数据集大小(训练集和测试集为 60000 和 10000)不能被 128 整除。所以还有一些剩余,并将其重塑为 128 x 。 ..导致输入数据与输入层神经元数量的维度不匹配。
有两个可能的修复方法。
- 只需放下最后一批:
train_loader = DataLoader(mnist_train, batch_size=batch_size, shuffle=True, drop_last=True)
test_loader = DataLoader(mnist_test, batch_size=batch_size, shuffle=True, drop_last=True)
- 不要放弃最后一批。但是以保留原始 batch_size 的方式展平张量,而不是将其强制为 128:
spk_rec, mem_rec = net(data.flatten(1))