text
stringlengths 0
4.99k
|
---|
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 method
|
Model.summary(line_length=None, positions=None, print_fn=None)
|
Prints a string summary of the network.
|
Arguments
|
line_length: Total length of printed lines (e.g. set this to adapt the display to different terminal window sizes).
|
positions: Relative or absolute positions of log elements in each line. If not provided, defaults to [.33, .55, .67, 1.].
|
print_fn: Print function to use. Defaults to 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
|
ValueError: if summary() is called before the model is built.
|
get_layer method
|
Model.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
|
name: String, name of layer.
|
index: Integer, index of layer.
|
Returns
|
A layer instance.
|
Raises
|
ValueError: In case of invalid layer name or index.The Sequential class
|
Sequential class
|
tf.keras.Sequential(layers=None, name=None)
|
Sequential groups a linear stack of layers into a tf.keras.Model.
|
Sequential provides training and inference features on this model.
|
Examples
|
>>> # Optionally, the first layer can receive an `input_shape` argument:
|
>>> model = tf.keras.Sequential()
|
>>> model.add(tf.keras.layers.Dense(8, input_shape=(16,)))
|
>>> # Afterwards, we do automatic shape inference:
|
>>> model.add(tf.keras.layers.Dense(4))
|
>>> # This is identical to the following:
|
>>> model = tf.keras.Sequential()
|
>>> model.add(tf.keras.Input(shape=(16,)))
|
>>> model.add(tf.keras.layers.Dense(8))
|
>>> # Note that you can also omit the `input_shape` argument.
|
>>> # In that case the model doesn't have any weights until the first call
|
>>> # to a training/evaluation method (since it isn't yet built):
|
>>> model = tf.keras.Sequential()
|
>>> model.add(tf.keras.layers.Dense(8))
|
>>> model.add(tf.keras.layers.Dense(4))
|
>>> # model.weights not created yet
|
>>> # Whereas if you specify the input shape, the model gets built
|
>>> # continuously as you are adding layers:
|
>>> model = tf.keras.Sequential()
|
>>> model.add(tf.keras.layers.Dense(8, input_shape=(16,)))
|
>>> model.add(tf.keras.layers.Dense(4))
|
>>> len(model.weights)
|
4
|
>>> # When using the delayed-build pattern (no input shape specified), you can
|
>>> # choose to manually build your model by calling
|
>>> # `build(batch_input_shape)`:
|
>>> model = tf.keras.Sequential()
|
>>> model.add(tf.keras.layers.Dense(8))
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.