请尝试在Windows10上使用python中的pytorch在pytorch中运行Logistic回归,但在尝试训练模型时,始终遇到此错误NotimplementedError
。代码如下所示
iterations = 0
for epoch in range(num_epochs):
for i,(images,labels) in enumerate(train_data):
images = Variable(images.view(-1,28*28))
labels = Variable(labels)
optimizer.zero_grad()
output = model(images)
loss = criterion(output,labels)
loss.backward()
optimizer.step()
iterations +=1
if iterations % 500 == 0:
#calculate acuracy
correct = 0
total = 0
for images,labels in test_data:
images = Variable(images.view(-1,28*28))
output = model(images)
_, predicted = torch.max(output.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum()
accuracy = 100 * correct/total
print('Iteration {}, loss {}, accuracy {}'.format(iterations,loss.data,accuracy))