Sequential Model Creation - tf.keras.Sequential:
Example:
model = tf.keras.Sequential([
tf.keras.layers.Dense(64, activation='relu', input_shape=(784,)),
tf.keras.layers.Dense(10, activation='softmax')
])
Output:
Creating a sequential model with two dense layers for a simple feedforward neural network.
Functional API Model Building:
Example:
inputs = tf.keras.layers.Input(shape=(784,))
x = tf.keras.layers.Dense(64, activation='relu')(inputs)
outputs = tf.keras.layers.Dense(10, activation='softmax')(x)
model = tf.keras.Model(inputs, outputs)
Output:
Building a model using the functional API for greater flexibility.
Custom Layer Creation - Subclassing tf.keras.layers.Layer:
Example:
class MyDenseLayer(tf.keras.layers.Layer):
def __init__(self, units):
super(MyDenseLayer, self).__init__()
self.units = units
def build(self, input_shape):
self.w = self.add_weight("weights", shape=(input_shape[-1], self.units))
self.b = self.add_weight("bias", shape=(self.units,))
def call(self, inputs):
return tf.matmul(inputs, self.w) + self.b
my_layer = MyDenseLayer(64)
Output:
Creating a custom layer by subclassing tf.keras.layers.Layer.
Model Compilation - model.compile:
Example:
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
Output:
Compiling the model with an optimizer, loss function, and evaluation metrics.
Model Summary - model.summary():
Example:
model.summary()
Output:
Displaying a summary of the model architecture with layer names, output shapes, and parameter counts.
Model Visualization - tf.keras.utils.plot_model:
Example:
tf.keras.utils.plot_model(model, to_file='model.png', show_shapes=True)
Output:
Generating a graphical visualization of the model architecture.
Model Loading - tf.keras.models.load_model:
Example:
loaded_model = tf.keras.models.load_model('saved_model.h5')
Output:
Loading a pre-trained model from a saved model file.
Model Training - model.fit:
Example:
history = model.fit(train_data, train_labels, epochs=10, validation_data=(val_data, val_labels))
Output:
Training the model using provided data and monitoring training progress and validation metrics.
Fine-Tuning Pretrained Models - Transfer Learning:
Example:
base_model = tf.keras.applications.MobileNetV2(weights='imagenet', include_top=False)
base_model.trainable = False # Freeze base model layers
inputs = tf.keras.layers.Input(shape=(224, 224, 3))
x = base_model(inputs)
outputs = tf.keras.layers.Dense(10, activation='softmax')(x)
model = tf.keras.Model(inputs, outputs)
Output:
Building a new model by fine-tuning a pretrained MobileNetV2 model for a specific task.
Top comments (0)