26

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!

venergiac
  • 7,469
  • 2
  • 48
  • 70

8 Answers8

47

I solved with numpy downgrade to 1.18.5

pip install -U numpy==1.18.5
venergiac
  • 7,469
  • 2
  • 48
  • 70
  • 4
    I was just looking at the 1.20 release notes. There are a number of low level changes (which I didn't fully understand), so I'm not surprised that there compatibility issues with `tensorflow`. I don't know who has to fix what. – hpaulj Feb 15 '21 at 17:53
  • 1
    no idea waiting for a better support ... also I'm curious why nobody found a similar issue – venergiac Feb 15 '21 at 18:01
  • 2
    I just did as well :) – Nikkolasg Mar 06 '21 at 21:52
  • 12
    Numpy version 1.19.5 resolves the issues as well. – Strozzascotte Mar 14 '21 at 22:24
  • 3
    `numpy 1.18.5` is not compatible with `python 3.9`, so I had to downgrade to `python 3.8` first before installing `numpy 1.18.5`. (`numpy==1.18.5 -> python[version='>=3.6,<3.7.0a0|>=3.7,<3.8.0a0|>=3.8,<3.9.0a0']`) – Vishal May 03 '21 at 21:21
  • I had the same issue with "numpy-1.20.3" and downgrading to "numpy-1.18.5" as mentioned in this answer solved the issue. – Prabath Jun 21 '21 at 07:55
  • What happen when another library requires ```numpy 1.20.0```? It would give an error. This a good solution but you need to be careful when you are using different versions. – Luis Miguel Aug 06 '21 at 19:17
  • I just had the same error. Downgrading to 1.19 has solved my problem. – Marlon Teixeira Dec 15 '21 at 18:41
15

If you are using anaconda:

conda install numpy=1.19

Chikwado
  • 451
  • 6
  • 7
14

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(

.

Chris Fu
  • 141
  • 1
  • 3
5

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

panquela
  • 51
  • 1
  • 2
1

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

nasalp
  • 11
  • 1
1

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.

navy
  • 11
  • 2
0

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 !

0

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.

mockash
  • 1,204
  • 5
  • 14
  • 26