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.023.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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 文巾解题 5. 最长回文子串
- 下一篇: 文巾解题 6. Z 字形变换