tensorflow / recommenders-addons

Additional utils and helpers to extend TensorFlow when build recommendation systems, contributed and maintained by SIG Recommenders.
Apache License 2.0
587 stars 132 forks source link

item embedding and user sequence item embedding can not use the same embedding variable #450

Open uzhy1987 opened 1 month ago

uzhy1987 commented 1 month ago

System information

Describe the bug

File "/opt/python/tensorflow/lib/python3.9/site-packages/keras/src/utils/traceback_utils.py", line 70, in error_handler raise e.with_traceback(filtered_tb) from None File "/opt/python/tensorflow/lib/python3.9/site-packages/tensorflow_recommenders_addons/dynamic_embedding/python/ops/dynamic_embedding_optimizer.py", line 739, in apply_gradients_strategy_v2_lagacy return self._distributed_apply(strategy, grads_and_vars, name, File "/opt/python/tensorflow/lib/python3.9/site-packages/tensorflow_recommenders_addons/dynamic_embedding/python/ops/dynamic_embedding_optimizer.py", line 204, in _distributed_apply update_op = distribution.extended.update(var, File "/opt/python/tensorflow/lib/python3.9/site-packages/tensorflow_recommenders_addons/dynamic_embedding/python/ops/dynamic_embedding_optimizer.py", line 172, in apply_grad_to_update_var _apply_op = self._resource_apply_sparse_duplicate_indices( tensorflow.python.framework.errors_impl.InvalidArgumentError: {{function_node wrappedResourceScatterAdddevice/job:localhost/replica:0/task:0/device:CPU:0}} indices[758] = 758 is not in [0, 758) [Op:ResourceScatterAdd] name:

A clear and concise description of what the bug is.

This problem occurs when the user behavior sequence features reuse embbeding from item, but not when used separately 当用户行为序列特征复用item的embbeding时会出现这个问题,分开使用时没有问题

Code to reproduce the issue

for f in sparse_cols:
    _input = sparse_inputs[f]
    embeddings[f] = de.keras.layers.Embedding(
        embed_dim, initializer=keras.initializers.RandomNormal(seed=25), name=f"emb_{f}",
        # init_capacity=lookups[f].vocabulary_size(), restrict_policy=de.FrequencyRestrictPolicy
    )
    input_embed[f] = Flatten()(embeddings[f](_input))

for f in varlens_inputs:
    _input = varlens_inputs[f] 
    mask = Masking(mask_value=0).compute_mask(_input)
    embedding = embeddings["bid"] if "_bids" in f else de.keras.layers.Embedding(
        embed_dim, initializer=keras.initializers.RandomNormal(seed=30), name=f"emb_{f}",
        # init_capacity=lookups[f].vocabulary_size(), restrict_policy=de.FrequencyRestrictPolicy
    )
    _embed = Reshape([-1, embed_dim])(embedding(_input))
    out_embed = MeanPoolLayer(axis=1)(_embed, mask)
    input_embed[f] = out_embed

Provide a reproducible test case that is the bare minimum necessary to generate the problem.

Other info / logs

Include any logs or source code that would be helpful to diagnose the problem. If including tracebacks, please include the full traceback. Large logs and files should be attached.