【问题标题】:Accessing tensor numpy array using `dataset.map()` in tensorflow在tensorflow中使用`dataset.map()`访问张量numpy数组
【发布时间】:2021-07-22 01:03:38
【问题描述】:

我正在尝试从使用https://www.tensorflow.org/api_docs/python/tf/data/Dataset#map 处理的张量对象访问 numpy 数组。

我收到错误:AttributeError: 'Tensor' object has no attribute 'numpy'

当我尝试访问张量时:np_array = tensor.numpy()

如果我使用:dataset.take(n),我可以访问 numpy 数组。

为了更清楚地了解我所面临的情况,这里有一个简短的可重现的 google colab 中的错误示例:

https://colab.research.google.com/drive/13ectGEMDSygcyuW4ip9zrWaHO3pSxc3p?usp=sharing

张量流版本:2.4.1

更新:在上面的colab之外添加代码:

import os
import numpy as np
import tensorflow as tf

# This works
def get_spectrogram_and_label_id(file_path):
    spectrogram, label = get_spectrogram(audio) # not showing the function here since it is irrelevant
    return spectrogram, label

# This doesn't!
def get_spec_and_label_time(spectrogram, label):
    time_step_spectrogram = generate_time_step_samples(spectrogram)
    return time_step_spectrogram, label

# I want to manipulate the Tensor by extracting the numpy array as part of the map function
def generate_time_step_samples(tensor):
    np_array = tensor.numpy() # ERROR: AttributeError: 'Tensor' object has no attribute 'numpy'
    # Do something with the numpy array
    return np_array

filenames = ['file1.wav', 'file2.wav', ...]
files_ds = tf.data.Dataset.from_tensor_slices(filenames)
spectrogram_ds = files_ds.map(get_spectrogram_and_label_id) # this works
spectrogram_time_ds = spectrogram_ds.map(get_spec_and_label_time) # this doesn't

google colab 中的更多详细信息。

【问题讨论】:

标签: python tensorflow tensorflow2.0 tf.data.dataset


【解决方案1】:

您无法在 .map() 函数中访问 .numpy()

这不是错误,它是 TensorFlow 在幕后处理静态图的方式。

在这里阅读我的答案以获得更全面的解释。

AttributeError: 'Tensor' object has no attribute 'numpy' in Tensorflow 2.1

【讨论】:

  • 感谢@TimbusCalin 的参考,这很有帮助。但是,我正在努力实现我正在尝试做的事情。我尝试将:spectrogram_time_step_ds = spectrogram_ds.map(get_time_step_spectrogram_and_label_id) 替换为 spectrogram_time_step_ds = tf.py_function(func=get_time_step_spectrogram_and_label_id, inp=[spectrogram_ds], Tout=[tf.int64, np.ndarray]) 但得到 TypeError: Expected DataType for argument 'Tout' not <class 'numpy.ndarray'>. 我仍然需要访问 numpy 数组,但不确定如何
  • 如果你完全写数据类型会发生什么,比如np.int32/np.float32?
猜你喜欢
  • 2016-03-09
  • 2020-12-31
  • 1970-01-01
  • 1970-01-01
  • 2016-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-04
相关资源
最近更新 更多