Copyright 2020 The TensorFlow Datasets Authors, Licensed under the Apache License, Version 2.0
In [0]:
import tensorflow.compat.v2 as tf
import tensorflow_datasets as tfds
tfds.disable_progress_bar()
tf.enable_v2_behavior()
In [0]:
(ds_train, ds_test), ds_info = tfds.load(
'mnist',
split=['train', 'test'],
shuffle_files=True,
as_supervised=True,
with_info=True,
)
Apply the following transormations:
ds.map: TFDS provide the images as tf.uint8, while the model expect tf.float32, so normalize imagesds.cache As the dataset fit in memory, cache before shuffling for better performance.ds.shuffle: For true randomness, set the shuffle buffer to the full dataset size.ds.batch: Batch after shuffling to get unique batches at each epoch.ds.prefetch: Good practice to end the pipeline by prefetching for performances.
In [0]:
def normalize_img(image, label):
"""Normalizes images: `uint8` -> `float32`."""
return tf.cast(image, tf.float32) / 255., label
ds_train = ds_train.map(
normalize_img, num_parallel_calls=tf.data.experimental.AUTOTUNE)
ds_train = ds_train.cache()
ds_train = ds_train.shuffle(ds_info.splits['train'].num_examples)
ds_train = ds_train.batch(128)
ds_train = ds_train.prefetch(tf.data.experimental.AUTOTUNE)
In [0]:
ds_test = ds_test.map(
normalize_img, num_parallel_calls=tf.data.experimental.AUTOTUNE)
ds_test = ds_test.batch(128)
ds_test = ds_test.cache()
ds_test = ds_test.prefetch(tf.data.experimental.AUTOTUNE)
In [0]:
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28, 1)),
tf.keras.layers.Dense(128,activation='relu'),
tf.keras.layers.Dense(10, activation='softmax')
])
model.compile(
loss='sparse_categorical_crossentropy',
optimizer=tf.keras.optimizers.Adam(0.001),
metrics=['accuracy'],
)
model.fit(
ds_train,
epochs=6,
validation_data=ds_test,
)