jfkirk / tensorrec

A TensorFlow recommendation algorithm and framework in Python.
Apache License 2.0
1.27k stars 223 forks source link

Error on dimensionality #161

Open RiccardoDiGuida opened 3 years ago

RiccardoDiGuida commented 3 years ago

Hi, I am facing a problem with dimensionality when I build the model using DeepRepresentationGraph as per your example

Specifically

class DeepRepresentationGraph(AbstractKerasRepresentationGraph):

    # This method returns an ordered list of Keras layers connecting the user/item features to the user/item
    # representation. When TensorRec learns, the learning will happen in these layers.
    def create_layers(self, n_features, n_components):
        return [
            ks.layers.Dense(n_components * 16, activation='relu'), #rectified linear unit
            ks.layers.Dense(n_components * 8, activation='relu'), #you can try other activation layer too
            ks.layers.Dense(n_components * 2, activation='relu'), #most likely changes only benefit other ML like image recog.
            ks.layers.Dense(n_components, activation='tanh'),
        ]

n_sampled_items = int(item_features.shape[0] * .1)

model = TensorRec(n_components=n_components,
                      user_repr_graph=DeepRepresentationGraph(),
                      item_repr_graph=NormalizedLinearRepresentationGraph(),
                      loss_graph=WMRBLossGraph(),
                      biased=biased)

model.fit(train_interactions,
              user_features,
              item_features,
              epochs=epochs,
              verbose=False,
              alpha=alpha,
              n_sampled_items=n_sampled_items,
              learning_rate=learning_rate)

Please bear in mind that train_interactions, user_features and item_features are all scipy.sparse.coo_matrix

The error I get is the following

Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.3.3\plugins\python-ce\helpers\pydev\_pydevd_bundle\pydevd_exec2.py", line 3, in Exec
    exec(exp, global_vars, local_vars)
  File "<input>", line 1, in <module>
  File "C:\Users\xyzxyz\PycharmProjects\recommender_tf\model.py", line 47, in fit
    learning_rate=learning_rate)
  File "C:\Users\xyzxyz\anaconda3\envs\recommender_tf\lib\site-packages\tensorrec\tensorrec.py", line 538, in fit
    n_sampled_items=n_sampled_items)
  File "C:\Users\xyzxyz\anaconda3\envs\recommender_tf\lib\site-packages\tensorrec\tensorrec.py", line 623, in fit_partial
    session.run(self.tf_optimizer, feed_dict=feed_dict)
  File "C:\Users\xyzxyz\anaconda3\envs\recommender_tf\lib\site-packages\tensorflow_core\python\client\session.py", line 956, in run
    run_metadata_ptr)
  File "C:\Users\xyzxyz\anaconda3\envs\recommender_tf\lib\site-packages\tensorflow_core\python\client\session.py", line 1180, in _run
    feed_dict_tensor, options, run_metadata)
  File "C:\Users\xyzxyz\anaconda3\envs\recommender_tf\lib\site-packages\tensorflow_core\python\client\session.py", line 1359, in _do_run
    run_metadata)
  File "C:\Users\xyzxyz\anaconda3\envs\recommender_tf\lib\site-packages\tensorflow_core\python\client\session.py", line 1384, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: indices[1107898] = 25196 is not in [0, 25196)
     [[node GatherV2_6 (defined at C:\Users\xyzxyz\anaconda3\envs\recommender_tf\lib\site-packages\tensorflow_core\python\framework\ops.py:1748) ]]
