# third-party library import torch import torch.nn as nn from torch.autograd import Variable import torch.utils.data as Data import torchvision
# torch.manual_seed(1) # reproducible
# Hyper Parameters EPOCH = 1# train the training data n times, to save time, we just train 1 epoch BATCH_SIZE = 50 LR = 0.001# learning rate DOWNLOAD_MNIST = False
# Mnist digits dataset if not(os.path.exists('./mnist/')) or not os.listdir('./mnist/'): # not mnist dir or mnist is empyt dir DOWNLOAD_MNIST = True
train_data = torchvision.datasets.MNIST( root='./mnist/', train=True,# this is training data transform=torchvision.transforms.ToTensor(),# Converts a PIL.Image or numpy.ndarray to # torch.FloatTensor of shape (C x H x W) and normalize in the range [0.0, 1.0] download=DOWNLOAD_MNIST, )
# Data Loader for easy mini-batch return in training, the image batch shape will be (50, 1, 28, 28) train_loader = Data.DataLoader(dataset=train_data,batch_size=BATCH_SIZE,shuffle=True)
# convert test data into Variable, pick 2000 samples to speed up testing test_data = torchvision.datasets.MNIST(root='./mnist/',train=False) test_x = Variable(torch.unsqueeze(test_data.test_data, dim=1),volatile=True).type(torch.FloatTensor)[:2000]/255.# shape from (2000, 28, 28) to (2000, 1, 28, 28), value in range(0,1) test_y = test_data.test_labels[:2000]
def forward(self, x): x = self.conv1(x) x = self.conv2(x) x = x.view(x.size(0), -1) # flatten the output of conv2 to (batch_size, 32 * 7 * 7) output = self.out(x) return output, x # return x for visualization
cnn = CNN()
optimizer = torch.optim.Adam(cnn.parameters(), lr=LR)# optimize all cnn parameters loss_func = nn.CrossEntropyLoss() # the target label is not one-hotted
# training and testing for epoch in range(EPOCH): for step, (x, y) in enumerate(train_loader): # gives batch data, normalize x when iterate train_loader b_x = Variable(x) # batch x b_y = Variable(y) # batch y
output = cnn(b_x)[0] # cnn output loss = loss_func(output, b_y) # cross entropy loss optimizer.zero_grad() # clear gradients for this training step loss.backward() # backpropagation, compute gradients optimizer.step() # apply gradients