neural-network - pytorch 中 MNIST 数据集的 DNN 大小不匹配
问题描述
我必须找到一种方法来创建神经网络模型并在 MNIST 数据集上对其进行训练。我需要有 5 层,每层有 100 个神经元。但是,当我尝试设置它时,我收到一个错误,即大小不匹配。你能帮忙吗?我希望我可以在下面的模型上进行训练:
class Mnist_DNN(nn.Module):
def __init__(self):
super().__init__()
self.layer1 = nn.Linear(784, 100)
self.layer2 = nn.Linear(100, 100)
self.layer3 = nn.Linear(100, 100)
self.layer4 = nn.Linear(100, 100)
self.layer5 = nn.Linear(100, 10)
def forward(self, xb):
xb = xb.view(-1, 1, 28, 28)
xb = F.relu(self.layer1(xb))
xb = F.relu(self.layer2(xb))
xb = F.relu(self.layer3(xb))
xb = F.relu(self.layer4(xb))
xb = F.relu(self.layer5(xb))
return self.layer5(xb)
解决方案
您将图层设置为获得一批昏暗 784 (=28*28) 的一维向量。但是,在您的forward
函数中,您view
将输入作为一批大小为 28*28 的 2D 矩阵。
尝试将输入视为一批 1D 信号:
xb = xb.view(-1, 784)
推荐阅读
- kotlin - 即使在函数体中没有调用中缀参数,它也会被计算出来
- php - 使用 jquery 根据选定的单选按钮重新填充下拉列表
- java - 为什么我的 if 语句在 java 中不起作用?
- sql - 在 SQL Server 中将秒舍入为分钟
- python - 查找列表中的所有子序列
- json - 如何使用烧瓶和 jsonify 在 json 响应中添加嵌套数组和对象
- c# - 通过 HTTP 向 ASP.NET Core 端点资源发送空字符串
- python - 请求 session.close() 不会关闭会话
- angular - Angular:使用模式的反应式表单验证未按预期工作
- java - 如何使用 POST 方法执行 findAllBy()?