LSTM (Long Short Term Memory) networks
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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | # Define LSTM class Lstm(nn.Module): def __init__( self , input_size, hidden_size = 2 , output_size = 1 , num_layers = 1 ): super ().__init__() self .layer1 = nn.LSTM(input_size, hidden_size, num_layers) self .layer2 = nn.Linear(hidden_size, output_size) def forward( self , _x): x, _ = self .layer1(_x) s, b, h = x.shape x = x.view(s * b, h) x = self .layer2(x) x = x.view(s, b, - 1 ) return x # Generate data n = 100 t = np.linspace( 0 , 10.0 * np.pi,n) X = np.sin(t) X = X.astype( 'float32' ) # Set window of past points for LSTM model input_N = 5 output_N = 1 batch = 5 # Split into train/test data last = int (n / 2.5 ) Xtrain = X[: - last] Xtest = X[ - last - input_N:] # Store window number of points as a sequence xin = [] next_X = [] for i in range (input_N, len (Xtrain)): xin.append(Xtrain[i - input_N:i]) next_X.append(Xtrain[i]) # Reshape data to format for LSTM xin, next_X = np.array(xin), np.array(next_X) xin = xin.reshape( - 1 , 1 ,input_N) train_x = torch.from_numpy(xin) train_y = torch.from_numpy(next_X) train_x_tensor = train_x.reshape( - 1 ,batch,input_N) # set batch size to 5 train_y_tensor = train_y.reshape( - 1 ,batch,output_N) # set batch size to 5 model = Lstm(input_N, 5 , output_N, 1 ) # 5 hidden units loss_function = nn.MSELoss() optimizer = torch.optim.Adam(model.parameters(), lr = 1e - 2 ) max_epochs = 1000 for epoch in range (max_epochs): output = model(train_x_tensor) loss = loss_function(output, train_y_tensor) loss.backward() optimizer.step() optimizer.zero_grad() if loss.item() < 1e - 4 : print ( 'Epoch [{}/{}], Loss: {:.5f}' . format (epoch + 1 , max_epochs, loss.item())) print ( "The loss value is reached" ) break elif (epoch + 1 ) % 100 = = 0 : print ( 'Epoch: [{}/{}], Loss:{:.5f}' . format (epoch + 1 , max_epochs, loss.item())) # prediction on training dataset predictive_y_for_training = model(train_x_tensor) predictive_y_for_training = predictive_y_for_training.view( - 1 , 1 ).data.numpy() # ----------------- plot ------------------- plt.figure() plt.plot(train_y, label = 'Actual' ) plt.plot(predictive_y_for_training, 'm--' , label = 'Lstm' ) plt.title( 'Train' ) plt.legend(loc = 1 ) x_test_in = [] test_X = [] for i in range (input_N, len (Xtest)): x_test_in.append(Xtrain[i - input_N:i]) test_X.append(Xtrain[i]) x_test_in = np.array(x_test_in) test_x_tensor = x_test_in.reshape( - 1 , batch, input_N) # set batch size to 5 test_x_tensor = torch.from_numpy(test_x_tensor) predictive_y_for_testing = model(test_x_tensor) predictive_y_for_testing = predictive_y_for_testing.view( - 1 , 1 ).data.numpy() plt.figure() plt.plot(test_X,label = 'Actual' ) plt.plot(predictive_y_for_testing, 'r-.' ,label = 'Lstm' ) plt.title( 'Test' ) plt.legend(loc = 0 ) plt.show() |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人