tensorflow 数据集洗牌然后批处理或批处理然后洗牌
tensorflow dataset shuffle then batch or batch then shuffle
最近开始学习tensorflow。
我不确定是否有区别
x = np.array([[1],[2],[3],[4],[5]])
dataset = tf.data.Dataset.from_tensor_slices(x)
ds.shuffle(buffer_size=4)
ds.batch(4)
和
x = np.array([[1],[2],[3],[4],[5]])
dataset = tf.data.Dataset.from_tensor_slices(x)
ds.batch(4)
ds.shuffle(buffer_size=4)
此外,我不确定为什么我不能使用
dataset = dataset.shuffle_batch(buffer_size=2,batch_size=BATCH_SIZE)
因为它给出了错误
dataset = dataset.shuffle_batch(buffer_size=2,batch_size=BATCH_SIZE)
AttributeError: 'TensorSliceDataset' object has no attribute 'shuffle_batch'
谢谢!
TL;DR: 是的,有区别。几乎总是,您会想要调用 Dataset.shuffle()
before Dataset.batch()
. There is no shuffle_batch()
method on the tf.data.Dataset
class,并且您必须分别调用这两个方法来对数据集进行打乱和批处理。
tf.data.Dataset
的转换按照它们被调用的相同顺序应用。 Dataset.batch()
将其输入的连续元素组合成输出中的单个批处理元素。
通过考虑以下两个数据集,我们可以看到操作顺序的影响:
tf.enable_eager_execution() # To simplify the example code.
# Batch before shuffle.
dataset = tf.data.Dataset.from_tensor_slices([0, 0, 0, 1, 1, 1, 2, 2, 2])
dataset = dataset.batch(3)
dataset = dataset.shuffle(9)
for elem in dataset:
print(elem)
# Prints:
# tf.Tensor([1 1 1], shape=(3,), dtype=int32)
# tf.Tensor([2 2 2], shape=(3,), dtype=int32)
# tf.Tensor([0 0 0], shape=(3,), dtype=int32)
# Shuffle before batch.
dataset = tf.data.Dataset.from_tensor_slices([0, 0, 0, 1, 1, 1, 2, 2, 2])
dataset = dataset.shuffle(9)
dataset = dataset.batch(3)
for elem in dataset:
print(elem)
# Prints:
# tf.Tensor([2 0 2], shape=(3,), dtype=int32)
# tf.Tensor([2 1 0], shape=(3,), dtype=int32)
# tf.Tensor([0 1 1], shape=(3,), dtype=int32)
在第一个版本中(batch before shuffle),每个batch的元素都是来自input的3个连续元素;而在第二个版本中(批处理前随机播放),它们是从输入中随机抽取的。通常,当通过(某些变体)小批量 stochastic gradient descent 进行训练时,应从总输入中尽可能均匀地对每个批量的元素进行采样。否则,网络可能会过度适应输入数据中的任何结构,并且生成的网络不会达到那么高的准确度。
完全同意@mrry,但存在一种情况,您可能希望在 洗牌之前进行批处理。假设您正在处理一些将输入 RNN 的文本数据。这里每个句子都被视为一个序列,一个批次将包含多个序列。由于句子的长度是可变的,我们需要将一批中的句子 pad 到统一的长度。一种有效的方法是通过批处理将相似长度的句子组合在一起,然后进行洗牌。否则,我们可能会以 <pad>
标记满的批次结束。
最近开始学习tensorflow。
我不确定是否有区别
x = np.array([[1],[2],[3],[4],[5]])
dataset = tf.data.Dataset.from_tensor_slices(x)
ds.shuffle(buffer_size=4)
ds.batch(4)
和
x = np.array([[1],[2],[3],[4],[5]])
dataset = tf.data.Dataset.from_tensor_slices(x)
ds.batch(4)
ds.shuffle(buffer_size=4)
此外,我不确定为什么我不能使用
dataset = dataset.shuffle_batch(buffer_size=2,batch_size=BATCH_SIZE)
因为它给出了错误
dataset = dataset.shuffle_batch(buffer_size=2,batch_size=BATCH_SIZE)
AttributeError: 'TensorSliceDataset' object has no attribute 'shuffle_batch'
谢谢!
TL;DR: 是的,有区别。几乎总是,您会想要调用 Dataset.shuffle()
before Dataset.batch()
. There is no shuffle_batch()
method on the tf.data.Dataset
class,并且您必须分别调用这两个方法来对数据集进行打乱和批处理。
tf.data.Dataset
的转换按照它们被调用的相同顺序应用。 Dataset.batch()
将其输入的连续元素组合成输出中的单个批处理元素。
通过考虑以下两个数据集,我们可以看到操作顺序的影响:
tf.enable_eager_execution() # To simplify the example code.
# Batch before shuffle.
dataset = tf.data.Dataset.from_tensor_slices([0, 0, 0, 1, 1, 1, 2, 2, 2])
dataset = dataset.batch(3)
dataset = dataset.shuffle(9)
for elem in dataset:
print(elem)
# Prints:
# tf.Tensor([1 1 1], shape=(3,), dtype=int32)
# tf.Tensor([2 2 2], shape=(3,), dtype=int32)
# tf.Tensor([0 0 0], shape=(3,), dtype=int32)
# Shuffle before batch.
dataset = tf.data.Dataset.from_tensor_slices([0, 0, 0, 1, 1, 1, 2, 2, 2])
dataset = dataset.shuffle(9)
dataset = dataset.batch(3)
for elem in dataset:
print(elem)
# Prints:
# tf.Tensor([2 0 2], shape=(3,), dtype=int32)
# tf.Tensor([2 1 0], shape=(3,), dtype=int32)
# tf.Tensor([0 1 1], shape=(3,), dtype=int32)
在第一个版本中(batch before shuffle),每个batch的元素都是来自input的3个连续元素;而在第二个版本中(批处理前随机播放),它们是从输入中随机抽取的。通常,当通过(某些变体)小批量 stochastic gradient descent 进行训练时,应从总输入中尽可能均匀地对每个批量的元素进行采样。否则,网络可能会过度适应输入数据中的任何结构,并且生成的网络不会达到那么高的准确度。
完全同意@mrry,但存在一种情况,您可能希望在 洗牌之前进行批处理。假设您正在处理一些将输入 RNN 的文本数据。这里每个句子都被视为一个序列,一个批次将包含多个序列。由于句子的长度是可变的,我们需要将一批中的句子 pad 到统一的长度。一种有效的方法是通过批处理将相似长度的句子组合在一起,然后进行洗牌。否则,我们可能会以 <pad>
标记满的批次结束。