【问题标题】:AttributeError: 'tensorflow.python.framework.ops.EagerTensor' object has no attribute 'decode'AttributeError:“tensorflow.python.framework.ops.EagerTensor”对象没有属性“decode”
【发布时间】:2019-11-02 13:29:34
【问题描述】:

我在 python 3.7、Ubuntu 16.04 上使用 tensorflow。抛出上述错误的代码写在下面。它基于以下code。我在 tensorflow 1.13 和 2.0.0-beta1 上都遇到了这个错误

我有一个数据集文件夹,其中包含数百万个表单数据对(图像、时间序列)。时间序列采用 numpy 格式。我想使用 np.load() 函数来加载数据。但文件名是字符串张量格式。问题是 np.load() 不接受 tensorflow.python.framework.ops.EagerTensor

import tensorflow as tf
import numpy as np
import imageio

#tf.enable_eager_execution()    # use this line if using tensorflow 1.13

imageio.imwrite('data.jpg', np.random.rand(256,256,3))
np.save('data.npy',np.ones(1024))

def load(image_file,timeseries_file):
    image = tf.io.read_file(image_file)
    image = tf.image.decode_jpeg(image)
    timeseries = np.load(timeseries_file.decode())
    timeseries = tf.convert_to_tensor(timeseries, np.float32)
    image = tf.cast(image, tf.float32)
    timeseries = tf.cast(timeseries, tf.float32)
    return image, timeseries

image_files = ['data.jpg']
timeseries_files = ['data.npy']
train_dataset = tf.data.Dataset.from_tensor_slices((image_files, timeseries_files))
train_dataset = train_dataset.map(
lambda image_file, timeseries_file: tuple(tf.py_function(
    load, [image_file, timeseries_file], [tf.float32, tf.float32])))
for x in train_dataset.take(1):
    print(x)

【问题讨论】:

    标签: python numpy tensorflow tensorflow-datasets tensorflow2.0


    【解决方案1】:

    EagerTensor 可以转换为 numpy 数组:

    tensor.numpy()
    # or
    np.array(tensor)
    

    那就试试吧:

    timeseries = np.load(timeseries_file.numpy().decode())
    

    【讨论】:

      【解决方案2】:

      请使用

      import tensorflow.compat.v1 as tf 
      tf.disable_v2_behavior()
      

      【讨论】:

      • 一些解释如何以及为什么解决错误将非常有帮助。
      • 在 tf 2.0 中,默认情况下启用了急切执行,因此禁用 v2 行为会禁用急切执行。 More on eager execution.
      猜你喜欢
      • 2021-12-03
      • 1970-01-01
      • 1970-01-01
      • 2018-12-01
      • 2014-05-26
      • 2021-07-05
      • 2021-10-30
      • 2019-03-18
      • 2018-12-22
      相关资源
      最近更新 更多