在张量流中使用“dataset.map()”访问张量 numpy 数组
Accessing tensor numpy array using `dataset.map()` in tensorflow
我正在尝试从使用 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 中有更多详细信息。
您无法在 .map()
函数内访问 .numpy()
。
这不是错误,这是 TensorFlow 在幕后处理静态图的方式。
在这里阅读我的回答以获得更全面的解释。
我正在尝试从使用 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 中有更多详细信息。
您无法在 .map()
函数内访问 .numpy()
。
这不是错误,这是 TensorFlow 在幕后处理静态图的方式。
在这里阅读我的回答以获得更全面的解释。