【问题标题】:NotImplementedError: Cannot convert a symbolic Tensor to a numpy arrayNotImplementedError:无法将符号张量转换为 numpy 数组
【发布时间】:2021-02-10 17:05:34
【问题描述】:

下面的代码去年可以工作,但 keras/tensorflow/numpy 中的更新破坏了它。它现在输出下面的异常。有谁知道如何让它再次工作?

我正在使用:

  • 张量流 2.4.1
  • Keras 2.4.3
  • Numpy 1.20.1
  • Python 3.9.1
import numpy as np
from keras.layers import LSTM, Embedding, Input, Bidirectional

dim = 30
max_seq_length = 40
vecs = np.random.rand(45,dim)

input_layer = Input(shape=(max_seq_length,))
embedding_layer = Embedding(len(vecs), dim, weights=[vecs], input_length=max_seq_length, trainable=False, name="layerA")(input_layer)
lstm_nobi = LSTM(max_seq_length, return_sequences=True, activation="linear", name="layerB")
lstm = Bidirectional(lstm_nobi, name="layerC")(embedding_layer)

上面脚本的完整输出:https://pastebin.com/DsQNWVwz

缩短输出:

2021-02-10 17:51:13.037468: I tensorflow/compiler/jit/xla_cpu_device.cc:41] Not creating XLA devices, tf_xla_enable_xla_devices not set
2021-02-10 17:51:13.037899: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  SSE3 SSE4.1 SSE4.2 AVX AVX2 FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2021-02-10 17:51:13.038418: I tensorflow/core/common_runtime/process_util.cc:146] Creating new thread pool with default inter op setting: 2. Tune using inter_op_parallelism_threads for best performance.
Traceback (most recent call last):
  File "/run/media/volker/DATA/configruns/load/./test.py", line 13, in <module>
    lstm = Bidirectional(lstm_nobi, name="layerC")(embedding_layer)
  ... omitted, see pastebin ...
  File "/usr/lib/python3.9/site-packages/tensorflow/python/framework/ops.py", line 852, in __array__
    raise NotImplementedError(
NotImplementedError: Cannot convert a symbolic Tensor (layerC/forward_layerB/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

【问题讨论】:

  • 仅供参考,我使用 anaconda 运行您的代码没有任何问题。我的 tf 版本是 2.3.0,python 3.7.0,numpy 1.19.2。我希望这会有所帮助

标签: numpy tensorflow


【解决方案1】:

即使使用 Python 3.9,安装 numpy 1.19.5 也适用于我

pip install -U numpy==1.19.5

我的环境是 Windows,由于我没有安装 Visual C++ 编译器,所以我使用第三方 whl 文件安装

pip install -U https://mirrors.aliyun.com/pypi/packages/bc/40/d6f7ba9ce5406b578e538325828ea43849a3dfd8db63d1147a257d19c8d1/numpy-1.19.5-cp39-cp39-win_amd64.whl#sha256=0eef32ca3132a48e43f6a0f5a82cb508f22ce5a3d6f67a8329c81c8e226d3f6e

【讨论】:

  • 我的解决方案:使用Python 3.8,因为Tensorflow不支持Python 3.9。
  • 这个解决方案在 Linux、Python 3.8 和 Tensorflow 2.4.1 中对我有用。我不得不降级 numpy 版本。
  • 不知何故,我也将我的 numpy 升级到了 1.21.2,当我降级到 1.19 时,我的 conda 环境看到了以下错误——“tensorflow 2.4.0 需要 numpy~=1.19.2 ”。似乎直到 1.19.5 都没有任何重大问题。但也许张贴张量流 2.4.0 尚不支持更高版本。
  • 修复了 TF2.5.0、Windows 10、Python 3.9 中的问题。谢谢!
【解决方案2】:

解决方案:使用 Python 3.8,因为 Python 3.9 不受 Tensorflow 支持。

【讨论】:

    猜你喜欢
    • 2020-02-17
    • 2020-06-11
    • 2022-01-12
    • 1970-01-01
    • 1970-01-01
    • 2022-10-05
    • 2021-04-16
    • 1970-01-01
    • 2020-10-31
    相关资源
    最近更新 更多