1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| import torch from torch import nn import torchvision.datasets as datasets import torchvision.transforms as transforms
torch.manual_seed(2019)
EPOCH = 1 BATCH_SIZE = 32 TIME_STEP = 28 INPUT_SIZE = 28 INIT_LR = 0.01 DOWNLOAD_MNIST = True
train_data = datasets.MNIST(root='mnist', train=True, transform=transforms.ToTensor(), download=DOWNLOAD_MNIST) train_loader = torch.utils.data.DataLoader(dataset=train_data, batch_size=BATCH_SIZE, shuffle=True) test_data = datasets.MNIST(root='mnist', train=False) test_x = test_data.test_data.type(torch.FloatTensor)[:2000] / 255. test_y = test_data.test_labels.numpy()[:2000]
class RNN(nn.Module): def __init__(self): super(RNN, self).__init__() self.rnn = nn.LSTM( input_size=INPUT_SIZE, hidden_size=64, num_layers=1, batch_first=True ) self.out = nn.Linear(64, 10)
def forward(self, x): r_out, (h_n, h_c) = self.rnn(x) out = self.out(r_out[:, -1, :]) return out
rnn = RNN() print(rnn)
optimizer = torch.optim.Adam(rnn.parameters(), lr=INIT_LR) loss_func = nn.CrossEntropyLoss()
for epoch in range(EPOCH): for step, (b_x, b_y) in enumerate(train_loader): b_x = b_x.view(-1, TIME_STEP, INPUT_SIZE) output = rnn(b_x) loss = loss_func(output, b_y) optimizer.zero_grad() loss.backward() optimizer.step()
if step % 50 == 0: prediction = rnn(test_x) pred_y = torch.max(prediction, 1)[1].data.numpy() accuracy = (pred_y == test_y).sum() / float(test_y.size) print(f'Epoch: [{step}/{epoch}]', f'| train loss: {loss.item()}', f'| test accuracy: {accuracy}')
prediction = rnn(test_x[:20].view(-1, 28, 28)) pred_y = torch.max(prediction, 1)[1].data.numpy() print(pred_y, 'prediction number') print(test_y[:20], 'real number')
|