使用带有函数(tensorflow 或 numpy)的 tf.data.Dataset.from_generator() 作为生成源(而不是文件)
Using tf.data.Dataset.from_generator() with a function (tensorflow or numpy) as the generating source (instead of a file)
该脚本试图使用一个函数(在本例中为 np.sin()
)作为生成器,旨在最终通过管道传输到模型中进行训练(因此 tf.Session()
)。不幸的是,我不断收到错误消息:
"ValueError: generator
yielded an element of shape () where an element of shape (1,) was expected."
我过去曾使用生成器从 .hdf5 文件中提取数据,但我在这里尝试做的是从可调用 class 中的函数生成波形数据。
在 tf.data.Dataset.from_generator()
之外调用生成函数可以正常工作:
next(sine_wave_source())
import numpy as np
import tensorflow as tf
class sine_wave_source:
def __init__(self,frequency = 1,sampling_frequency = 100):
self.fc = frequency
self.Fs = sampling_frequency
self.time_vector = np.arange(0,1,1/self.Fs,dtype = 'float32')
def __call__(self):
for t in self.time_vector:
yield np.sin(2*np.pi*self.fc*t,dtype = 'float32')
data_gen = tf.data.Dataset.from_generator(
sine_wave_source(),
output_types = (tf.float32),
output_shapes = (tf.TensorShape([1])))
data_iterator = data_gen.make_initializable_iterator()
next_sample = data_iterator.get_next()
with tf.Session() as sess:
sess.run(data_iterator.initializer)
for ii in range(0,100):
sample = sess.run([next_sample])
print(sample)
使用 output_shapes = (tf.TensorShape([1]))
表示数据集中的每个项目都将是一个具有一个元素的 one-dimensional 张量。然而,生成器在 sine_wave_source
returns 标量值。 TensorFlow 在这里很严格,它可以只广播到 one-item 向量,但认为这是一个错误。您可以将 output_shapes
更改为:
data_gen = tf.data.Dataset.from_generator(sine_wave_source(),
output_types=tf.float32,
output_shapes=tf.TensorShape([]))
或者将生成器更改为 return 列表或数组,例如:
def __call__(self):
for t in self.time_vector:
yield [np.sin(2 * np.pi * self.fc * t, dtype='float32')]
该脚本试图使用一个函数(在本例中为 np.sin()
)作为生成器,旨在最终通过管道传输到模型中进行训练(因此 tf.Session()
)。不幸的是,我不断收到错误消息:
"ValueError: generator
yielded an element of shape () where an element of shape (1,) was expected."
我过去曾使用生成器从 .hdf5 文件中提取数据,但我在这里尝试做的是从可调用 class 中的函数生成波形数据。
在 tf.data.Dataset.from_generator()
之外调用生成函数可以正常工作:
next(sine_wave_source())
import numpy as np
import tensorflow as tf
class sine_wave_source:
def __init__(self,frequency = 1,sampling_frequency = 100):
self.fc = frequency
self.Fs = sampling_frequency
self.time_vector = np.arange(0,1,1/self.Fs,dtype = 'float32')
def __call__(self):
for t in self.time_vector:
yield np.sin(2*np.pi*self.fc*t,dtype = 'float32')
data_gen = tf.data.Dataset.from_generator(
sine_wave_source(),
output_types = (tf.float32),
output_shapes = (tf.TensorShape([1])))
data_iterator = data_gen.make_initializable_iterator()
next_sample = data_iterator.get_next()
with tf.Session() as sess:
sess.run(data_iterator.initializer)
for ii in range(0,100):
sample = sess.run([next_sample])
print(sample)
使用 output_shapes = (tf.TensorShape([1]))
表示数据集中的每个项目都将是一个具有一个元素的 one-dimensional 张量。然而,生成器在 sine_wave_source
returns 标量值。 TensorFlow 在这里很严格,它可以只广播到 one-item 向量,但认为这是一个错误。您可以将 output_shapes
更改为:
data_gen = tf.data.Dataset.from_generator(sine_wave_source(),
output_types=tf.float32,
output_shapes=tf.TensorShape([]))
或者将生成器更改为 return 列表或数组,例如:
def __call__(self):
for t in self.time_vector:
yield [np.sin(2 * np.pi * self.fc * t, dtype='float32')]