欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

RuntimeError: size mismatch, m1: [80 x 4], m2: [320 x 50] at ..\aten\src\TH/generic/THTensorMath.cpp

发布时间:2024/8/23 57 豆豆
生活随笔 收集整理的这篇文章主要介绍了 RuntimeError: size mismatch, m1: [80 x 4], m2: [320 x 50] at ..\aten\src\TH/generic/THTensorMath.cpp 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

RuntimeError: size mismatch, m1: [80 x 4], m2: [320 x 50] at …\aten\src\TH/generic/THTensorMath.cpp:41

使用pytorch进行深度学习的训练会出现这种问题,原因是fc全连接层的输入维度问题,由于输入是二维的数据,很多时候在输入全连接层的时候我们没有调整维度
例如:

def forward(self, input):x = self.pool1(F.relu(self.conv1(input)))x = self.pool2(F.relu(self.conv2(x)))x = self.fc1(x)x = self.fc2(x)return x

我们的fc1的输入将是804的情况,而需要的是3201的情况,这时会报错

改为:

def forward(self, input):x = self.pool1(F.relu(self.conv1(input)))x = self.pool2(F.relu(self.conv2(x))).view(320)x = self.fc1(x)x = self.fc2(x)return x

使用view调整维度即可

总结

以上是生活随笔为你收集整理的RuntimeError: size mismatch, m1: [80 x 4], m2: [320 x 50] at ..\aten\src\TH/generic/THTensorMath.cpp的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。