【问题标题】:Loading NumPy array in dataset parser function在数据集解析器函数中加载 NumPy 数组
【发布时间】:2019-01-21 21:47:42
【问题描述】:

我正在使用 TensorFlow 数据集来使用硬盘驱动器中的数据。数据存储在 NumPy 数组中,NumPy 数组的路径存储在文本文件中。创建数据集时,我使用dataset.map() 函数将每个路径映射到 NumPy 数组。

以下是我的代码的相关部分:

def parser(path):
    x = np.load(path)
    return x

paths = ['data1.npy', 'data2.npy', 'data3.npy', 'data4.npy', ... ]

dataset = tf.data.Dataset.from_tensor_slices((paths))
dataset = dataset.map(map_func=parser)

但是,这会产生以下错误:

AttributeError: 'Tensor' object has no attribute 'read'

错误涉及x = np.load(path) 行。所以看来我无法在解析器函数中以这种方式加载 NumPy 数组,因为path 实际上不是字符串,而是张量。

这样做的正确方法是什么?如果可能,我想避免使用 TFRecords。


我也尝试过如下包装加载函数:

x = tf.py_func(np.load(path))

但这在该行上给了我同样的错误:

AttributeError: 'Tensor' object has no attribute 'read'

【问题讨论】:

标签: tensorflow


【解决方案1】:

您收到此错误是因为np.load 需要一个字符串作为输入,但获取的是 Tensor。 您可以使用tf.py_func 包装加载函数。

【讨论】:

  • 感谢您的建议。我试过这个,但是,我仍然收到相同的错误消息(见上文)。
  • 像这样使用tf.py_funcx = tf.py_func(np.load, path, tf.string)。可以看@jdehesa提供的链接,有一个很好的例子。
猜你喜欢
  • 2019-04-08
  • 2020-06-05
  • 2017-11-09
  • 2020-03-20
  • 1970-01-01
  • 2018-06-22
  • 2020-08-18
  • 1970-01-01
  • 2020-09-23
相关资源
最近更新 更多