问题描述
这是错误:
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