第21章推薦システム
21.4.2. Implementing the Model21.4.2 モデルを実装する
A typical autoencoder consists of an encoder and a decoder. The encoder projects the input to hidden representations and the decoder maps the hidden layer to the reconstruction layer. We follow this practice and create the encoder and decoder with fully connected layers. The activation of encoder is set to
sigmoid by default and no activation is applied for decoder. Dropout is included after the encoding transformation to reduce over-fitting. The gradients of unobserved inputs are masked out to ensure that only observed ratings contribute to the model learning process.英語のヒント
典型的なオートエンコーダは、エンコーダとデコーダからなる。エンコーダは入力を隠れた表現に射影し、デコーダは隠れ層を再構成層に写す。この慣例に従って、全結合層でエンコーダとデコーダを作る。エンコーダの活性化関数は既定で
sigmoidに設定し、デコーダには活性化関数を適用しない。過学習を減らすため、符号化の変換の後にドロップアウトを入れる。観測されていない入力の勾配はマスクされ、観測された評価だけがモデルの学習過程に寄与するようにする。class AutoRec(nn.Block):
def __init__(self, num_hidden, num_users, dropout=0.05):
super(AutoRec, self).__init__()
self.encoder = nn.Dense(num_hidden, activation='sigmoid',
use_bias=True)
self.decoder = nn.Dense(num_users, use_bias=True)
self.dropout = nn.Dropout(dropout)
def forward(self, input):
hidden = self.dropout(self.encoder(input))
pred = self.decoder(hidden)
if autograd.is_training(): # Mask the gradient during training
return pred * np.sign(input)
else:
return pred