pdfcoffee

soumyasankar99
from soumyasankar99 More from this publisher
09.05.2023 Views

Chapter 2Let's see an example of a custom layer that simply multiplies an input by a matrixnamed kernel (for the sake of simplicity, the import lines are skipped in this text butare of course used in GitHub code):class MyLayer(layers.Layer):def __init__(self, output_dim, **kwargs):self.output_dim = output_dimsuper(MyLayer, self).__init__(**kwargs)def build(self, input_shape):# Create a trainable weight variable for this layer.self.kernel = self.add_weight(name='kernel',shape=(input_shape[1], self.output_dim),initializer='uniform',trainable=True)def call(self, inputs):# Do the multiplication and returnreturn tf.matmul(inputs, self.kernel)Once the MyLayer() custom brick is defined, it can be composed just like any otherbrick, as in this following example, where a Sequential model is defined by stackingMyLayer with a softmax activation function:model = tf.keras.Sequential([MyLayer(20),layers.Activation('softmax')])So, in short, you can use Model subclassing if you are in the business of buildingbricks.In this section we have seen that tf.keras offers a higher API level, with threedifferent programming models: Sequential API, Functional API, and Modelsubclassing. Now let's move our attention to callbacks, a different feature, whichis useful during training with tf.keras.CallbacksCallbacks are objects passed to a model to extend or modify behaviors duringtraining. There are a few useful callbacks that are commonly used in tf.keras:• tf.keras.callbacks.ModelCheckpoint: This feature is used to savecheckpoints of your model at regular intervals and recover in case ofproblems.[ 67 ]

TensorFlow 1.x and 2.x• tf.keras.callbacks.LearningRateScheduler: This feature is usedto dynamically change the learning rate during optimization.• tf.keras.callbacks.EarlyStopping: This feature is used to interrupttraining when validation performance has stopped improving after a while.• tf.keras.callbacks.TensorBoard: This feature is used to monitor themodel's behavior using TensorBoard.For example, we have already used TensorBoard as in this example:callbacks = [# Write TensorBoard logs to './logs' directorytf.keras.callbacks.TensorBoard(log_dir='./logs')]model.fit(data, labels, batch_size=256, epochs=100,callbacks=callbacks,validation_data=(val_data, val_labels))Saving a model and weightsAfter training a model, it can be useful to save the weights in a persistent way. Thisis easily achieved with the following code fragment, which saves to TensorFlow'sinternal format:# Save weights to a Tensorflow Checkpoint filemodel.save_weights('./weights/my_model')If you want to save in Keras's format, which is portable across multiple backends,then use:# Save weights to a HDF5 filemodel.save_weights('my_model.h5', save_format='h5')Weights are easily loaded with:# Restore the model's statemodel.load_weights(file_path)In addition to weights, a model can be serialized in JSON with:json_string = model.to_json() # savemodel = tf.keras.models.model_from_json(json_string) # restoreIf you prefer, a model can be serialized in YAML with:yaml_string = model.to_yaml() # savemodel = tf.keras.models.model_from_yaml(yaml_string) # restore[ 68 ]

Chapter 2

Let's see an example of a custom layer that simply multiplies an input by a matrix

named kernel (for the sake of simplicity, the import lines are skipped in this text but

are of course used in GitHub code):

class MyLayer(layers.Layer):

def __init__(self, output_dim, **kwargs):

self.output_dim = output_dim

super(MyLayer, self).__init__(**kwargs)

def build(self, input_shape):

# Create a trainable weight variable for this layer.

self.kernel = self.add_weight(name='kernel',

shape=(input_shape[1], self.output_dim),

initializer='uniform',

trainable=True)

def call(self, inputs):

# Do the multiplication and return

return tf.matmul(inputs, self.kernel)

Once the MyLayer() custom brick is defined, it can be composed just like any other

brick, as in this following example, where a Sequential model is defined by stacking

MyLayer with a softmax activation function:

model = tf.keras.Sequential([

MyLayer(20),

layers.Activation('softmax')])

So, in short, you can use Model subclassing if you are in the business of building

bricks.

In this section we have seen that tf.keras offers a higher API level, with three

different programming models: Sequential API, Functional API, and Model

subclassing. Now let's move our attention to callbacks, a different feature, which

is useful during training with tf.keras.

Callbacks

Callbacks are objects passed to a model to extend or modify behaviors during

training. There are a few useful callbacks that are commonly used in tf.keras:

• tf.keras.callbacks.ModelCheckpoint: This feature is used to save

checkpoints of your model at regular intervals and recover in case of

problems.

[ 67 ]

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

Saved successfully!

Ooh no, something went wrong!