tensorflow: ValueError: Cannot convert a Tensor of dtype resource to a NumPy array
System information
- Have I written custom code (as opposed to using a stock example script provided in TensorFlow): Yes
- OS Platform and Distribution (e.g., Linux Ubuntu 16.04): N/A, as it can be reproduced in Google Colab
- Mobile device (e.g. iPhone 8, Pixel 2, Samsung Galaxy) if the issue happens on mobile device: N/A
- TensorFlow installed from (source or binary): - TensorFlow version (use command below): 2.1
- Python version: - Bazel version (if compiling from source): N/A
- GCC/Compiler version (if compiling from source): N/A
- CUDA/cuDNN version: - GPU model and memory: N/A
Describe the current behavior: It is resulting in Error, InvalidArgumentError: Cannot convert a Tensor of dtype resource to a NumPy array., while running the First Code but is working fine when tf.keras.Input is replaced with tf.Variable in the Second Code.
Describe the expected behavior: Code should work fine with tf.keras.Input as well
Standalone code to reproduce the issue
**Code with Error:**
import tensorflow as tf
num_uids = 50 #input_uid = tf.keras.layers.Input(shape=(1,), dtype=tf.int32, batch_size = 32) 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, batch_size = 32) 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)
**Working Code:**
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)
Please find the Github Gist.
There is a Stack Overflow Question also, associated with this issue.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 19 (4 by maintainers)
Same problem here. Tensorflow version
'2.2.0-rc3'Code to reproduce the issue:
The issue:
Same issue, any update?
As obtuse as this might be, this worked for me too!
I had the same issue, and was able to work around it like this:
Rather unintuitively, the critical part is actually that the function arguments (
paramsandindices) are swapped in the custom layer. If you don’t do that, you still get the same error. I didn’t dig deeper into why this is the case.I have posted a solution in the stackoverflow question associated with this issue - https://stackoverflow.com/a/62161525/2352424 Hope that helps
I also had the same issue. Are there any solutions?