NotImplementedError: Cannot convert a symbolic Tensor (lstm_2/strided_slice:0) to a numpy array. T

Viewed 46364

tensorflow version 2.3.1 numpy version 1.20

below the code

# define model
model = Sequential()
model.add(LSTM(50, activation='relu', input_shape=(n_steps, n_features)))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mse')

we got

NotImplementedError: Cannot convert a symbolic Tensor (lstm_2/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

it seems to me a crazy error!

8 Answers

I solved with numpy downgrade to 1.18.5

pip install -U numpy==1.18.5

If you are using anaconda:

conda install numpy=1.19

Similar issue, with

  • tensorflow 2.3.0
  • numpy 1.20.3

on Windows 7.

Solved by modifying tensorflow/python/framework/ops.py, replacing

  def __array__(self):
    raise NotImplementedError(

at line #845~846 with

  def __array__(self):
    raise TypeError(

.

Tensorflow 2.5 update:

tensorflow and tensorflow-gpu 2.5 packages still includes numpy-1.19.5 as a dependency.

The error referenced in this post will be reproduced if tensorflow 2.5 installation is mixed with numpy>1.19.5

tensorflow-2.5, numpy-1.19.5 are compatible with python-3.9

I had the same problem, solved it by downgrading python from 3.8 to 3.6

Same issue with tf 2.4.1, numpy 1.21, and python 3.9 .

Downgrading numpy to 1.19.2 with

conda install numpy==1.19.2 

solved my problem.

I faced this issue with M1 chip. Here is the how I fixed:

conda create create --name tf
conda activate tf
conda install numpy ~=1.18.5
pip install tensorflow-macos

and voila you are ready to go !

I had the same issue with tensorflow 2.5.0 and numpy 1.21.2. There were suggestions here to make changes in array_ops.py file but this didn't work for me. Another answer in the same page with following steps worked.

pip uninstall tensorflow
pip install tensorflow
pip uninstall numpy
pip install numpy

Basically these steps don't downgrade numpy but either upgrades or keeps it at the same level. Above steps upgraded tensorflow 2.7.0 and numpy 1.21.4 and my code ran without any issues.

Related