Model classtf.keras.Model()
Model groups layers into an object with training and inference features.
Arguments
keras.Input object or list of
keras.Input objects.There are two ways to instantiate a Model:
1 - With the "Functional API", where you start from Input,
you chain layer calls to specify the model's forward pass,
and finally you create your model from inputs and outputs:
import tensorflow as tf
inputs = tf.keras.Input(shape=(3,))
x = tf.keras.layers.Dense(4, activation=tf.nn.relu)(inputs)
outputs = tf.keras.layers.Dense(5, activation=tf.nn.softmax)(x)
model = tf.keras.Model(inputs=inputs, outputs=outputs)
2 - By subclassing the Model class: in that case, you should define your
layers in __init__ and you should implement the model's forward pass
in call.
import tensorflow as tf
class MyModel(tf.keras.Model):
def __init__(self):
super(MyModel, self).__init__()
self.dense1 = tf.keras.layers.Dense(4, activation=tf.nn.relu)
self.dense2 = tf.keras.layers.Dense(5, activation=tf.nn.softmax)
def call(self, inputs):
x = self.dense1(inputs)
return self.dense2(x)
model = MyModel()
If you subclass Model, you can optionally have
a training argument (boolean) in call, which you can use to specify
a different behavior in training and inference:
import tensorflow as tf
class MyModel(tf.keras.Model):
def __init__(self):
super(MyModel, self).__init__()
self.dense1 = tf.keras.layers.Dense(4, activation=tf.nn.relu)
self.dense2 = tf.keras.layers.Dense(5, activation=tf.nn.softmax)
self.dropout = tf.keras.layers.Dropout(0.5)
def call(self, inputs, training=False):
x = self.dense1(inputs)
if training:
x = self.dropout(x, training=training)
return self.dense2(x)
model = MyModel()
Once the model is created, you can config the model with losses and metrics
with model.compile(), train the model with model.fit(), or use the model
to do prediction with model.predict().
summary methodModel.summary(line_length=None, positions=None, print_fn=None)
Prints a string summary of the network.
Arguments
[.33, .55, .67, 1.].print.
It will be called on each line of the summary.
You can set it to a custom function
in order to capture the string summary.Raises
summary() is called before the model is built.get_layer methodModel.get_layer(name=None, index=None)
Retrieves a layer based on either its name (unique) or index.
If name and index are both provided, index will take precedence.
Indices are based on order of horizontal graph traversal (bottom-up).
Arguments
Returns
A layer instance.
Raises