Python深度學習 迴圈神經網路

2021-10-02 23:16:37 字數 3762 閱讀 7539

rnn存在的問題:梯度較容易出現衰減或**(bptt)

⻔控迴圈神經⽹絡:捕捉時間序列中時間步距離較⼤的依賴關係

重置⻔有助於捕捉時間序列⾥短期的依賴關係;

• 更新⻔有助於捕捉時間序列⾥⻓期的依賴關係。

長短期記憶long short-term memory :

遺忘門:控制上一時間步的記憶細胞 輸入門:控制當前時間步的輸入

輸出門:控制從記憶細胞到隱藏狀態

記憶細胞:⼀種特殊的隱藏狀態的資訊的流動

初始化引數:

num_inputs, num_hiddens, num_outputs = vocab_size,

256, vocab_size

print

('will use'

, device)

defget_params()

:def

_one

(shape)

: ts = torch.tensor(np.random.normal(0,

0.01

, size=shape)

, device=device, dtype=torch.float32)

return torch.nn.parameter(ts, requires_grad=

true

)def

_three()

:return

(_one(

(num_inputs, num_hiddens)),

_one(

(num_hiddens, num_hiddens)),

torch.nn.parameter(torch.zeros(num_hiddens, device=device, dtype=torch.float32)

, requires_grad=

true))

w_xi, w_hi, b_i = _three(

)# 輸入門引數

w_xf, w_hf, b_f = _three(

)# 遺忘門引數

w_xo, w_ho, b_o = _three(

)# 輸出門引數

w_xc, w_hc, b_c = _three(

)# 候選記憶細胞引數

# 輸出層引數

w_hq = _one(

(num_hiddens, num_outputs)

) b_q = torch.nn.parameter(torch.zeros(num_outputs, device=device, dtype=torch.float32)

, requires_grad=

true

)return nn.parameterlist(

[w_xi, w_hi, b_i, w_xf, w_hf, b_f, w_xo, w_ho, b_o, w_xc, w_hc, b_c, w_hq, b_q]

)def

init_lstm_state

(batch_size, num_hiddens, device)

:return

(torch.zeros(

(batch_size, num_hiddens)

, device=device)

, torch.zeros(

(batch_size, num_hiddens)

, device=device)

)

out:will use cpu

lstm模型:

def

lstm

(inputs, state, params)

:[w_xi, w_hi, b_i, w_xf, w_hf, b_f, w_xo, w_ho, b_o, w_xc, w_hc, b_c, w_hq, b_q]

= params

(h, c)

= state

outputs =

for x in inputs:

i = torch.sigmoid(torch.matmul(x, w_xi)

+ torch.matmul(h, w_hi)

+ b_i)

f = torch.sigmoid(torch.matmul(x, w_xf)

+ torch.matmul(h, w_hf)

+ b_f)

o = torch.sigmoid(torch.matmul(x, w_xo)

+ torch.matmul(h, w_ho)

+ b_o)

c_tilda = torch.tanh(torch.matmul(x, w_xc)

+ torch.matmul(h, w_hc)

+ b_c)

c = f * c + i * c_tilda

h = o * c.tanh(

) y = torch.matmul(h, w_hq)

+ b_q

return outputs,

(h, c)

訓練模型:

num_epochs, num_steps, batch_size, lr, clipping_theta =

160,35,

32,1e2,1e-

2pred_period, pred_len, prefixes =40,

50,['分開'

,'不分開'

]d2l.train_and_predict_rnn(lstm, get_params, init_lstm_state, num_hiddens,

vocab_size, device, corpus_indices, idx_to_char,

char_to_idx,

false

, num_epochs, num_steps, lr,

clipping_theta, batch_size, pred_period, pred_len,

prefixes)

out:

epoch 40, perplexity 211.457328, time 1.51 sec

簡潔實現:

num_hiddens=

256num_epochs, num_steps, batch_size, lr, clipping_theta =

160,35,

32,1e2,1e-

2pred_period, pred_len, prefixes =40,

50,['分開'

,'不分開'

]lr =1e-

2# 注意調整學習率

lstm_layer = nn.lstm(input_size=vocab_size, hidden_size=num_hiddens)

model = d2l.rnnmodel(lstm_layer, vocab_size)

d2l.train_and_predict_rnn_pytorch(model, num_hiddens, vocab_size, device,

corpus_indices, idx_to_char, char_to_idx,

num_epochs, num_steps, lr, clipping_theta,

batch_size, pred_period, pred_len, prefixes)

out:

epoch 40, perplexity 1.019881, time 1.04 sec

TensorFlow深度學習筆記 迴圈神經網路實踐

歡迎star,有問題可以到issue區討論 官方教程位址 text8中只包含27種字元 小寫的從a到z,以及空格符。如果把它打出來,讀起來就像是去掉了所有標點的wikipedia。用zipfile讀取zip內容為字串,並拆分成單詞list 用connections模組統計單詞數量並找出最常見的單詞 ...

TensorFlow深度學習筆記 迴圈神經網路實踐

歡迎star,有問題可以到issue區討論 官方教程位址 text8中只包含27種字元 小寫的從a到z,以及空格符。如果把它打出來,讀起來就像是去掉了所有標點的wikipedia。用zipfile讀取zip內容為字串,並拆分成單詞list 用connections模組統計單詞數量並找出最常見的單詞 ...

(pytorch 深度學習)深度迴圈神經網路

迴圈神經網路只有乙個單向的隱藏層,在深度學習應用裡,我們通常會用到含有多個隱藏層的迴圈神經網路,也稱作深度迴圈神經網路。下圖演示了乙個有l ll個隱藏層的深度迴圈神經網路,每個隱藏狀態不斷傳遞至當前層的下一時間步和當前時間步的下一層。具體來說 第1隱藏層的隱藏狀態和之前的計算一樣 h t 1 xtw...