MyFeedForwardNeuralNet

MyFeedForwardNeuralNet_第1张图片
image.png

MyFeedForwardNeuralNet_第2张图片
image.png

MyFeedForwardNeuralNet_第3张图片
image.png
import torch
import torch.nn as nn
import torch.nn.functional as F 
import torchvision
# import torchvision.datasets as datasets
import torchvision.transforms as transforms
from torch.autograd import Variable

input_size = 784
hidden_size = 500
num_classes = 10
output_size = num_classes
num_epoches = 5
batch_size = 100
learning_rate = 0.001

train_dataset = torchvision.datasets.MNIST(root = './data', train = True, transform = transforms.ToTensor(), download = True)
test_dataset = torchvision.datasets.MNIST(root = './data', train = False, transform = transforms.ToTensor(), )
train_loader = torch.utils.data.DataLoader(dataset = train_dataset, batch_size = batch_size, shuffle = True)
test_loader = torch.utils.data.DataLoader(dataset = test_dataset, batch_size = batch_size, shuffle = False)

class Net(nn.Module):
    def __init__(self, input_size, hidden_size, output_size):
        super(Net, self).__init__()
        self.layer1 = nn.Linear(input_size, hidden_size)
        self.layer2 = nn.Linear(hidden_size, output_size)
    def forward(self, x):
        x = F.relu(self.layer1(x))
        x = self.layer2(x)
        return x

net = Net(input_size, hidden_size, output_size)
print(net)

net.load_state_dict(torch.load('feedforward_parameters.pkl'))
# criterion = nn.CrossEntropyLoss()
# optimizer = torch.optim.Adam(net.parameters(), lr= learning_rate)

# for epoch in range(num_epoches):
#   for i, (images, labels) in enumerate(train_loader):
#       images = Variable(images.view(-1, 28*28))
#       labels = Variable(labels)
#       optimizer.zero_grad()
#       outputs = net(images)
#       loss = criterion(outputs, labels)
#       loss.backward()
#       optimizer.step()
#       if (i+1)%100 == 0:
#           print ('Epoch [%d/%d], Step [%d/%d], Loss: %.4f' % (epoch+1, num_epoches, i+1, len(train_dataset)//batch_size, loss.data[0]))
# # 60000 train_dataset, batchsize = 100, the ith batch in 600 batches


correct = 0.0
total = 0.0
for images, labels in test_loader:
    images = Variable(images.view(-1, 28*28))
    outputs = net(images)
    _, predicted = torch.max(outputs.data, 1)
    total += labels.size(0)
    correct += (predicted == labels).sum()

print('Accuracy:%.2lf %%' % (100*correct/total))

torch.save(net.state_dict(),'feedforward_parameters.pkl')

net.load_state_dict(torch.load('feedforward_parameters.pkl'))
torch.save(net.state_dict(),'feedforward_parameters.pkl')

你可能感兴趣的:(MyFeedForwardNeuralNet)