Feedforward_neural_network
import torch
import torch. nn as nn
import torchvision
import torchvision. transforms as transforms
device = torch. device( 'cuda' if torch. cuda. is_available( ) else 'cpu' )
print ( device)
cuda
input_size = 28 * 28
hidden_size = 500
num_classes = 10
num_epochs = 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( '../../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= True )
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)
criterion = nn. CrossEntropyLoss( )
optimizer = torch. optim. Adam( model. parameters( ) , lr= learning_rate)
total_step = len ( train_loader)
for epoch in range ( num_epochs) :
for i, ( images, labels) in enumerate ( train_loader) :
images = images. reshape( - 1 , input_size) . to( device)
labels = labels. to( device)
outputs = model( images)
loss = criterion( outputs, labels)
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
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 %
torch. save( model. state_dict( ) , 'model_param.ckpt' )
model = model. load_state_dict( torch. load( 'model_param.ckpt' ) )