feedforward_neural_network

Feedforward_neural_network

# Import necessary packages.
import torch
import torch.nn as nn
import torchvision
import torchvision.transforms as transforms
# Device configuration.
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print(device)
cuda
# Set the Hyper-parameters.
input_size = 28 * 28    # 784
hidden_size = 500
num_classes = 10
num_epochs = 5
batch_size = 100
learning_rate = 0.001
# Get the MNIST dataset.
train_dataset = torchvision.datasets.MNIST(root='../../data/',
                                           train=True,
                                           transform=transforms.ToTensor(),
                                           download=True)

test_dataset = torchvision.datasets.MNIST('../../data/',
                                          train=False,
                                          transform=transforms.ToTensor())
# Define the Data Loader.
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=True)
# Fully connected neural network with one hidden layer
class NeuralNet(nn.Module):
    def __init__(self, input_size, hidden_size, num_classes):
        super(NeuralNet, self).__init__()
        self.fc1 = nn.Linear(input_size, hidden_size)
        self.relu = nn.ReLU()
        self.fc2 = nn.Linear(hidden_size, num_classes)

    def forward(self, x):
        out = self.fc1(x)
        out = self.relu(out)
        out = self.fc2(out)
        return out
    
model = NeuralNet(input_size, hidden_size,num_classes).to(device)
# Loss and optimizer.
criterion  = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
# Train the model.
total_step = len(train_loader)
for epoch in range(num_epochs):
    for i, (images, labels) in enumerate(train_loader):
        # Move tensors to the configured device.
        images = images.reshape(-1, input_size).to(device)
        labels = labels.to(device)

        # Forward pass.
        outputs = model(images)
        loss = criterion(outputs, labels)

        # Backward and optimize.
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        if (i+1) % 100 == 0:
            print('Epoch [{}/{}], Step [{}/{}], loss: {:.4f}'
                  .format(epoch+1, num_epochs, i+1, total_step, loss.item()))
Epoch [1/5], Step [100/600], loss: 0.2747
Epoch [1/5], Step [200/600], loss: 0.2937
Epoch [1/5], Step [300/600], loss: 0.3607
Epoch [1/5], Step [400/600], loss: 0.2670
Epoch [1/5], Step [500/600], loss: 0.2306
Epoch [1/5], Step [600/600], loss: 0.1151
Epoch [2/5], Step [100/600], loss: 0.0780
Epoch [2/5], Step [200/600], loss: 0.1091
Epoch [2/5], Step [300/600], loss: 0.1936
Epoch [2/5], Step [400/600], loss: 0.1072
Epoch [2/5], Step [500/600], loss: 0.0502
Epoch [2/5], Step [600/600], loss: 0.0394
Epoch [3/5], Step [100/600], loss: 0.1356
Epoch [3/5], Step [200/600], loss: 0.0447
Epoch [3/5], Step [300/600], loss: 0.0266
Epoch [3/5], Step [400/600], loss: 0.0744
Epoch [3/5], Step [500/600], loss: 0.1216
Epoch [3/5], Step [600/600], loss: 0.0685
Epoch [4/5], Step [100/600], loss: 0.0739
Epoch [4/5], Step [200/600], loss: 0.0348
Epoch [4/5], Step [300/600], loss: 0.0894
Epoch [4/5], Step [400/600], loss: 0.0911
Epoch [4/5], Step [500/600], loss: 0.0651
Epoch [4/5], Step [600/600], loss: 0.0704
Epoch [5/5], Step [100/600], loss: 0.0388
Epoch [5/5], Step [200/600], loss: 0.0231
Epoch [5/5], Step [300/600], loss: 0.0143
Epoch [5/5], Step [400/600], loss: 0.1222
Epoch [5/5], Step [500/600], loss: 0.0317
Epoch [5/5], Step [600/600], loss: 0.1114
# Test the model.
# In test phase, we don't need to compute gradients (for memory efficiency).
with torch.no_grad():
    correct = 0
    total = 0
    for images, labels in test_loader:
        images = images.reshape(-1, input_size).to(device)
        labels = labels.to(device)
        outputs = model(images)
        _, predicted = torch.max(outputs.data, 1)
        total += labels.size(0)
        correct += (predicted == labels).sum().item()

    print('Accuracy of the network on the 10000 test images: {} %'.format(100 * correct / total))
Accuracy of the network on the 10000 test images: 97.86 %
# Save the model checkpoint.
torch.save(model.state_dict(), 'model_param.ckpt')
# Alternative method to save the hole model
# torch.save(model, 'model.ckpt')

# Load the model checkpoint.
model = model.load_state_dict(torch.load('model_param.ckpt'))
# Alternative mothod to load the hole model
# model = torch.load('model.ckpt')

你可能感兴趣的:(神经网络与深度学习,深度学习,人工智能,神经网络,pytorch)