tf.data+tf.distribution

tf.data:TensorFlow Input Pipeline

  1. Extract:
    • read data from memory/storage
    • parse file format
  2. Transform
    • text vectorization
    • image transformations
    • video temporal sampling
    • shuffling, batching, …
  3. Load
    • transfer data to the accelerator

简单实例:

# one method
import tensorflow as tf
def preprocess(record)"
	pass
dataset = tf.data.TFRecordDataset(../*.tfrecord")  # reads data from storage
dataset = dataset.map(preprocess, num_parallel_calls=Y)  # applies user-defined preprocessing
dataset = dataset.batch(batch_size=32)  # 
dataset = dataset.prefetch(buffer_size=X)

model = ...
model.fit(dataset, epochs=10)




# another method
import tensorflow as tf
def preprocess(record)"
	pass
dataset = tf.data.Dataset.list_files(../*.tfrecord")  # reads data from storage
dataset = dataset.interleave(TFRecordDataset, num_parallel_calls=Z)
dataset = dataset.map(preprocess, num_parallel_calls=X)  # applies user-defined preprocessing
dataset = dataset.batch(batch_size=32)  # 
dataset = dataset.prefetch(buffer_size=X)

model = ...
model.fit(dataset, epochs=10)

tf.data Options

  • tf.data.Options
    • statistics aggregation
    • optimizations (autotuning, fushion, vectorization, parallelization, determinism,…)
    • threading (private thread pool, intra op parallelism)
dataset = ...
options = tf.data.Options()
options.experimental_optimization.map_parallelization = True
dataset = dataset.with_options(options)

TFDS:TensorFlow Datasets

  • https://www.tensorflow.org/datasets/datasets
  • canned datasets ready to be used with rest of TensorFlow
import tensorflow as tf
import tensorflow_datasets as tfds

# see available datasets
print(tfds.list_builders())

# construct a tf.data.Dataset
dataset = tfds.load(name="mnist", split=tfds.Split.TRAIN)

# customize your input pipeline
dataset = dataset.shuffle(1024).batch(32)
for features in dataset.take(1):
	image, label = features["image"], features["labels"]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值