ValueError:无法将 dtype 资源的张量转换为 NumPy 数组

Gab*_*abe 5 python keras tensorflow tensorflow2.0

我试图通过使用参数矩阵来隔离一些特定于用户的参数,其中每个数组将学习特定于该用户的参数。

我想使用用户 ID 索引矩阵,并将参数连接到其他功能。

最后,有一些完全连接的层以获得理想的结果。

但是,我在代码的最后一行不断收到此错误。


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-1-93de3591ccf0> in <module>
     20 # combined = tf.keras.layers.Concatenate(axis=-1)([le_param, le])
     21 
---> 22 net = tf.keras.layers.Dense(128)(combined)

~/anaconda3/envs/tam-env/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/base_layer.py in __call__(self, inputs, *args, **kwargs)
    793     # framework.
    794     if build_graph and base_layer_utils.needs_keras_history(inputs):
--> 795       base_layer_utils.create_keras_history(inputs)
    796 
    797     # Clear eager losses on top level model call.

~/anaconda3/envs/tam-env/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/base_layer_utils.py in create_keras_history(tensors)
    182     keras_tensors: The Tensors found that came from a Keras Layer.
    183   """
--> 184   _, created_layers = _create_keras_history_helper(tensors, set(), [])
    185   return created_layers
    186 

~/anaconda3/envs/tam-env/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/base_layer_utils.py in _create_keras_history_helper(tensors, processed_ops, created_layers)
    229               constants[i] = backend.function([], op_input)([])
    230       processed_ops, created_layers = _create_keras_history_helper(
--> 231           layer_inputs, processed_ops, created_layers)
    232       name = op.name
    233       node_def = op.node_def.SerializeToString()

~/anaconda3/envs/tam-env/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/base_layer_utils.py in _create_keras_history_helper(tensors, processed_ops, created_layers)
    229               constants[i] = backend.function([], op_input)([])
    230       processed_ops, created_layers = _create_keras_history_helper(
--> 231           layer_inputs, processed_ops, created_layers)
    232       name = op.name
    233       node_def = op.node_def.SerializeToString()

~/anaconda3/envs/tam-env/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/base_layer_utils.py in _create_keras_history_helper(tensors, processed_ops, created_layers)
    227           else:
    228             with ops.init_scope():
--> 229               constants[i] = backend.function([], op_input)([])
    230       processed_ops, created_layers = _create_keras_history_helper(
    231           layer_inputs, processed_ops, created_layers)

~/anaconda3/envs/tam-env/lib/python3.6/site-packages/tensorflow_core/python/keras/backend.py in __call__(self, inputs)
   3746     return nest.pack_sequence_as(
   3747         self._outputs_structure,
-> 3748         [x._numpy() for x in outputs],  # pylint: disable=protected-access
   3749         expand_composites=True)
   3750 

~/anaconda3/envs/tam-env/lib/python3.6/site-packages/tensorflow_core/python/keras/backend.py in <listcomp>(.0)
   3746     return nest.pack_sequence_as(
   3747         self._outputs_structure,
-> 3748         [x._numpy() for x in outputs],  # pylint: disable=protected-access
   3749         expand_composites=True)
   3750 

ValueError: Cannot convert a Tensor of dtype resource to a NumPy array.
Run Code Online (Sandbox Code Playgroud)

重现错误的代码:

import tensorflow as tf

num_uids = 50
input_uid = tf.keras.layers.Input(shape=(1,), dtype=tf.int32)
params = tf.Variable(tf.random.normal((num_uids, 9)), trainable=True)

param = tf.gather_nd(params, input_uid)

input_shared_features = tf.keras.layers.Input(shape=(128,), dtype=tf.float32)
combined = tf.concat([param, input_shared_features], axis=-1)

net = tf.keras.layers.Dense(128)(combined)
Run Code Online (Sandbox Code Playgroud)

我尝试了几件事:

  1. 我尝试使用 tf.keras.layers.Lambda 来封装 tf.gather_nd 和 tf.concat。
  2. 我尝试用 tf.keras.layers.Concatenate 替换 tf.concat。

奇怪的是,如果我指定项目数并用 tf.Variable 替换 Input,代码将按预期工作:

import tensorflow as tf

num_uids = 50
input_uid = tf.Variable(tf.ones((32, 1), dtype=tf.int32))
params = tf.Variable(tf.random.normal((num_uids, 9)), trainable=True)

param = tf.gather_nd(params, input_uid)

input_shared_features = tf.Variable(tf.ones((32, 128), dtype=tf.float32))
combined = tf.concat([param, input_shared_features], axis=-1)

net = tf.keras.layers.Dense(128)(combined)
Run Code Online (Sandbox Code Playgroud)

我在 Python 3.6.10 中使用 Tensorflow 2.1

Jit*_*ees 3

当我尝试tf.lookup.StaticHashTable在 TensorFlow 2.x 中使用 TensorFlow 表查找 ( )时,我遇到了类似的问题。我最终通过将其保留在Custom Keras Layer中来解决它。相同的解决方案似乎也适用于这个问题\xe2\x80\x94,至少直到问题中提到的版本为止。(我尝试使用 TensorFlow 2.0、2.1 和 2.2,它在所有这些版本中都有效。)

\n\n
import tensorflow as tf\n\nnum_uids = 50\ninput_uid = tf.keras.Input(shape=(1,), dtype=tf.int32)\ninput_shared_features = tf.keras.layers.Input(shape=(128,), dtype=tf.float32)\n\nclass CustomLayer(tf.keras.layers.Layer):\n    def __init__(self,num_uids):\n        super(CustomLayer, self).__init__(trainable=True,dtype=tf.int64)\n        self.num_uids = num_uids\n\n    def build(self,input_shape):\n        self.params = tf.Variable(tf.random.normal((num_uids, 9)), trainable=True)\n        self.built=True\n\n    def call(self, input_uid,input_shared_features):\n        param = tf.gather_nd(self.params, input_uid)\n        combined = tf.concat([param, input_shared_features], axis=-1)\n        return combined\n\n    def get_config(self):\n        config = super(CustomLayer, self).get_config()\n        config.update({\'num_uids\': self.num_uids})\n        return config\n\ncombined = CustomLayer(num_uids)(input_uid,input_shared_features)\nnet = tf.keras.layers.Dense(128)(combined)\nmodel = tf.keras.Model(inputs={\'input_uid\':input_uid,\'input_shared_features\':input_shared_features},outputs=net)\nmodel.summary()\n
Run Code Online (Sandbox Code Playgroud)\n\n

模型摘要如下:

\n\n
Model: "model"\n__________________________________________________________________________________________________\nLayer (type)                    Output Shape         Param #     Connected to                     \n==================================================================================================\ninput_1 (InputLayer)            [(None, 1)]          0                                            \n__________________________________________________________________________________________________\ninput_2 (InputLayer)            [(None, 128)]        0                                            \n__________________________________________________________________________________________________\ncustom_layer (CustomLayer)      (None, 137)          450         input_1[0][0]                    \n__________________________________________________________________________________________________\ndense (Dense)                   (None, 128)          17664       custom_layer[0][0]               \n==================================================================================================\nTotal params: 18,114\nTrainable params: 18,114\nNon-trainable params: 0\n
Run Code Online (Sandbox Code Playgroud)\n\n

有关更多信息,您可以参考tf.keras.layers.Layer文档

\n\n

如果您想参考查表问题和解决方案,以下是链接:

\n\n\n

  • 感谢@JeremyCaney 的鼓励话语和清理答案。我希望我将来能够为社区做出更多贡献:) (2认同)