PyTorchRNN

    1. # Example code of a simple RNN, GRU, LSTM on the MNIST dataset.
    2. import torch
    3. import torchvision
    4. # 参数函数,类似激活函数
    5. import torch.nn.functional as F
    6. # standard datasets
    7. import torchvision.datasets as datasets
    8. # 可以对数据集进行转换
    9. import torchvision.transforms as transforms
    10. from torch import optim # 参数优化
    11. from torch import nn # 所有神经网络模块
    12. # Gives easier dataset managment by creating mini batches etc
    13. from torch.utils.data import DataLoader
    14. # for nice progress bar!
    15. from tqdm import tqdm
    16. # Set device cuda for GPU if it's available otherwise run on the CPU
    17. device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    18. #--------------------------------------------------------------------
    19. # Recurrent neural network (many-to-one)
    20. class RNN(nn.Module):
    21. def __init__(self,input_size,hidden_size,num_layers,num_classes):
    22. super(RNN,self).__init__()
    23. self.hidden_size = hidden_size
    24. self.num_layers = num_layers
    25. self.rnn = nn.RNN(input_size,hidden_size,num_layers,batch_first=True)
    26. self.fc = nn.Linear(hidden_size*seq_len,num_classes)
    27. def forward(self,x):
    28. # set initial hidden and cell stats
    29. h0 = torch.zeros(self.num_layers,x.size(0),self.hidden_size).to(device)
    30. out,_ = self.rnn(x,h0)
    31. out = out.reshape(x.shape[0],-1)
    32. out = self.fc(out)
    33. return out
    34. #--------------------------------------------------------------------
    35. class RNN_GRU(nn.Module):
    36. def __int__(self,input_size,hidden_size,num_layers,num_classes):
    37. super(RNN_GRU,self).__init__()
    38. self.hidden_size = hidden_size
    39. self.num_layers = num_layers
    40. selg.gru = nn.GRU(input_size,hidden_size,num_layers,batch_size=True)
    41. self.fc = nn.Linear(hidden_size*seq_len,num_classes)
    42. def forward(self,x):
    43. h0 = torch.zeros(num_layers,x.size(0),self.hidden_size).to(device)
    44. out,_ = self.gru(x,h0)
    45. out = out.reshape(x.size(0),-1)
    46. out =self.fc(out)
    47. return out
    48. #--------------------------------------------------------------------
    49. class RNN_LSTM(nn.Module):
    50. def __init__(self,input_size,hidden_size,num_layers,num_classes):
    51. super(RNN_LSTM,self).__init__()
    52. self.hidden_size = hidden_size
    53. self.num_layers = num_layers
    54. self.lstm = nn.LSTM(input_size,hidden_size,num_layers,batch_first=True)
    55. self.fc = nn.Linear(hidden_size*seq_len,num_classes)
    56. def forward(self,x):
    57. c0 = torch.zeros(num_layers,x.size(0),self.hidden_size)
    58. h0 = torch.zeros(num_layers,x.size(0),self.hidden_size)
    59. out,_ = self.lstm(x,(h0,c0))
    60. # out: tensor of shape (batch_size, seq_length, hidden_size)
    61. out = out.reshape(out.shape[0], -1)
    62. out = self.fc(out)
    63. return out
    64. #--------------------------------------------------------------------
    65. # 参数设置
    66. input_size = 28
    67. num_classes = 10
    68. learning_rate = 0.001
    69. batch_size = 64
    70. num_epochs = 3
    71. num_layers = 2
    72. seq_len = 28
    73. hidden_size = 256
    74. # Load Training and Test data
    75. train_dataset = datasets.MNIST(root='dataset/',train=True,transform=transforms.ToTensor(),download=True)
    76. test_dataset = datasets.MNIST(root='dataset/',train=False,transform=transforms.ToTensor(),download=True)
    77. train_loader = DataLoader(dataset=train_dataset,shuffle=True,batch_size=batch_size)
    78. test_loader = DataLoader(dataset=test_dataset,shuffle=True,batch_size=batch_size)
    79. #--------------------------------------------------------------------
    80. # 初始化网络
    81. model = RNN_LSTM(input_size, hidden_size, num_layers, num_classes).to(device)
    82. # loss and optimizer
    83. criterion= nn.CrossEntropyLoss()
    84. optimizer = optim.Adam(model.parameters(),lr=learning_rate)
    85. # 训练网络
    86. for epoch in range(num_epochs):
    87. for batch_idxm,(data,targets) in enumerate(tqdm(train_loader)):
    88. data = data.to(device).squeeze(1)
    89. targets = targets.to(device)
    90. # forward
    91. outputs = model(data)
    92. # 计算损失
    93. loss = criterion(outputs,targets)
    94. # 向后传播
    95. loss.backward()
    96. # 梯度归0
    97. optimizer.zero_grad()
    98. # 梯度优化
    99. optimizer.step()
    100. #--------------------------------------------------------------------
    101. # Check accuracy on training & test to see how good our model
    102. def check_accuracy(loader, model):
    103. num_correct = 0
    104. num_samples = 0
    105. model.eval()
    106. with torch.no_grad():
    107. for x, y in loader:
    108. x = x.to(device=device).squeeze(1)
    109. y = y.to(device=device)
    110. outputs = model(x)
    111. _, indexes = outputs.max(1)
    112. num_correct += (indexes == y).sum()
    113. num_samples += indexes.size(0) # batch_size
    114. model.train()
    115. return num_correct/num_samples
    116. #--------------------------------------------------------------------
    117. print(f"Accuracy on training set: {check_accuracy(train_loader, model)*100:.2f}")
    118. print(f"Accuracy on test set: {check_accuracy(test_loader, model)*100:.2f}")
    119. # Accuracy on training set: 10.22
    120. # Accuracy on test set: 10.10