【问题标题】:This error may indicate that you're trying to pass a Tensor to a NumPy call, which is not supported此错误可能表明您正在尝试将张量传递给 NumPy 调用,这是不受支持的
【发布时间】:2022-01-13 17:23:51
【问题描述】:

我遇到了这个错误,我该如何解决?

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
# train the model
model = define_model(vocab_size, max_length)
# train the model, run epochs manually and save after each epoch
epochs = 20
steps = len(train_descriptions)
for i in range(epochs):
    # create the data generator
    generator = data_generator(train_descriptions, train_features, tokenizer, max_length)
    # fit for one epoch
    model.fit_generator(generator, epochs=1, steps_per_epoch=steps, verbose=1)
    # save model
    model.save('model_' + str(i) + '.h5')

【问题讨论】:

  • 您是否使用带有 numpy 调用的自定义层?如果是,请检查此 numpy 函数是否有 tensorflow 对应部分,通常必须为 tensorflow 适配函数
  • 你读过this thread吗?
  • @MohanRadhakrishnan 是的,现在我通过更改 ops.py 文件解决了这个问题,谢谢

标签: python numpy tensorflow tensorflow-datasets


【解决方案1】:

正如其他人在其他地方指出的那样,这是由于特定 tensorflow 版本和特定 numpy 版本之间的不兼容造成的。

conda 版本 4.11.0

设置工作环境的命令:

conda activate base
conda env remove -y --name myenv
conda create -y --name myenv tensorflow=2.4.0
conda activate myenv
conda install -y numpy=1.19.2
conda install -y keras

系统信息

System:    Kernel: 5.4.0-100-generic x86_64 bits: 64 compiler: gcc v: 9.3.0 
           Desktop: Cinnamon 5.2.7 wm: muffin dm: LightDM Distro: Linux Mint 20.3 Una 
           base: Ubuntu 20.04 focal 
Machine:   Type: Laptop System: LENOVO product: 20308 v: Lenovo Ideapad Flex 14 serial: <filter> 
           Chassis: type: 10 v: Lenovo Ideapad Flex 14 serial: <filter> 
           Mobo: LENOVO model: Strawberry 4A v: 31900059Std serial: <filter> UEFI: LENOVO 
           v: 8ACN30WW date: 12/06/2013 
CPU:       Topology: Dual Core model: Intel Core i5-4200U bits: 64 type: MT MCP arch: Haswell 
           rev: 1 L2 cache: 3072 KiB 
           flags: avx avx2 lm nx pae sse sse2 sse3 sse4_1 sse4_2 ssse3 vmx bogomips: 18357 
           Speed: 798 MHz min/max: 800/2600 MHz Core speeds (MHz): 1: 798 2: 798 3: 798 4: 799 
Graphics:  Device-1: Intel Haswell-ULT Integrated Graphics vendor: Lenovo driver: i915 v: kernel 
           bus ID: 00:02.0 chip ID: 8086:0a16 
           Display: x11 server: X.Org 1.20.13 driver: modesetting unloaded: fbdev,vesa 
           resolution: 1366x768~60Hz 
           OpenGL: renderer: Mesa DRI Intel HD Graphics 4400 (HSW GT2) v: 4.5 Mesa 21.2.6 
           compat-v: 3.0 direct render: Yes 
 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-07
    • 1970-01-01
    • 2018-08-14
    • 2019-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-10
    相关资源
    最近更新 更多