【发布时间】:2019-04-13 14:19:12
【问题描述】:
问题
我正在尝试使用不同的方法(TensorFlow、PyTorch 和从头开始)实现 2 层神经网络,然后根据 MNIST 数据集比较它们的性能。
我不确定自己犯了什么错误,但在 PyTorch 中准确率只有 10% 左右,这基本上是随机猜测。我认为权重可能根本没有更新。
请注意,我有意使用 TensorFlow 提供的数据集,以通过 3 种不同的方法使我使用的数据保持一致,以便进行准确的比较。
from tensorflow.examples.tutorials.mnist import input_data
import torch
class Net(torch.nn.Module):
def __init__(self):
super(Net, self).__init__()
self.fc1 = torch.nn.Linear(784, 100)
self.fc2 = torch.nn.Linear(100, 10)
def forward(self, x):
# x -> (batch_size, 784)
x = torch.relu(x)
# x -> (batch_size, 10)
x = torch.softmax(x, dim=1)
return x
net = Net()
net.zero_grad()
Loss = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(net.parameters(), lr=0.01)
for epoch in range(1000): # loop over the dataset multiple times
batch_xs, batch_ys = mnist_m.train.next_batch(100)
# convert to appropriate settins
# note the input to the linear layer should be (n_sample, n_features)
batch_xs = torch.tensor(batch_xs, requires_grad=True)
# batch_ys -> (batch_size,)
batch_ys = torch.tensor(batch_ys, dtype=torch.int64)
# forward
# output -> (batch_size, 10)
output = net(batch_xs)
# result -> (batch_size,)
result = torch.argmax(output, dim=1)
loss = Loss(output, batch_ys)
# backward
optimizer.zero_grad()
loss.backward()
optimizer.step()
【问题讨论】:
标签: deep-learning pytorch