我已经上传了我的代码下面的链接,我不确定的参数和大小.感谢帮助code here
fhg3lkii1#
我发现了几个错误-首先你需要扁平化图像,你需要一个热编码BCE损失或你应该使用交叉熵损失,你需要激活函数,例如Sigmoid,请参阅下面的代码
mld = nn.Sequential(nn.Flatten(), nn.Linear(784,10), nn.Sigmoid()) loss_list=[] epochs=100 loss_fn = nn.BCELoss() optimizer = optim.SGD(mld.parameters(), lr=0.1) mld.train() for epoch in range(epochs): for X_train, y_train in train_loader: pred = mld(X_train.float()) print(pred.shape) print(torch.nn.functional.one_hot(y_train).shape) loss = loss_fn(pred, torch.nn.functional.one_hot(y_train, num_classes=10).float()) optimizer.zero_grad() loss.backward() print(loss.item()) loss_list.append(loss.item()) optimizer.step() mld.eval() with torch.no_grad(): y_pred = mld(X_train) accuracy = (y_pred.round() == y).float().mean() print("Accuracy {:.2f}".format(accuracy * 100))
字符串
1条答案
按热度按时间fhg3lkii1#
我发现了几个错误-首先你需要扁平化图像,你需要一个热编码BCE损失或你应该使用交叉熵损失,你需要激活函数,例如Sigmoid,请参阅下面的代码
字符串