第9章リカレントニューラルネットワーク
9.6.1. Defining the Model9.6.1 モデルを定義する
We define the following class using the RNN implemented by high-level APIs.
英語のヒント
高水準APIで実装されたRNNを使って、次のクラスを定義する。
class RNN(d2l.Module): #@save
"""The RNN model implemented with high-level APIs."""
def __init__(self, num_inputs, num_hiddens):
super().__init__()
self.save_hyperparameters()
self.rnn = nn.RNN(num_inputs, num_hiddens)
def forward(self, inputs, H=None):
return self.rnn(inputs, H)Inheriting from the
RNNLMScratch class in Section 9.5, the following RNNLM class defines a complete RNN-based language model. Note that we need to create a separate fully connected output layer.英語のヒント
9.5節の
RNNLMScratchクラスを継承して、次のRNNLMクラスは、RNNにもとづく完全な言語モデルを定義する。別個の全結合の出力層を作る必要があることに注意してほしい。class RNNLM(d2l.RNNLMScratch): #@save
"""The RNN-based language model implemented with high-level APIs."""
def init_params(self):
self.linear = nn.LazyLinear(self.vocab_size)
def output_layer(self, hiddens):
return self.linear(hiddens).swapaxes(0, 1)