pytorch-mnist neural network training

Net.py network configuration in which, for the structure of the network input 28 * 28, the output of the first layer to the hidden layer 300, outputs the output of the second layer is 100, the last layer is the output layer 10,  

net.py 

import torch
from torch import nn

class Batch_Net(nn.Module):
    def __init__(self, in_dim, n_hidden_1, n_hidden_2, out_dim):
        super(Batch_Net, self).__init__()
        self.layer_1 = nn.Sequential(nn.Linear(in_dim, n_hidden_1), nn.BatchNorm1d(n_hidden_1), nn.ReLU(True))
        self.layer_2 = nn.Sequential(nn.Linear(n_hidden_1, n_hidden_2), nn.BatchNorm1d(n_hidden_2), nn.ReLU(True))
        self.output = nn.Sequential(nn.Linear(n_hidden_2, out_dim))

    def forward(self, x):
        x = self.layer_1(x)
        x = self.layer_2(x)
        x = self.output(x)
        return x

main.py training network 

Import Torch
 from Torch Import NN, Optim
 from torch.autograd Import Variable
 from torch.utils.data Import DataLoader
 from torchvision Import Datasets, TRANSFORMS 


Import NET 


batch_size = 128   # each batch_size size 
learning_rate = 1E-2 # learning rate magnitude 
num_epoches = 20   # iteration epoch value 
 # represents the data between the data becomes 0, 1, 0.5, 0.5 represents the mean minus the standard deviation imposed 
data_tf = transforms.Compose ([transforms.ToTensor () , transforms.Normalize ([0.5] , [0.5])])   #Represents the mean and standard deviation 
# obtain data of the training set 
train_dataset = datasets.MNIST (= the root ' ./data ' , Train = True, Transform = data_tf, downloads = True)
 # obtain data test set 
test_dataset = datasets.MNIST (root = ' ./data ' , train = False, Transform = data_tf, downloads = True)
 # obtain the training set may be iteratively queue 
train_loader = DataLoader (train_dataset, the batch_size = the batch_size, shuffle = True)
 # obtaining a test set may be iteratively queue 
test_loader DataLoader = (test_dataset, the batch_size = the batch_size, shuffle = False)
 # network configuration model 
model net.Batch_Net = (28 * 28, 300, 100, 10 )
 IFtorch.cuda.is_available (): # if cuda model will be on the GPU 
    model.cuda () 

Criterion = nn.CrossEntropyLoss () # constructor cross loss function 
optimizer = optim.SGD (model.parameters (), lr = learning_rate) # configured model optimizer 

for Epoch in Range (num_epoches): # iterations Epoch 
    train_loss = 0 # loss training value 
    test_loss = 0 # loss measurement value 
    eval_acc = 0 # accuracy of the test set 
    for Data in train_loader:   # obtain a sample of a batch 
        img, the Data label = # get pictures and labels
        img.view = img (img.size (0), -1) # The conversion img picture 
        IF torch.cuda.is_available (): # If there Torch 
            img = Variable (img) .cuda () # pictures put on the torch 
            label = variable (label) .cuda () # and the labels on the torch 
        the else : 
            img = variable (img)   # configured img variable 
            label = variable (label) 
        optimizer.zero_grad () # eliminate optimizer gradient 
        out model.forward = (IMG) # performing forward propagation 
        loss = Criterion (OUT, label) # calculates loss values 
        loss.backward ()# After the propagation loss value 
        () optimizer.step # optimization optimizer 
        train_loss loss.data + = #  
    for Data in test_loader: 
        IMG, label = Data 
        IMG = img.view (img.size (0), - . 1 )
         IF torch.cuda.is_available (): 
            IMG = Variable (IMG, volatile = True) .cuda () 
            label = Variable (label, volatile = True) .cuda ()
         the else : 
            IMG = Variable (IMG, volatile = True ) 
            label = Variable (label, volatile =True) 
        OUT = model.forward (IMG) 
        Loss = Criterion (OUT, label) 
        test_loss + = loss.data 
        top_p, top_class = out.topk (. 1, Dim =. 1) # obtains an output of each sample maximum loss 
        equals = == label.view top_class (* top_class.shape) # determines whether or not the two samples are equal to the tag 
        accuracy = torch.mean (equals.type (torch.FloatTensor)) # calculation accuracy 
        eval_acc + = accuracy
     Print ( ' train_loss {: } .6f, test_loss {:. 6F}, Acc: {} 6F :. '.format(train_loss / len(train_loader), test_loss / len(test_loader), eval_acc / len(test_loader)))

 

Guess you like

Origin www.cnblogs.com/my-love-is-python/p/11719714.html