Source code for elliot.recommender.neural.GeneralizedMF.generalized_matrix_factorization_model

"""
Module description:

"""

__version__ = '0.3.1'
__author__ = 'Vito Walter Anelli, Claudio Pomo, Daniele Malitesta'
__email__ = 'vitowalter.anelli@poliba.it, claudio.pomo@poliba.it, daniele.malitesta@poliba.it'

import os
import numpy as np
import tensorflow as tf
from tensorflow import keras

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'


[docs]class GeneralizedMatrixFactorizationModel(keras.Model): def __init__(self, num_users, num_items, embed_mf_size, is_edge_weight_train, learning_rate=0.01, random_seed=42, name="GeneralizedMatrixFactorizationModel", **kwargs): super().__init__(name=name, **kwargs) tf.random.set_seed(random_seed) self.num_users = num_users self.num_items = num_items self.embed_mf_size = embed_mf_size self.is_edge_weight_train = is_edge_weight_train self.initializer = tf.initializers.GlorotUniform() self.user_mf_embedding = keras.layers.Embedding(input_dim=self.num_users, output_dim=self.embed_mf_size, embeddings_initializer=self.initializer, name='U_GMF', dtype=tf.float32) self.item_mf_embedding = keras.layers.Embedding(input_dim=self.num_items, output_dim=self.embed_mf_size, embeddings_initializer=self.initializer, name='I_GMF', dtype=tf.float32) self.user_mf_embedding(0) self.item_mf_embedding(0) if self.is_edge_weight_train: self.activation = keras.activations.sigmoid self.edge_weight = tf.Variable(self.initializer([self.embed_mf_size, 1]), name='h') self.loss = keras.losses.BinaryCrossentropy() else: self.activation = keras.activations.linear self.edge_weight = tf.Variable(initial_value=1, shape=[self.embed_mf_size, 1], name='h') self.loss = keras.losses.MeanSquaredError() self.optimizer = tf.optimizers.Adam(learning_rate)
[docs] @tf.function def call(self, inputs, training=None, mask=None): user, item = inputs user_mf_e = self.user_mf_embedding(user) item_mf_e = self.item_mf_embedding(item) mf_output = user_mf_e * item_mf_e output = self.activation(tf.matmul(mf_output, self.edge_weight)) return tf.squeeze(output)
[docs] @tf.function def train_step(self, batch): user, pos, label = batch with tf.GradientTape() as tape: # Clean Inference output = self(inputs=(user, pos), training=True) loss = self.loss(label, output) grads = tape.gradient(loss, self.trainable_weights) self.optimizer.apply_gradients(zip(grads, self.trainable_weights)) return loss
[docs] @tf.function def get_recs(self, inputs, training=False, **kwargs): """ Get full predictions on the whole users/items matrix. Returns: The matrix of predicted values. """ output = self(inputs, training=training) return tf.squeeze(output)
[docs] @tf.function def get_top_k(self, preds, train_mask, k=100): return tf.nn.top_k(tf.where(train_mask, preds, -np.inf), k=k, sorted=True)