0

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

我收到错误:AttributeError:“张量”对象没有属性“numpy”

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

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

为了更清楚地了解我所面临的情况,这里是一个简短的可重现的谷歌 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。

4

1 回答 1

1

您无法访问函数.numpy()内部.map()

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

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

AttributeError:“张量”对象在 Tensorflow 2.1 中没有属性“numpy”

于 2021-04-28T19:02:39.867 回答