这是indexloc提供的服务,不要输入任何密码
Skip to content

BruteForce layer cannot be built with tf.string identifiers #753

@comckay

Description

@comckay

Currently, on version 0.7.3 BruteForce within layers.factorized_top_k cannot be built with identifiers of type tf.string.

Simple example that works with identifiers as tf.int64

tfrs.layers.factorized_top_k.BruteForce(k=10).index(candidates=tf.random.normal((200,100)), identifiers=tf.constant([_ for _ in range(200)], dtype=tf.int64))

Simple example that does not work throwing an error when creating variables in the background

tfrs.layers.factorized_top_k.BruteForce(k=10).index(candidates=tf.random.normal((200,100)), identifiers=tf.constant([str(_) for _ in range(200)], dtype=tf.string))

Full trace here

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[25], line 1
----> 1 tfrs.layers.factorized_top_k.BruteForce(k=10).index(candidates=tf.random.normal((200,100)), identifiers=tf.constant([str(_) for _ in range(200)], dtype=tf.string))

File [~/dev/scratchpad/factorized_top_k_debugging/.venv/lib/python3.10/site-packages/tensorflow_recommenders/layers/factorized_top_k.py:550](http://localhost:8888/lab/tree/~/dev/scratchpad/factorized_top_k_debugging/.venv/lib/python3.10/site-packages/tensorflow_recommenders/layers/factorized_top_k.py#line=549), in BruteForce.index(self, candidates, identifiers)
    547 # We need any value that has the correct dtype.
    548 identifiers_initial_value = tf.zeros((), dtype=identifiers.dtype)
--> 550 self._identifiers = self.add_weight(
    551     name="identifiers",
    552     dtype=identifiers.dtype,
    553     shape=identifiers.shape,
    554     initializer=tf.keras.initializers.Constant(
    555         value=identifiers_initial_value),
    556     trainable=False)
    557 self._candidates = self.add_weight(
    558     name="candidates",
    559     dtype=candidates.dtype,
    560     shape=candidates.shape,
    561     initializer=tf.keras.initializers.Zeros(),
    562     trainable=False)
    564 self._identifiers.assign(identifiers)

File [~/dev/scratchpad/factorized_top_k_debugging/.venv/lib/python3.10/site-packages/keras/src/layers/layer.py:546](http://localhost:8888/lab/tree/~/dev/scratchpad/factorized_top_k_debugging/.venv/lib/python3.10/site-packages/keras/src/layers/layer.py#line=545), in Layer.add_weight(self, shape, initializer, dtype, trainable, autocast, regularizer, constraint, aggregation, name)
    544 initializer = initializers.get(initializer)
    545 with backend.name_scope(self.name, caller=self):
--> 546     variable = backend.Variable(
    547         initializer=initializer,
    548         shape=shape,
    549         dtype=dtype,
    550         trainable=trainable,
    551         autocast=autocast,
    552         aggregation=aggregation,
    553         name=name,
    554     )
    555 # Will be added to layer.losses
    556 variable.regularizer = regularizers.get(regularizer)

File [~/dev/scratchpad/factorized_top_k_debugging/.venv/lib/python3.10/site-packages/keras/src/backend/common/variables.py:186](http://localhost:8888/lab/tree/~/dev/scratchpad/factorized_top_k_debugging/.venv/lib/python3.10/site-packages/keras/src/backend/common/variables.py#line=185), in Variable.__init__(self, initializer, shape, dtype, trainable, autocast, aggregation, name)
    184 if callable(initializer):
    185     self._shape = self._validate_shape(shape)
--> 186     self._initialize_with_initializer(initializer)
    187 else:
    188     self._initialize(initializer)

File [~/dev/scratchpad/factorized_top_k_debugging/.venv/lib/python3.10/site-packages/keras/src/backend/tensorflow/core.py:48](http://localhost:8888/lab/tree/~/dev/scratchpad/factorized_top_k_debugging/.venv/lib/python3.10/site-packages/keras/src/backend/tensorflow/core.py#line=47), in Variable._initialize_with_initializer(self, initializer)
     47 def _initialize_with_initializer(self, initializer):
---> 48     self._initialize(lambda: initializer(self._shape, dtype=self._dtype))

File [~/dev/scratchpad/factorized_top_k_debugging/.venv/lib/python3.10/site-packages/keras/src/backend/tensorflow/core.py:39](http://localhost:8888/lab/tree/~/dev/scratchpad/factorized_top_k_debugging/.venv/lib/python3.10/site-packages/keras/src/backend/tensorflow/core.py#line=38), in Variable._initialize(self, value)
     38 def _initialize(self, value):
---> 39     self._value = tf.Variable(
     40         value,
     41         dtype=self._dtype,
     42         trainable=self.trainable,
     43         name=self.name,
     44         aggregation=self._map_aggregation(self.aggregation),
     45     )

File [~/dev/scratchpad/factorized_top_k_debugging/.venv/lib/python3.10/site-packages/tensorflow/python/util/traceback_utils.py:153](http://localhost:8888/lab/tree/~/dev/scratchpad/factorized_top_k_debugging/.venv/lib/python3.10/site-packages/tensorflow/python/util/traceback_utils.py#line=152), in filter_traceback.<locals>.error_handler(*args, **kwargs)
    151 except Exception as e:
    152   filtered_tb = _process_traceback_frames(e.__traceback__)
--> 153   raise e.with_traceback(filtered_tb) from None
    154 finally:
    155   del filtered_tb

File [~/dev/scratchpad/factorized_top_k_debugging/.venv/lib/python3.10/site-packages/keras/src/backend/tensorflow/core.py:48](http://localhost:8888/lab/tree/~/dev/scratchpad/factorized_top_k_debugging/.venv/lib/python3.10/site-packages/keras/src/backend/tensorflow/core.py#line=47), in Variable._initialize_with_initializer.<locals>.<lambda>()
     47 def _initialize_with_initializer(self, initializer):
---> 48     self._initialize(lambda: initializer(self._shape, dtype=self._dtype))

File [~/dev/scratchpad/factorized_top_k_debugging/.venv/lib/python3.10/site-packages/keras/src/initializers/constant_initializers.py:36](http://localhost:8888/lab/tree/~/dev/scratchpad/factorized_top_k_debugging/.venv/lib/python3.10/site-packages/keras/src/initializers/constant_initializers.py#line=35), in Constant.__call__(self, shape, dtype)
     34 def __call__(self, shape, dtype=None):
     35     dtype = standardize_dtype(dtype)
---> 36     return ops.cast(self.value, dtype=dtype) * ops.ones(
     37         shape=shape, dtype=dtype
     38     )

File [~/dev/scratchpad/factorized_top_k_debugging/.venv/lib/python3.10/site-packages/keras/src/ops/numpy.py:6589](http://localhost:8888/lab/tree/~/dev/scratchpad/factorized_top_k_debugging/.venv/lib/python3.10/site-packages/keras/src/ops/numpy.py#line=6588), in ones(shape, dtype)
   6578 @keras_export(["keras.ops.ones", "keras.ops.numpy.ones"])
   6579 def ones(shape, dtype=None):
   6580     """Return a new tensor of given shape and type, filled with ones.
   6581 
   6582     Args:
   (...)
   6587         Tensor of ones with the given shape and dtype.
   6588     """
-> 6589     return backend.numpy.ones(shape, dtype=dtype)

File [~/dev/scratchpad/factorized_top_k_debugging/.venv/lib/python3.10/site-packages/keras/src/backend/tensorflow/numpy.py:704](http://localhost:8888/lab/tree/~/dev/scratchpad/factorized_top_k_debugging/.venv/lib/python3.10/site-packages/keras/src/backend/tensorflow/numpy.py#line=703), in ones(shape, dtype)
    702 def ones(shape, dtype=None):
    703     dtype = dtype or config.floatx()
--> 704     return tf.ones(shape, dtype=dtype)

TypeError: Cannot convert 1 to EagerTensor of dtype string

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions