創建一個Dataset
,包含來自一個或多個文本文件的行。
繼承自:Dataset
用法
tf.data.TextLineDataset(
filenames, compression_type=None, buffer_size=None, num_parallel_reads=None,
name=None
)
參數
-
filenames
一個tf.data.Dataset
,其元素是tf.string
標量、tf.string
張量或可以轉換為tf.string
張量的值(例如 Python 字符串列表)。 -
compression_type
(可選。)tf.string
標量評估為""
(無壓縮)、"ZLIB"
或"GZIP"
之一。 -
buffer_size
(可選。)tf.int64
標量,表示要緩衝的字節數。值 0 會導致根據壓縮類型選擇默認緩衝值。 -
num_parallel_reads
(可選。)tf.int64
標量,表示要並行讀取的文件數。如果大於1,則以交錯的順序輸出並行讀取的文件記錄。如果您的輸入管道存在 I/O 瓶頸,請考慮將此參數設置為大於 1 的值以並行化 I/O。如果None
,文件將被順序讀取。 -
name
(可選。) tf.data 操作的名稱。
屬性
-
element_spec
此數據集元素的類型規範。dataset = tf.data.Dataset.from_tensor_slices([1, 2, 3]) dataset.element_spec TensorSpec(shape=(), dtype=tf.int32, name=None)
如需更多信息,請閱讀本指南。
tf.data.TextLineDataset
從文本文件加載文本並創建一個數據集,其中文件的每一行都成為數據集的一個元素。
例如,假設我們有 2 個文件 "text_lines0.txt" 和 "text_lines1.txt",其中包含以下行:
with open('/tmp/text_lines0.txt', 'w') as f:
f.write('the cow\n')
f.write('jumped over\n')
f.write('the moon\n')
with open('/tmp/text_lines1.txt', 'w') as f:
f.write('jack and jill\n')
f.write('went up\n')
f.write('the hill\n')
我們可以從它們構造一個 TextLineDataset,如下所示:
dataset = tf.data.TextLineDataset(['/tmp/text_lines0.txt',
'/tmp/text_lines1.txt'])
數據集的元素預計為:
for element in dataset.as_numpy_iterator():
print(element)
b'the cow'
b'jumped over'
b'the moon'
b'jack and jill'
b'went up'
b'the hill'
相關用法
- Python tf.data.TextLineDataset.reduce用法及代碼示例
- Python tf.data.TextLineDataset.with_options用法及代碼示例
- Python tf.data.TextLineDataset.as_numpy_iterator用法及代碼示例
- Python tf.data.TextLineDataset.random用法及代碼示例
- Python tf.data.TextLineDataset.take_while用法及代碼示例
- Python tf.data.TextLineDataset.from_tensor_slices用法及代碼示例
- Python tf.data.TextLineDataset.take用法及代碼示例
- Python tf.data.TextLineDataset.apply用法及代碼示例
- Python tf.data.TextLineDataset.batch用法及代碼示例
- Python tf.data.TextLineDataset.skip用法及代碼示例
- Python tf.data.TextLineDataset.from_generator用法及代碼示例
- Python tf.data.TextLineDataset.padded_batch用法及代碼示例
- Python tf.data.TextLineDataset.concatenate用法及代碼示例
- Python tf.data.TextLineDataset.window用法及代碼示例
- Python tf.data.TextLineDataset.get_single_element用法及代碼示例
- Python tf.data.TextLineDataset.interleave用法及代碼示例
- Python tf.data.TextLineDataset.scan用法及代碼示例
- Python tf.data.TextLineDataset.bucket_by_sequence_length用法及代碼示例
- Python tf.data.TextLineDataset.choose_from_datasets用法及代碼示例
- Python tf.data.TextLineDataset.prefetch用法及代碼示例
注:本文由純淨天空篩選整理自tensorflow.org大神的英文原創作品 tf.data.TextLineDataset。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。