当前位置:   article > 正文

pytorch实现GRU(附code)_pytorch gru

pytorch gru

接上一篇文章(pytorch实现LSTM),在实现LSTM之后,又发现了GRU网络。

说GRU是LSTM网络的一种效果很好的变体,它较LSTM网络的结构更加简单,而且效果也很好,也是当前非常流形的一种网络。

所以决定尝试一下!

注:数据集仍然使用上文中的IGBT加速老化数据集(3),数据与处理方法不变,直接上代码!!!

在保证输入样本参数和学习率不变的情况下,即input_size = 5,out_oput = 1,lr = 0.01,试了很多参数,发现在训练集80%,测试集20%,hidden_size = 30,使用两个全连接层时结果最好,与上文的LSTM效果接近(代码为CPU版本哦)。

(说好的更简单,效果更好,不明白为啥使用的隐藏层和全连接层要更多了???)

  1. import scipy.io as sio
  2. import numpy as np
  3. import torch
  4. from torch import nn
  5. import matplotlib.pyplot as plt
  6. import matplotlib
  7. import pandas as pd
  8. from torch.autograd import Variable
  9. import math
  10. import csv
  11. # Define LSTM Neural Networks
  12. class GruRNN(nn.Module):
  13. """
  14. Parameters:
  15. - input_size: feature size
  16. - hidden_size: number of hidden units
  17. - output_size: number of output
  18. - num_layers: layers of LSTM to stack
  19. """
  20. def __init__(self, input_size, hidden_size=1, output_size=1, num_layers=1):
  21. super().__init__()
  22. self.gru = nn.GRU(input_size, hidden_size, num_layers) # utilize the GRU model in torch.nn
  23. self.linear1 = nn.Linear(hidden_size, 16) # 全连接层
  24. self.linear2 = nn.Linear(16, output_size) # 全连接层
  25. def forward(self, _x):
  26. x, _ = self.gru(_x) # _x is input, size (seq_len, batch, input_size)
  27. s, b, h = x.shape # x is output, size (seq_len, batch, hidden_size)
  28. x = x.view(s * b, h)
  29. x = self.linear1(x)
  30. x = self.linear2(x)
  31. x = x.view(s, b, -1)
  32. return x
  33. if __name__ == '__main__':
  34. # checking if GPU is available
  35. device = torch.device("cpu")
  36. if (torch.cuda.is_available()):
  37. device = torch.device("cuda:0")
  38. print('Training on GPU.')
  39. else:
  40. print('No GPU available, training on CPU.')
  41. # 数据读取&类型转换
  42. data_x = np.array(pd.read_csv('Data_x.csv', header=None)).astype('float32')
  43. data_y = np.array(pd.read_csv('Data_y.csv', header=None)).astype('float32')
  44. # 数据集分割
  45. data_len = len(data_x)
  46. t = np.linspace(0, data_len, data_len + 1)
  47. train_data_ratio = 0.8 # Choose 80% of the data for training
  48. train_data_len = int(data_len * train_data_ratio)
  49. train_x = data_x[5:train_data_len]
  50. train_y = data_y[5:train_data_len]
  51. t_for_training = t[5:train_data_len]
  52. test_x = data_x[train_data_len:]
  53. test_y = data_y[train_data_len:]
  54. t_for_testing = t[train_data_len:]
  55. # ----------------- train -------------------
  56. train_x_tensor = train_x.reshape(5, -1, 1)
  57. train_y_tensor = train_y.reshape(1, -1, 1)
  58. # transfer data to pytorch tensor
  59. train_x_tensor = torch.from_numpy(train_x_tensor)
  60. train_y_tensor = torch.from_numpy(train_y_tensor)
  61. gru_model = GruRNN(1, 30, output_size=1, num_layers=1) # 30 hidden units
  62. print('GRU model:', gru_model )
  63. print('model.parameters:', gru_model .parameters)
  64. print('train x tensor dimension:', Variable(train_x_tensor).size())
  65. criterion = nn.MSELoss()
  66. optimizer = torch.optim.Adam(gru_model .parameters(), lr=1e-2)
  67. prev_loss = 1000
  68. max_epochs = 3000
  69. train_x_tensor = train_x_tensor.to(device)
  70. for epoch in range(max_epochs):
  71. output = gru_model(train_x_tensor).to(device)
  72. loss = criterion(output, train_y_tensor)
  73. optimizer.zero_grad()
  74. loss.backward()
  75. optimizer.step()
  76. if loss < prev_loss:
  77. torch.save(gru_model.state_dict(), 'lstm_model.pt') # save model parameters to files
  78. prev_loss = loss
  79. if loss.item() < 1e-4:
  80. print('Epoch [{}/{}], Loss: {:.5f}'.format(epoch + 1, max_epochs, loss.item()))
  81. print("The loss value is reached")
  82. break
  83. elif (epoch + 1) % 100 == 0:
  84. print('Epoch: [{}/{}], Loss:{:.5f}'.format(epoch + 1, max_epochs, loss.item()))
  85. # prediction on training dataset
  86. pred_y_for_train = gru_model(train_x_tensor).to(device)
  87. pred_y_for_train = pred_y_for_train.view(-1, OUTPUT_FEATURES_NUM).data.numpy()
  88. # ----------------- test -------------------
  89. gru_model = gru_model .eval() # switch to testing model
  90. # prediction on test dataset
  91. test_x_tensor = test_x.reshape(1, -1, 5)
  92. test_x_tensor = torch.from_numpy(test_x_tensor) # 变为tensor
  93. test_x_tensor = test_x_tensor.to(device)
  94. pred_y_for_test = gru_model(test_x_tensor).to(device)
  95. pred_y_for_test = pred_y_for_test.view(-1, OUTPUT_FEATURES_NUM).data.numpy()
  96. loss = criterion(torch.from_numpy(pred_y_for_test), torch.from_numpy(test_y))
  97. print("test loss:", loss.item())
  98. # ----------------- plot -------------------
  99. plt.figure()
  100. plt.plot(t_for_training, train_y, 'b', label='y_trn')
  101. plt.plot(t_for_training, pred_y_for_train, 'y--', label='pre_trn')
  102. plt.plot(t_for_testing, test_y, 'k', label='y_tst')
  103. plt.plot(t_for_testing, pred_y_for_test, 'm--', label='pre_tst')
  104. plt.xlabel('t')
  105. plt.ylabel('Vce')
  106. plt.show()

结果如下图所示:

蓝线代表训练集真实值,黄线代表训练集预测值

黑线代表测试集真实值,紫线代表测试集预测值

test_loss=0.0038289446383714676

上篇文章有数据集和代码哦。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/101488
推荐阅读
相关标签
  

闽ICP备14008679号