如何解决使用 Pytorch 在 Python 中进行多层感知器深度学习
我在 MLP 中执行我的代码的训练函数时出错。
这是错误:
mat1 和 mat2 形状不能相乘(128x10 和 48x10)
我的火车功能代码是这样的:
class net(nn.Module):
def __init__(self,input_dim2,hidden_dim2,output_dim2):
super(net,self).__init__()
self.input_dim2 = input_dim2
self.fc1 = nn.Linear(input_dim2,hidden_dim2)
self.relu = nn.ReLU()
self.fc2 = nn.Linear(hidden_dim2,hidden_dim2)
self.fc3 = nn.Linear(hidden_dim2,output_dim2)
def forward(self,x):
x = self.fc1(x)
x = self.relu(x)
x = self.fc2(x)
x = self.relu(x)
x = self.fc3(x)
x = F.softmax(self.fc3(x))
return x
model = net(input_dim2,output_dim2) #create the network
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.RMSprop(model.parameters(),lr = learning_rate2)
def train(num_epochs2):
for i in range(num_epochs2):
tmp_loss = []
for (x,y) in train_loader:
print(y.shape)
print(x.shape)
outputs = model(x) #forward pass
print(outputs.shape)
loss = criterion(outputs,y) #loss computation
tmp_loss.append(loss.item()) #recording the loss
optimizer.zero_grad() #all the accumulated gradient
loss.backward() #auto-differentiaton - accumulation of gradient
optimizer.step() # a gradient step
print("Loss at {}th epoch: {}".format(i,np.mean(tmp_loss)))
我不知道我错在哪里。我的代码似乎工作正常。
解决方法
从有限的信息来看,我猜你错的地方是以下片段:
x = self.fc3(x)
x = F.softmax(self.fc3(x))
尝试替换为:
x = self.fc3(x)
x = F.softmax(x)
一个好的问题应该包括:错误回溯信息和可能重复错误的完整玩具示例!
,此处“init”函数中似乎缺少 relu
激活。或者在 forward 函数中有一个额外的 relu
激活。查看下面的代码并尝试找出多余的或缺失的。
def __init__(self,input_dim2,hidden_dim2,output_dim2):
super(net,self).__init__()
self.input_dim2 = input_dim2
self.fc1 = nn.Linear(input_dim2,hidden_dim2)
self.relu = nn.ReLU()
self.fc2 = nn.Linear(hidden_dim2,hidden_dim2)
self.fc3 = nn.Linear(hidden_dim2,output_dim2)
def forward(self,x):
x = self.fc1(x)
x = self.relu(x)
x = self.fc2(x)
x = self.relu(x)
x = self.fc3(x)
x = F.softmax(self.fc3(x))
return x
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。