【发布时间】:2021-07-27 09:46:02
【问题描述】:
import torch
import torch.nn as nn
import torch.nn.functional as F
class Net(nn.Module):
def __init__(self):
super().__init__()
self.fc1=nn.Linear(28*28,64)
self.fc2=nn.Linear(64,64)
self.fc3=nn.Linear(64,64)
self.fc4=nn.Linear(64,10)
def forward(self,x):
x=F.relu(self.fc1(x))
x=F.relu(self.fc2(x))
x=F.relu(self.fc3(x))
x=self.fc4(x)
return F.log_softmax(x,dim=1)
net=Net()
print(net)
X=torch.rand((28,28))
X=X.unsqueeze(0)
output=net(X)
print(output)
RuntimeError: mat1 and mat2 shapes cannot be multiplied (28x28 and 784x64)
当我使用 X=X.view(-1,28*28) 代替 X=X.unsqueeze(0) 时,它给了我想要的结果。我无法完全理解这里使用 unsqueeze() 和 view() 的区别。
【问题讨论】:
标签: deep-learning pytorch