pdfcoffee

soumyasankar99
from soumyasankar99 More from this publisher
09.05.2023 Views

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 ]

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 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!