import torch
from torchvision import transforms
from torchvision import datasets
from torch.utils.data import DataLoader
import torch.nn.functional as F
import torch.optim as optim
def train(epoch):
running_loss =0.0for batch_idx, data in enumerate(train_loader,0):
inputs, target = data
optimizer.zero_grad()#forward+ backward + update
outputs =model(inputs)
loss =criterion(outputs, target)
loss.backward()
optimizer.step()
running_loss += loss.item()if batch_idx %300==299:print('[%d, %5d] loss: %.3f'%(epoch +1, batch_idx +1, running_loss /300))
running_loss =0.0
def test():
correct =0
total =0
with torch.no_grad():for data in test_loader:
images, labels = data
outputs =model(images)
_, predicted = torch.max(outputs.data, dim=1)
total += labels.size(0)
correct +=(predicted == labels).sum().item()print('Accuracy on test set: %d %%'%(100* correct / total))
if __name__ =='__main__':for epoch in range(10):train(epoch)test()