tensorflow: Cannot convert a symbolic Tensor (gru/strided_slice:0) to a numpy array.

System information

  • OS Platform and Distribution: Arch Linux
  • TensorFlow installed from package: python-tensorflow
  • TensorFlow version: unknown 2.4.1
  • Python version: 3.9.1
  • CUDA/cuDNN version: No GPU

Standalone code to reproduce the issue

from tensorflow.keras.layers import Embedding, Input, GRU

x = Input(shape=(None,))
x = Embedding(input_dim=50, output_dim=16, mask_zero=True)(x)
x = GRU(units=256)(x)

Other info / logs

Traceback (most recent call last):
  File "/home/jnphilipp/Nextcloud/Code/jnphilipp/deep_learning/test3.py", line 5, in <module>
    x = GRU(units=256)(x)
  File "/usr/lib/python3.9/site-packages/tensorflow/python/keras/layers/recurrent.py", line 660, in __call__
    return super(RNN, self).__call__(inputs, **kwargs)
  File "/usr/lib/python3.9/site-packages/tensorflow/python/keras/engine/base_layer.py", line 951, in __call__
    return self._functional_construction_call(inputs, args, kwargs,
  File "/usr/lib/python3.9/site-packages/tensorflow/python/keras/engine/base_layer.py", line 1090, in _functional_construction_call
    outputs = self._keras_tensor_symbolic_call(
  File "/usr/lib/python3.9/site-packages/tensorflow/python/keras/engine/base_layer.py", line 822, in _keras_tensor_symbolic_call
    return self._infer_output_signature(inputs, args, kwargs, input_masks)
  File "/usr/lib/python3.9/site-packages/tensorflow/python/keras/engine/base_layer.py", line 863, in _infer_output_signature
    outputs = call_fn(inputs, *args, **kwargs)
  File "/usr/lib/python3.9/site-packages/tensorflow/python/keras/layers/recurrent_v2.py", line 439, in call
    inputs, initial_state, _ = self._process_inputs(inputs, initial_state, None)
  File "/usr/lib/python3.9/site-packages/tensorflow/python/keras/layers/recurrent.py", line 859, in _process_inputs
    initial_state = self.get_initial_state(inputs)
  File "/usr/lib/python3.9/site-packages/tensorflow/python/keras/layers/recurrent.py", line 642, in get_initial_state
    init_state = get_initial_state_fn(
  File "/usr/lib/python3.9/site-packages/tensorflow/python/keras/layers/recurrent.py", line 1948, in get_initial_state
    return _generate_zero_filled_state_for_cell(self, inputs, batch_size, dtype)
  File "/usr/lib/python3.9/site-packages/tensorflow/python/keras/layers/recurrent.py", line 2987, in _generate_zero_filled_state_for_cell
    return _generate_zero_filled_state(batch_size, cell.state_size, dtype)
  File "/usr/lib/python3.9/site-packages/tensorflow/python/keras/layers/recurrent.py", line 3005, in _generate_zero_filled_state
    return create_zeros(state_size)
  File "/usr/lib/python3.9/site-packages/tensorflow/python/keras/layers/recurrent.py", line 3000, in create_zeros
    return array_ops.zeros(init_state_size, dtype=dtype)
  File "/usr/lib/python3.9/site-packages/tensorflow/python/util/dispatch.py", line 201, in wrapper
    return target(*args, **kwargs)
  File "/usr/lib/python3.9/site-packages/tensorflow/python/ops/array_ops.py", line 2819, in wrapped
    tensor = fun(*args, **kwargs)
  File "/usr/lib/python3.9/site-packages/tensorflow/python/ops/array_ops.py", line 2868, in zeros
    output = _constant_if_small(zero, shape, dtype, name)
  File "/usr/lib/python3.9/site-packages/tensorflow/python/ops/array_ops.py", line 2804, in _constant_if_small
    if np.prod(shape) < 1000:
  File "<__array_function__ internals>", line 5, in prod
  File "/usr/lib/python3.9/site-packages/numpy/core/fromnumeric.py", line 3030, in prod
    return _wrapreduction(a, np.multiply, 'prod', axis, dtype, out,
  File "/usr/lib/python3.9/site-packages/numpy/core/fromnumeric.py", line 87, in _wrapreduction
    return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
  File "/usr/lib/python3.9/site-packages/tensorflow/python/framework/ops.py", line 852, in __array__
    raise NotImplementedError(
NotImplementedError: Cannot convert a symbolic Tensor (gru/strided_slice:0) to a numpy array. This error may indicate that you're trying to pass a Tensor to a NumPy call, which is not supported

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 10
  • Comments: 19 (7 by maintainers)

Commits related to this issue

Most upvoted comments

@jvishnuvardhan yes numpy.__version__ 1.19.3 this problem arises with numpy version 1.20 and 1.21

I met a similar problem when I run the BERT model (repo: https://github.com/google-research/bert). I have downgrade Numpy from 1.20 to 1.19.2 and the problem solved. Thanks!

I am closing this issue as this is more related to numpy version as mentioned above and not related core Tensorflow. Loading correct numpy version resolves the issue. Thanks!

[Solved]

I have the same error when training the data, even also try to downgrade the NumPy version, but it not be fixed. Finally, I solved that issue. Try the below command:

!pip install numpy==1.17.4 !pip install pycocotools==2.0.0

@jvishnuvardhan yes numpy.__version__ 1.19.3 this problem arises with numpy version 1.20 and 1.21

Thanks! The same issue just started happening on my setup, and downgrading to 1.19.4 solved.