解决NotImplementedError: Cannot convert a symbolic Tensor to a numpy array.问题

问题描述:
PyCharm中通过Tensorflow,采用tf.keras方式搭建神经网络模型时出现报错:NotImplementedError: Cannot convert a symbolic Tensor (sequential/simple_rnn/rided_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.

现有配置:
PyCharm 2022.2
在Anaconda搭建的python环境(TF2.3):
python 3.8
tensorflow 2.3
numpy 1.23.1
pandas 1.5.0

问题原因及解决方法:
numpy版本过高,应降低其版本。

#打开Anaconda Prompt,进入TF2.3环境。
(base) C:\Users\Queena>conda activate TF2.3

#查看当前numpy版本
(TF2.3) C:\Users\Queena>pip show numpy
Name: numpy
Version: 1.23.1

#卸载现有1.23.1版本的numpy
(TF2.3) C:\Users\Queena>pip uninstall numpy
Successfully uninstalled numpy-1.23.1

#查看可以安装的numpy版本,在1.19版本附近进行尝试,寻找可以解决问题的版本。
(TF2.3) C:\Users\Queena>conda search --full --name numpy

#我选择numpy 1.19.2版本即可正常工作,遂安装1.19.2版本numpy.
(TF2.3) C:\Users\Queena>pip install numpy==1.19.2

#若在安装numpy 1.19.2过程中,出现报错PackagesNotFoundError: The following packages are not available from current channels.则需要将conda-forge添加到搜索路径后,再进行安装。
(TF2.3) C:\Users\Queena>conda config --append channels conda-forge
(TF2.3) C:\Users\Queena>pip install numpy==1.19.2

#安装完成再次查看numpy版本
(TF2.3) C:\Users\Queena>pip show numpy
Name: numpy
Version: 1.19.2

此时运行程序,如题的报错不再出现,但存在pandas与numpy版本不匹配的问题,即pandas 1.5.0要求numpy >1.20。这时需要降低pandas版本,如下:

#查看现有pandas版本
(TF2.3) C:\Users\Queena>pip show pandas
Name: pandas
Version: 1.5.0

#卸载当前版本的pandas
(TF2.3) C:\Users\Queena>pip uninstall pandas
Successfully uninstalled pandas-1.5.0

#查看可以安装的pandas版本。
(TF2.3) C:\Users\Queena>conda search --full --name pandas

#选择pandas 1.1.5进行安装
(TF2.3) C:\Users\Queena>pip install pandas==1.1.5

#安装完成再次查看pandas版本
(TF2.3) C:\Users\Queena>pip show pandas
Name: pandas
Version: 1.1.5
经过上述操作后的配置:
PyCharm 2022.2
在Anaconda搭建的python环境(TF2.3):
python 3.8
tensorflow 2.3
numpy 1.19.2
pandas 1.1.5

注:若日后需要升级numpy版本,可采用:pip install --upgrade numpy

至此问题得以解决!

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值