日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

pytorch笔记:实现简易LSTM

發布時間:2025/4/5 编程问答 13 豆豆
生活随笔 收集整理的這篇文章主要介紹了 pytorch笔记:实现简易LSTM 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1? lstm理論部分

詳細可見?機器學習筆記 RNN初探_劉文巾的博客-CSDN博客

2 torch.nn.LSTM

2.1 參數

–?input_size

–?hidden_size

–?num_layers

–?bias

–?batch_first?:設置了之后,輸出的維度為(batch, seq_len, hidden_size);否則為(seq_len,batch,hidden_size)

–?dropout

–?bidirectional

2.2 輸入

–?input?(seq_len, batch, input_size)

–?h_0?(num_layers * num_directions, batch, hidden_size)

–?c_0?(num_layers * num_directions, batch, hidden_size)

?

seq_len:每一次喂入的sequence有多長(一句話有幾個單詞)

input_size:每一個單詞的embedding dimension(一個word是由幾個維度的embedding組成的)

num_layers:有幾層RNN

num_directions:是單向RNN還是雙向RNN

hidden_size:隱藏層的維度(每個單詞在隱藏層中有多少維組成它的embedding)

2.3 輸出

?

–?output?(seq_len, batch, num_directions * hidden_size)
–?h_n?(num_layers * num_directions, batch, hidden_size)
–?c_n?(num_layers * num_directions, batch, hidden_size)

3 pytorch實現

我們還是用用pytorch實現簡易RNN_劉文巾的博客-CSDN博客?一樣的例子,記用sin預測cos,以示對照

3.1 導入庫 & 超參數設定

import torch import numpy as np import matplotlib.pyplot as pltTIME_STEP=10 INPUT_SIZE=1 HIDDEN_SIZE=32 LR=0.02

3.2 定義LSTM

class LSTM(torch.nn.Module):def __init__(self):super(LSTM,self).__init__()self.lstm=torch.nn.LSTM(input_size=INPUT_SIZE,hidden_size=HIDDEN_SIZE,num_layers=1,batch_first=True)''' batch_first的用法和RNN是一樣的 設置batch_first為True,那么輸入數據的維度為(batch_size,time_step,input_size) 如果不設置這個值,或者設置為False,那么輸入數據的維度為(time_step,batch_size,input_size)'''self.out=torch.nn.Linear(HIDDEN_SIZE,1)#__init__部分和RNN幾乎是一樣的def forward(self,x,h_n,h_c): #和RNN類似,上一個時間片的隱藏層狀態也要一直傳下去,只不過這邊我們要傳兩個值r_out,(h_n,h_c)=self.lstm(x,(h_n,h_c)) #r_out [Batch_size,Time_step(即前面的seq_len),hidden_size] #h_n h_c [Batch_size,num_layers*num_direction,hidden]r_out=r_out.view(-1,HIDDEN_SIZE)out=self.out(r_out)out=out.view(-1,TIME_STEP,1)return(out,(h_n,h_c))lstm=LSTM() print(lstm) ''' LSTM((lstm): LSTM(1, 32, batch_first=True)(out): Linear(in_features=32, out_features=1, bias=True) ) '''

3.3 設定優化函數和損失函數

optimizer=torch.optim.Adam(lstm.parameters(),lr=LR)loss_func=torch.nn.MSELoss()

3.4 訓練與驗證模型

h_n=torch.zeros((1,1,HIDDEN_SIZE)) h_c=torch.zeros((1,1,HIDDEN_SIZE)) for step in range(100):start=step*np.piend=(step+1)*np.pisteps=np.linspace(start,end,TIME_STEP,dtype=np.float32) #這里dtype這一部分一定要加,不然的話會報錯 #RuntimeError: expected scalar type Double but found Floatx_np=np.sin(steps).reshape(1,TIME_STEP,INPUT_SIZE)y_np=np.cos(steps).reshape(1,TIME_STEP,1)#和RNN一樣,目標:用sin預測cosx=torch.from_numpy(x_np)y=torch.from_numpy(y_np)prediction,(h_n,h_c)=lstm(x,h_n,h_c)h_n=h_n.datah_c=h_c.data #隱藏狀態向后傳loss=loss_func(prediction,y)optimizer.zero_grad()#清空上一步的參與更新參數值loss.backward()#誤差反向傳播,計算參數更新值optimizer.step()#將參數更新值施加到rnn的parameters上if(step % 10==0):plt.plot(steps,prediction.data.numpy().flatten(),'g*')plt.plot(steps,y_np.flatten(),'r-')plt.show()

4 實驗結果

4.1 一開始

最后

總結

以上是生活随笔為你收集整理的pytorch笔记:实现简易LSTM的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。