def reader(self, context=None, emb0=None):
if emb0 is None: # by default, can use the initial embedding
emb0 = self.emb0
if context is None: # empty contexts are not read
return emb0
context_inputs, context_ouputs = context
context_embs = tf.gather(emb0, context_inputs[:, :, 1])
preds = tf.reshape(tf.matmul(
tf.reshape(context_embs, (self.n_data * self.n_features, self.rank)),
tf.reshape(emb0[0, :], [self.rank, 1])),
(self.n_data, self.n_features))
update_strength = tf.tile(tf.reshape(loss_quadratic_grad(preds, context_ouputs),
(self.n_data, self.n_features, 1)), (1, 1, self.rank))
embs_after_reading = tf.tile(tf.reshape(emb0[0, :], (1, self.rank)), (self.n_data, 1)) \
- tf.reduce_sum(context_embs * update_strength, 1) * self.step_size
return embs_after_reading # size of the output: (n_data, rank)
评论列表
文章目录