# feed forward nn
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision.datasets as dsets
import torchvision.transforms as transforms
from torch.autograd import Variable
import argparse
parser = argparse.ArgumentParser(description='feedforward_mnist')
# learning & saving parameterss
parser.add_argument('-train', default = False, help = 'train the model')
parser.add_argument('-test', default = True, help = 'test the model')
parser.add_argument('-learning_rate', type = float, default = 0.005, help = 'initial learning rate [default = 0.001')
parser.add_argument('-num_epochs', type = int, default = 5, help = 'number of epochs of training [default = 10')
parser.add_argument('-batch_size', type = int, default = 100, help = 'batch size for training')
parser.add_argument('-input_size', type = int, default = 784, help = 'input size')
parser.add_argument('-hidden_size', type = int, default = 500, help = 'hidden size')
parser.add_argument('-output_size',type = int, default = 1, help = 'output size')
parser.add_argument('-num_classes', type = int, default = 10, help = 'hidden layer number')
parser.add_argument('-cuda',default = False, help = 'enable gpu')
args = parser.parse_args()
# Hyper Parameters
input_size = args.input_size
hidden_size = args.hidden_size
num_classes = args.num_classes
num_epochs = args.num_epochs
batch_size = args.batch_size
learning_rate = args.learning_rate
class Net(nn.Module):
def __init__(self, input_size, hidden_size, num_classes):
super(Net, self).__init__()
self.layer1 = nn.Linear(input_size, hidden_size)
# self.relu = nn.ReLU()
self.layer2 = nn.Linear(hidden_size, num_classes)
def forward(self, x):
x = F.leaky_relu(self.layer1(x))
x = self.layer2(x)
return x
net = Net(input_size, hidden_size, num_classes)
if args.train == True:
train_dataset = dsets.MNIST(root='./data',
train=args.train,
transform=transforms.ToTensor(),
download=True)
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
batch_size=batch_size,
shuffle=True)
# Loss and Optimizer
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(net.parameters(), lr=learning_rate)
# Train the Model
for epoch in range(num_epochs):
for i, (images, labels) in enumerate(train_loader):
# Convert torch tensor to Variable
images = Variable(images.view(-1, 28*28))# image.size() = ([100, 1, 28, 28])
labels = Variable(labels)
# Forward + Backward + Optimize
optimizer.zero_grad() # zero the gradient buffer
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_epochs, i+1, len(train_dataset)//batch_size, loss.data[0]))
torch.save(net.state_dict(), 'model.pkl')
# save only parameters
# MNIST Dataset
if args.test == True:
test_dataset = dsets.MNIST(root='./data',
train=args.train,
transform=transforms.ToTensor())
test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
batch_size=batch_size,
shuffle=False)
##############################
net.load_state_dict(torch.load('model.pkl'))
# load parameters to net model
##############################
correct = 0
total = 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 of the network on the 10000 test images: %d %%' % (100 * correct / total))
网友评论