Original stack trace for 'GatherV2_6':
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.3.3\plugins\python-ce\helpers\pydev\pydevd.py", line 2127, in <module>
    main()
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.3.3\plugins\python-ce\helpers\pydev\pydevd.py", line 2118, in main
    globals = debugger.run(setup['file'], None, None, is_module)
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.3.3\plugins\python-ce\helpers\pydev\pydevd.py", line 1427, in run
    return self._exec(is_module, entry_point_fn, module_name, file, globals, locals)
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.3.3\plugins\python-ce\helpers\pydev\pydevd.py", line 1434, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.3.3\plugins\python-ce\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/Users/xyzxyz/PycharmProjects/recommender_tf/main.py", line 62, in <module>
    main()
  File "C:/Users/xyzxyz/PycharmProjects/recommender_tf/main.py", line 57, in main
    preds = fit(item_f, user_f, train_interacions)
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.3.3\plugins\python-ce\helpers\pydev\pydevd.py", line 1099, in do_wait_suspend
    self._do_wait_suspend(thread, frame, event, arg, suspend_type, from_this_thread)
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.3.3\plugins\python-ce\helpers\pydev\pydevd.py", line 1113, in _do_wait_suspend
    self.process_internal_commands()
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.3.3\plugins\python-ce\helpers\pydev\pydevd.py", line 818, in process_internal_commands
    int_cmd.do_it(self)
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.3.3\plugins\python-ce\helpers\pydev\_pydevd_bundle\pydevd_comm.py", line 1647, in do_it
    result = pydevd_console_integration.console_exec(self.thread_id, self.frame_id, self.expression, dbg)
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.3.3\plugins\python-ce\helpers\pydev\_pydevd_bundle\pydevd_console_integration.py", line 222, in console_exec
    Exec(code, updated_globals, updated_globals)
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.3.3\plugins\python-ce\helpers\pydev\_pydevd_bundle\pydevd_exec2.py", line 3, in Exec
    exec(exp, global_vars, local_vars)
  File "<input>", line 1, in <module>
  File "C:\Users\xyzxyz\PycharmProjects\recommender_tf\model.py", line 47, in fit
    learning_rate=learning_rate)
  File "C:\Users\xyzxyz\anaconda3\envs\recommender_tf\lib\site-packages\tensorrec\tensorrec.py", line 538, in fit
    n_sampled_items=n_sampled_items)
  File "C:\Users\xyzxyz\anaconda3\envs\recommender_tf\lib\site-packages\tensorrec\tensorrec.py", line 606, in fit_partial
    self._build_tf_graph(n_user_features=n_user_features, n_item_features=n_item_features)
  File "C:\Users\xyzxyz\anaconda3\envs\recommender_tf\lib\site-packages\tensorrec\tensorrec.py", line 389, in _build_tf_graph
    tf_x_item=tf_x_item,
  File "C:\Users\xyzxyz\anaconda3\envs\recommender_tf\lib\site-packages\tensorrec\prediction_graphs.py", line 53, in connect_serial_prediction_graph
    gathered_user_reprs = tf.gather(tf_user_representation, tf_x_user)
  File "C:\Users\xyzxyz\anaconda3\envs\recommender_tf\lib\site-packages\tensorflow_core\python\util\dispatch.py", line 180, in wrapper
    return target(*args, **kwargs)
  File "C:\Users\xyzxyz\anaconda3\envs\recommender_tf\lib\site-packages\tensorflow_core\python\ops\array_ops.py", line 3956, in gather
    params, indices, axis, name=name)
  File "C:\Users\xyzxyz\anaconda3\envs\recommender_tf\lib\site-packages\tensorflow_core\python\ops\gen_array_ops.py", line 4082, in gather_v2
    batch_dims=batch_dims, name=name)
  File "C:\Users\xyzxyz\anaconda3\envs\recommender_tf\lib\site-packages\tensorflow_core\python\framework\op_def_library.py", line 794, in _apply_op_helper
    op_def=op_def)
  File "C:\Users\xyzxyz\anaconda3\envs\recommender_tf\lib\site-packages\tensorflow_core\python\util\deprecation.py", line 507, in new_func
    return func(*args, **kwargs)
  File "C:\Users\xyzxyz\anaconda3\envs\recommender_tf\lib\site-packages\tensorflow_core\python\framework\ops.py", line 3357, in create_op
    attrs, op_def, compute_device)
  File "C:\Users\xyzxyz\anaconda3\envs\recommender_tf\lib\site-packages\tensorflow_core\python\framework\ops.py", line 3426, in _create_op_internal
    op_def=op_def)
  File "C:\Users\xyzxyz\anaconda3\envs\recommender_tf\lib\site-packages\tensorflow_core\python\framework\ops.py", line 1748, in __init__
    self._traceback = tf_stack.extract_stack()

I am using TF 1.15 and tensorrec 0.26.2

ljluestc commented 3 months 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
)