pdfcoffee
Chapter 16Loading data with tf.dataOur goal is to implement a simple CNN on MNIST data (see Chapter 4,Convolutional Neural Networks). Then we want to run the model on a TPU. Todo this, we must load the data with tf.data libraries. Hence, we need to definea training and test function (see Chapter 2, TensorFlow 1.x and 2.x) as shown in thefollowing code:# training input functiondef train_input_fn(batch_size=1024):# Convert the inputs to a Dataset.dataset = tf.data.Dataset.from_tensor_slices((x_train,y_train))# Shuffle, repeat, and batch the examples.dataset = dataset.cache() # Loads the data into memorydataset = dataset.shuffle(1000, reshuffle_each_iteration=True)dataset = dataset.repeat()dataset = dataset.batch(batch_size, drop_remainder=True)return dataset# testing input functiondef test_input_fn(batch_size=1024):dataset = tf.data.Dataset.from_tensor_slices((x_test,y_test))# Shuffle, repeat, and batch the examples.dataset = dataset.cache()dataset = dataset.shuffle(1000, reshuffle_each_iteration=True)dataset = dataset.repeat()dataset = dataset.batch(batch_size, drop_remainder=True)return datasetWhere (x_train, y_train), (x_test, y_test) = mnist.load_data(). Notethat drop_remainder=True is an important parameter that forces the batch methodto pass fixed shapes expected by the TPUs. Note that TPU v2 has an MMU with 128× 128 multipliers. Usually you get the best performance by setting the batch size to128 per TPU core. With 10 TPU cores, for instance, the batch size would be 1,280.Building a model and loading it into the TPUAs of November 2019, TensorFlow 2.0 does not fully support TPUs. They areavailable in TensorFlow 1.5.0, and with the nightly build. Let's first see an examplewith TensorFlow 1.5, and the example with the nightly build will be shown later.[ 581 ]
Tensor Processing UnitNote that full support for TPUDistributionStrategy is plannedfor TensorFlow 2.1. 2.0 has limited support, and the issue istracked in https://github.com/tensorflow/tensorflow/issues/24412.So, let's define a standard CNN model made up of three convolutional layers,alternated with max-pooling layers and followed by two dense layers with a dropoutin the middle. For the sake of brevity, the definition of input_shape, batch_size isomitted. In this case, we use the functional tf.keras API (see Chapter 2, TensorFlow1.x and 2.x):Inp = tf.keras.Input(name='input', shape=input_shape, batch_size=batch_size, dtype=tf.float32)x = Conv2D(32, kernel_size=(3, 3), activation='relu',name = 'Conv_01')(Inp)x = MaxPooling2D(pool_size=(2, 2),name = 'MaxPool_01')(x)x = Conv2D(64, (3, 3), activation='relu',name = 'Conv_02')(x)x = MaxPooling2D(pool_size=(2, 2),name = 'MaxPool_02')(x)x = Conv2D(64, (3, 3), activation='relu',name = 'Conv_03')(x)x = Flatten(name = 'Flatten_01')(x)x = Dense(64, activation='relu',name = 'Dense_01')(x)x = Dropout(0.5,name = 'Dropout_02')(x)output = Dense(num_classes, activation='softmax',name = 'Dense_02')(x)model = tf.keras.Model(inputs=[Inp], outputs=[output])Let's now use Adam optimizer and compile the model:#Use a tf optimizer rather than a Keras one for nowopt = tf.train.AdamOptimizer(learning_rate)model.compile(optimizer=opt,loss='categorical_crossentropy',metrics=['acc'])Then, we call tpu.keras_to_tpu_model to convert to a TPU model and then we usethe tpu.TPUDistributionStrategy for running on a TPU. It's as simple as that; wejust need to take the appropriate strategy with TPUDistributionStrategy() and allthe rest is done transparently on our behalf:tpu_model = tf.contrib.tpu.keras_to_tpu_model(model,strategy=tf.contrib.tpu.TPUDistributionStrategy(tf.contrib.cluster_resolver.TPUClusterResolver(TPU_ADDRESS)))[ 582 ]
- Page 565 and 566: An introduction to AutoMLAs the nex
- Page 567 and 568: An introduction to AutoMLOnce the m
- Page 569 and 570: An introduction to AutoMLFigure 65:
- Page 571 and 572: An introduction to AutoMLOnce the m
- Page 573 and 574: An introduction to AutoMLWe can als
- Page 575 and 576: An introduction to AutoMLThe most e
- Page 577 and 578: An introduction to AutoMLReferences
- Page 579 and 580: The Math Behind Deep LearningSome m
- Page 581 and 582: The Math Behind Deep LearningSuppos
- Page 583 and 584: The Math Behind Deep LearningNote t
- Page 585 and 586: The Math Behind Deep LearningTheref
- Page 587 and 588: The Math Behind Deep LearningThe ea
- Page 589 and 590: The Math Behind Deep LearningThe re
- Page 591 and 592: The Math Behind Deep LearningCase 2
- Page 593 and 594: The Math Behind Deep LearningIn thi
- Page 595 and 596: The Math Behind Deep LearningHere,
- Page 597 and 598: The Math Behind Deep Learning(Note
- Page 599 and 600: The Math Behind Deep LearningIn man
- Page 601 and 602: The Math Behind Deep LearningIf we
- Page 603 and 604: The Math Behind Deep LearningChapte
- Page 605 and 606: The Math Behind Deep LearningThis c
- Page 607 and 608: Tensor Processing UnitMany people b
- Page 609 and 610: Tensor Processing UnitThe sequentia
- Page 611 and 612: Tensor Processing UnitIf you want t
- Page 613 and 614: Tensor Processing UnitOn the other
- Page 615: Tensor Processing UnitHow to use TP
- Page 619 and 620: Tensor Processing UnitEpoch 10/1060
- Page 621 and 622: Tensor Processing UnitFigure 11: Go
- Page 623 and 624: Tensor Processing UnitThen the usag
- Page 626 and 627: Other Books YouMay EnjoyIf you enjo
- Page 628 and 629: Other Books You May EnjoyAI Crash C
- Page 630: Other Books You May EnjoyLeave a re
- Page 633 and 634: AutoML pipelinedata preparation 493
- Page 635 and 636: Deep Deterministic Policy Gradient(
- Page 637 and 638: Google cloud consolereference link
- Page 639 and 640: used, for building GAN 193-198MNIST
- Page 641 and 642: regularizersreference link 38reinfo
- Page 643 and 644: TensorFlow Lite 81TensorFlow Core r
- Page 645: Xxception networks 160, 162YYOLO ne
Chapter 16
Loading data with tf.data
Our goal is to implement a simple CNN on MNIST data (see Chapter 4,
Convolutional Neural Networks). Then we want to run the model on a TPU. To
do this, we must load the data with tf.data libraries. Hence, we need to define
a training and test function (see Chapter 2, TensorFlow 1.x and 2.x) as shown in the
following code:
# training input function
def train_input_fn(batch_size=1024):
# Convert the inputs to a Dataset.
dataset = tf.data.Dataset.from_tensor_slices((x_train,y_train))
# Shuffle, repeat, and batch the examples.
dataset = dataset.cache() # Loads the data into memory
dataset = dataset.shuffle(1000, reshuffle_each_iteration=True)
dataset = dataset.repeat()
dataset = dataset.batch(batch_size, drop_remainder=True)
return dataset
# testing input function
def test_input_fn(batch_size=1024):
dataset = tf.data.Dataset.from_tensor_slices((x_test,y_test))
# Shuffle, repeat, and batch the examples.
dataset = dataset.cache()
dataset = dataset.shuffle(1000, reshuffle_each_iteration=True)
dataset = dataset.repeat()
dataset = dataset.batch(batch_size, drop_remainder=True)
return dataset
Where (x_train, y_train), (x_test, y_test) = mnist.load_data(). Note
that drop_remainder=True is an important parameter that forces the batch method
to pass fixed shapes expected by the TPUs. Note that TPU v2 has an MMU with 128
× 128 multipliers. Usually you get the best performance by setting the batch size to
128 per TPU core. With 10 TPU cores, for instance, the batch size would be 1,280.
Building a model and loading it into the TPU
As of November 2019, TensorFlow 2.0 does not fully support TPUs. They are
available in TensorFlow 1.5.0, and with the nightly build. Let's first see an example
with TensorFlow 1.5, and the example with the nightly build will be shown later.
[ 581 ]