【动手学深度学习pytorch】学习笔记 9.3. 深度循环神经网络
9.3. 深度循环神经网络 — 动手学深度学习 2.0.0-beta0 documentation (d2l.ai)
rnn.LSTM(num_hiddens, num_layers)
通过num_layers的值来设定隐藏层数
解释了前面的问题:【动手学深度学习pytorch】学习笔记 8.6. 循环神经网络的简洁实现 - HBU_DAVID - 博客园 (cnblogs.com)
import torch
from torch import nn
from d2l import torch as d2l
batch_size, num_steps = 32, 35
train_iter, vocab = d2l.load_data_time_machine(batch_size, num_steps)
vocab_size, num_hiddens, num_layers = len(vocab), 256, 2
num_inputs = vocab_size
device = d2l.try_gpu()
lstm_layer = nn.LSTM(num_inputs, num_hiddens, num_layers)
model = d2l.RNNModel(lstm_layer, len(vocab))
model = model.to(device)
num_epochs, lr = 500, 2
d2l.train_ch8(model, train_iter, vocab, lr, num_epochs, device)