如何在第一个时期正确缓存数据(Tensorflow、数据集)?
How to cache data during the first epoch correctly (Tensorflow, dataset)?
我正在尝试对 dataset
使用 cache
转换。这是我当前的代码(简化):
dataset = tf.data.TFRecordDataset(filenames, num_parallel_reads=1)
dataset = dataset.apply(tf.contrib.data.shuffle_and_repeat(buffer_size=5000, count=1))
dataset = dataset.map(_parser_a, num_parallel_calls=12)
dataset = dataset.padded_batch(
20,
padded_shapes=padded_shapes,
padding_values=padding_values
)
dataset = dataset.prefetch(buffer_size=1)
dataset = dataset.cache()
第一个纪元后,我收到以下错误消息:
The calling iterator did not fully read the dataset we were attempting
to cache. In order to avoid unexpected truncation of the sequence, the
current [partially cached] sequence will be dropped. This can occur if
you have a sequence similar to dataset.cache().take(k).repeat()
.
Instead, swap the order (i.e. dataset.take(k).cache().repeat()
)
然后,代码继续执行,仍然从硬盘而不是缓存中读取数据。那么,我应该在哪里放置 dataset.cache()
以避免错误?
谢谢。
Dataset.cache()
转换的实现相当简单:当您第一次完全 迭代时,它会构建一个通过它的元素列表,并且它 returns 来自该列表的元素在随后的迭代尝试中。如果第一遍只对数据执行 partial 遍历,那么列表是不完整的,TensorFlow 不会尝试使用缓存的数据,因为它不知道剩余的元素是否存在将需要,并且通常可能需要重新处理所有前面的元素以计算剩余元素。
通过修改您的程序以使用整个数据集,并对其进行迭代直到引发 tf.errors.OutOfRangeError
,缓存将拥有数据集中元素的完整列表,并将用于所有后续迭代次数。
我正在尝试对 dataset
使用 cache
转换。这是我当前的代码(简化):
dataset = tf.data.TFRecordDataset(filenames, num_parallel_reads=1)
dataset = dataset.apply(tf.contrib.data.shuffle_and_repeat(buffer_size=5000, count=1))
dataset = dataset.map(_parser_a, num_parallel_calls=12)
dataset = dataset.padded_batch(
20,
padded_shapes=padded_shapes,
padding_values=padding_values
)
dataset = dataset.prefetch(buffer_size=1)
dataset = dataset.cache()
第一个纪元后,我收到以下错误消息:
The calling iterator did not fully read the dataset we were attempting to cache. In order to avoid unexpected truncation of the sequence, the current [partially cached] sequence will be dropped. This can occur if you have a sequence similar to
dataset.cache().take(k).repeat()
. Instead, swap the order (i.e.dataset.take(k).cache().repeat()
)
然后,代码继续执行,仍然从硬盘而不是缓存中读取数据。那么,我应该在哪里放置 dataset.cache()
以避免错误?
谢谢。
Dataset.cache()
转换的实现相当简单:当您第一次完全 迭代时,它会构建一个通过它的元素列表,并且它 returns 来自该列表的元素在随后的迭代尝试中。如果第一遍只对数据执行 partial 遍历,那么列表是不完整的,TensorFlow 不会尝试使用缓存的数据,因为它不知道剩余的元素是否存在将需要,并且通常可能需要重新处理所有前面的元素以计算剩余元素。
通过修改您的程序以使用整个数据集,并对其进行迭代直到引发 tf.errors.OutOfRangeError
,缓存将拥有数据集中元素的完整列表,并将用于所有后续迭代次数。