將數據集寫入 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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。