第10章現代のリカレントニューラルネットワーク
10.3.2. Concise Implementation10.3.2 簡潔な実装
Fortunately many of the logistical details required to implement multiple layers of an RNN are readily available in high-level APIs. Our concise implementation will use such built-in functionalities. The code generalizes the one we used previously in Section 10.2, letting us specify the number of layers explicitly rather than picking the default of only one layer.
英語のヒント
幸い、RNNの複数の層を実装するのに必要な細かい段取りの多くは、高水準APIですぐに使える。簡潔な実装では、そうした組み込みの機能を使う。このコードは、前に10.2節で使ったものを一般化し、既定の1層だけを選ぶのではなく、層の数をはっきり指定できるようにする。
class GRU(d2l.RNN): #@save
"""The multilayer GRU model."""
def __init__(self, num_inputs, num_hiddens, num_layers, dropout=0):
d2l.Module.__init__(self)
self.save_hyperparameters()
self.rnn = nn.GRU(num_inputs, num_hiddens, num_layers,
dropout=dropout)The architectural decisions such as choosing hyperparameters are very similar to those of Section 10.2. We pick the same number of inputs and outputs as we have distinct tokens, i.e.,
vocab_size. The number of hidden units is still 32. The only difference is that we now select a nontrivial number of hidden layers by specifying the value of num_layers.英語のヒント
ハイパーパラメータの選び方のような構造上の判断は、10.2節のものととてもよく似ている。入力と出力の数は、異なるトークンの数、つまり
vocab_sizeと同じにする。隠れユニットの数はやはり32である。唯一の違いは、num_layersの値を指定して、自明でない数の隠れ層を選ぶことである。gru = GRU(num_inputs=len(data.vocab), num_hiddens=32, num_layers=2)
model = d2l.RNNLM(gru, vocab_size=len(data.vocab), lr=2)
trainer.fit(model, data)model.predict('it has', 20, data.vocab, d2l.try_gpu())'it has for and the time th'