The code is as follows, U I think the most important thing for newbies is to learn the format in which rnn reads the data.
# -*- coding: utf-8 -*- """ Created on Tue Oct 9 08:53:25 2018 @author: www """ import sys ('..') import torch import datetime from import Variable from torch import nn from import DataLoader from torchvision import transforms as tfs from import MNIST #Define the data data_tf = ([ (), ([0.5], [0.5]) ]) train_set = MNIST('E:/data', train=True, transform=data_tf, download=True) test_set = MNIST('E:/data', train=False, transform=data_tf, download=True) train_data = DataLoader(train_set, 64, True, num_workers=4) test_data = DataLoader(test_set, 128, False, num_workers=4) # Define the model class rnn_classify(): def __init__(self, in_feature=28, hidden_feature=100, num_class=10, num_layers=2): super(rnn_classify, self).__init__() = (in_feature, hidden_feature, num_layers)#Use two layers of lstm = (hidden_feature, num_class)# will be the last rnn to be fully concatenated to the final output def forward(self, x): The size of #x is (batch, 1, 28,28), so we need to convert it to the rnn input format (28, batch, 28) x = () # Remove the 1 from (batch, 1,28,28) to (batch, 28,28) x = (2, 0, 1)# Putting the last dimension into the first, it becomes (batch, 28,28) out, _ = (x) # Using the default hidden state, the out obtained is (28, batch, hidden_feature) out = out[-1,:,:]# Take the last in the sequence with size (batch, hidden_feature) out = (out) # Getting classification results return out net = rnn_classify() criterion = () optimizer = ((), 1e-1) # Define the training process def get_acc(output, label): total = [0] _, pred_label = (1) num_correct = (pred_label == label).sum().item() return num_correct / total def train(net, train_data, valid_data, num_epochs, optimizer, criterion): if .is_available(): net = () prev_time = () for epoch in range(num_epochs): train_loss = 0 train_acc = 0 net = () for im, label in train_data: if .is_available(): im = Variable(()) # (bs, 3, h, w) label = Variable(()) # (bs, h, w) else: im = Variable(im) label = Variable(label) # forward output = net(im) loss = criterion(output, label) # backward optimizer.zero_grad() () () train_loss += () train_acc += get_acc(output, label) cur_time = () h, remainder = divmod((cur_time - prev_time).seconds, 3600) m, s = divmod(remainder, 60) time_str = "Time %02d:%02d:%02d" % (h, m, s) if valid_data is not None: valid_loss = 0 valid_acc = 0 net = () for im, label in valid_data: if .is_available(): im = Variable(()) label = Variable(()) else: im = Variable(im) label = Variable(label) output = net(im) loss = criterion(output, label) valid_loss += () valid_acc += get_acc(output, label) epoch_str = ( "Epoch %d. Train Loss: %f, Train Acc: %f, Valid Loss: %f, Valid Acc: %f, " % (epoch, train_loss / len(train_data), train_acc / len(train_data), valid_loss / len(valid_data), valid_acc / len(valid_data))) else: epoch_str = ("Epoch %d. Train Loss: %f, Train Acc: %f, " % (epoch, train_loss / len(train_data), train_acc / len(train_data))) prev_time = cur_time print(epoch_str + time_str) train(net, train_data, test_data, 10, optimizer, criterion)
Above this pytorch using lstm to do mnist handwritten digit recognition classification example is all I share with you, I hope to give you a reference, and I hope you support me more.