将数据集写入 TFRecord 文件。 (已弃用)
用法
tf.data.experimental.TFRecordWriter(
filename, compression_type=None
)
参数
-
filename
一个字符串路径,指示在哪里写入 TFRecord 数据。 -
compression_type
(可选。)一个字符串,指示写入文件时要使用的压缩类型。有关可用的压缩类型,请参阅tf.io.TFRecordCompressionType
。默认为None
。
警告:此函数已弃用。它将在未来的版本中删除。更新说明:要将 TFRecords 写入磁盘,请使用 tf.io.TFRecordWriter
。要保存和加载数据集的内容,请使用 tf.data.experimental.save
和 tf.data.experimental.load
数据集的元素必须是标量字符串。要将数据集元素序列化为字符串,可以使用 tf.io.serialize_tensor
函数。
dataset = tf.data.Dataset.range(3)
dataset = dataset.map(tf.io.serialize_tensor)
writer = tf.data.experimental.TFRecordWriter("/path/to/file.tfrecord")
writer.write(dataset)
要读回元素,请使用 TFRecordDataset
。
dataset = tf.data.TFRecordDataset("/path/to/file.tfrecord")
dataset = dataset.map(lambda x:tf.io.parse_tensor(x, tf.int64))
要跨多个 TFRecord 文件分片 dataset
:
dataset = ... # dataset to be written
def reduce_func(key, dataset):
filename = tf.strings.join([PATH_PREFIX, tf.strings.as_string(key)])
writer = tf.data.experimental.TFRecordWriter(filename)
writer.write(dataset.map(lambda _, x:x))
return tf.data.Dataset.from_tensors(filename)
dataset = dataset.enumerate()
dataset = dataset.apply(tf.data.experimental.group_by_window(
lambda i, _:i % NUM_SHARDS, reduce_func, tf.int64.max
))
# Iterate through the dataset to trigger data writing.
for _ in dataset:
pass
相关用法
- Python tf.data.experimental.RandomDataset.group_by_window用法及代码示例
- Python tf.data.experimental.SqlDataset.enumerate用法及代码示例
- Python tf.data.experimental.make_saveable_from_iterator用法及代码示例
- Python tf.data.experimental.SqlDataset.zip用法及代码示例
- Python tf.data.experimental.Counter用法及代码示例
- Python tf.data.experimental.SqlDataset.shard用法及代码示例
- Python tf.data.experimental.CsvDataset.window用法及代码示例
- Python tf.data.experimental.RandomDataset.cache用法及代码示例
- Python tf.data.experimental.SqlDataset.snapshot用法及代码示例
- Python tf.data.experimental.CsvDataset.apply用法及代码示例
- Python tf.data.experimental.DatasetInitializer用法及代码示例
- Python tf.data.experimental.ignore_errors用法及代码示例
- Python tf.data.experimental.unbatch用法及代码示例
- Python tf.data.experimental.RandomDataset.map用法及代码示例
- Python tf.data.experimental.CsvDataset.flat_map用法及代码示例
- Python tf.data.experimental.assert_cardinality用法及代码示例
- Python tf.data.experimental.CsvDataset.random用法及代码示例
- Python tf.data.experimental.save用法及代码示例
- Python tf.data.experimental.CsvDataset.cardinality用法及代码示例
- Python tf.data.experimental.CsvDataset.interleave用法及代码示例
注:本文由纯净天空筛选整理自tensorflow.org大神的英文原创作品 tf.data.experimental.TFRecordWriter。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。