Open RiccardoDiGuida opened 3 years ago
import tensorflow as tf
import numpy as np
from tensorrec import TensorRec
from tensorrec.representation_graphs import AbstractKerasRepresentationGraph
from tensorrec.loss_graphs import WMRBLossGraph
from tensorrec.prediction_graphs import NormalizedLinearRepresentationGraph
from scipy import sparse
# Define custom representation graph
class DeepRepresentationGraph(AbstractKerasRepresentationGraph):
def create_layers(self, n_features, n_components):
return [
tf.keras.layers.Dense(n_components * 16, activation='relu'),
tf.keras.layers.Dense(n_components * 8, activation='relu'),
tf.keras.layers.Dense(n_components * 2, activation='relu'),
tf.keras.layers.Dense(n_components, activation='tanh'),
]
# Sample data
n_users = 1000
n_items = 150
n_components = 10
train_interactions = sparse.random(n_users, n_items, density=0.1, format='coo')
# User and item features
user_features = sparse.random(n_users, n_components, density=0.1, format='csr')
item_features = sparse.random(n_items, n_components, density=0.1, format='csr')
# Define model parameters
n_sampled_items = int(item_features.shape[0] * .1)
biased = False
epochs = 10
alpha = 0
learning_rate = 0.01
# Build model
model = TensorRec(
n_components=n_components,
user_repr_graph=DeepRepresentationGraph(),
item_repr_graph=NormalizedLinearRepresentationGraph(),
loss_graph=WMRBLossGraph(),
biased=biased
)
# Fit model
model.fit(
train_interactions,
user_features,
item_features,
epochs=epochs,
verbose=False,
alpha=alpha,
n_sampled_items=n_sampled_items,
learning_rate=learning_rate
)
Hi, I am facing a problem with dimensionality when I build the model using
DeepRepresentationGraph
as per your exampleSpecifically
Please bear in mind that
train_interactions
,user_features
anditem_features
are allscipy.sparse.coo_matrix
The error I get is the following
I am using TF 1.15 and tensorrec 0.26.2