使用from_生成器创建的Tensorflow数据集不按批大小迭代
我从生成器中创建了一个tensorflow数据集,但不知道如何按批大小对其进行迭代使用from_生成器创建的Tensorflow数据集不按批大小迭代,tensorflow,tensorflow-datasets,Tensorflow,Tensorflow Datasets,我从生成器中创建了一个tensorflow数据集,但不知道如何按批大小对其进行迭代 def ds_gen(): x=np.random.random((10,10,3)) y=np.随机.随机((2)) 产量x,y def create_tf_数据集(): dataset=tf.data.dataset.from_生成器(ds_-gen,output_-types=(tf.float32,tf.float32),output_-shapes=((10,10,3),(2,)) 返回数据集 ds=创
def ds_gen():
x=np.random.random((10,10,3))
y=np.随机.随机((2))
产量x,y
def create_tf_数据集():
dataset=tf.data.dataset.from_生成器(ds_-gen,output_-types=(tf.float32,tf.float32),output_-shapes=((10,10,3),(2,))
返回数据集
ds=创建数据集()
ds=ds.批次(10)
对于x_批次,ds中的y_批次:
打印(x_batch.shape,y_batch.shape)
此代码在1的批大小上循环,而不是在10的批大小上循环。请参阅下面的代码以按批大小进行迭代
import numpy as np
import tensorflow as tf
def ds():
for i in range(1000):
x = np.random.rand(10,10,3)
y = np.random.rand(2)
yield x,y
ds = tf.data.Dataset.from_generator(ds, output_types=(tf.float32, tf.float32), output_shapes=((10, 10, 3), (2,)))
ds = ds.batch(10)
for batch, (x,y) in enumerate(ds):
pass
print("Data shape: ", x.shape, y.shape)
输出:
Data shape: (10, 10, 10, 3) (10, 2)
如果您更改
ds=ds.batch(1)
,则输出将是数据形状:(1,10,10,3)(1,2)
请参考下面的代码以按批大小进行迭代
import numpy as np
import tensorflow as tf
def ds():
for i in range(1000):
x = np.random.rand(10,10,3)
y = np.random.rand(2)
yield x,y
ds = tf.data.Dataset.from_generator(ds, output_types=(tf.float32, tf.float32), output_shapes=((10, 10, 3), (2,)))
ds = ds.batch(10)
for batch, (x,y) in enumerate(ds):
pass
print("Data shape: ", x.shape, y.shape)
输出:
Data shape: (10, 10, 10, 3) (10, 2)
如果更改ds=ds.batch(1)
,则输出将是数据形状:(1,10,10,3)(1,2)