Keras is a high-level neural network API that runs on top of TensorFlow. It's designed to enable fast experimentation with deep neural networks and focuses on being user-friendly, modular, and extensible. Let's dive into the key components and features of the Keras API.
Keras offers two main types of models:
The Sequential model is the simplest, allowing you to stack layers one by one. Here's a basic example:
from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense model = Sequential([ Dense(64, activation='relu', input_shape=(10,)), Dense(32, activation='relu'), Dense(1, activation='sigmoid') ])
The Functional API is more flexible, allowing you to create models with complex architectures:
from tensorflow.keras.models import Model from tensorflow.keras.layers import Input, Dense inputs = Input(shape=(10,)) x = Dense(64, activation='relu')(inputs) x = Dense(32, activation='relu')(x) outputs = Dense(1, activation='sigmoid')(x) model = Model(inputs=inputs, outputs=outputs)
Keras provides a wide variety of built-in layers. Some common ones include:
Example of using different layers:
from tensorflow.keras.layers import Dense, Conv2D, LSTM, Dropout # Dense layer dense_layer = Dense(64, activation='relu') # Convolutional layer conv_layer = Conv2D(32, kernel_size=(3, 3), activation='relu') # LSTM layer lstm_layer = LSTM(64) # Dropout layer dropout_layer = Dropout(0.5)
Activation functions introduce non-linearity to the model. Keras offers various activation functions:
You can specify activations in layer definitions or use them separately:
from tensorflow.keras.layers import Dense from tensorflow.keras.activations import relu, sigmoid # In layer definition dense_layer = Dense(64, activation='relu') # Separate activation x = Dense(64)(inputs) x = relu(x)
Loss functions measure how well the model performs. Common loss functions include:
Example:
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
Optimizers adjust the model's weights to minimize the loss function. Popular optimizers include:
Example:
from tensorflow.keras.optimizers import Adam optimizer = Adam(learning_rate=0.001) model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy'])
Now that we've covered the core concepts, let's put them together to build and train a model:
import tensorflow as tf from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense from tensorflow.keras.optimizers import Adam # Create the model model = Sequential([ Dense(64, activation='relu', input_shape=(10,)), Dense(32, activation='relu'), Dense(1, activation='sigmoid') ]) # Compile the model model.compile(optimizer=Adam(learning_rate=0.001), loss='binary_crossentropy', metrics=['accuracy']) # Train the model history = model.fit(X_train, y_train, epochs=10, batch_size=32, validation_split=0.2)
After training, you can evaluate your model and make predictions:
# Evaluate the model test_loss, test_accuracy = model.evaluate(X_test, y_test) print(f"Test accuracy: {test_accuracy}") # Make predictions predictions = model.predict(X_new)
You can create custom layers by subclassing tf.keras.layers.Layer
:
class MyCustomLayer(tf.keras.layers.Layer): def __init__(self, units): super(MyCustomLayer, self).__init__() self.units = units def build(self, input_shape): self.w = self.add_weight(shape=(input_shape[-1], self.units), initializer='random_normal', trainable=True) self.b = self.add_weight(shape=(self.units,), initializer='zeros', trainable=True) def call(self, inputs): return tf.matmul(inputs, self.w) + self.b
Callbacks allow you to customize the training process:
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint early_stopping = EarlyStopping(patience=3, restore_best_weights=True) model_checkpoint = ModelCheckpoint('best_model.h5', save_best_only=True) history = model.fit(X_train, y_train, epochs=100, callbacks=[early_stopping, model_checkpoint])
You can use pre-trained models for transfer learning:
base_model = tf.keras.applications.MobileNetV2(input_shape=(224, 224, 3), include_top=False, weights='imagenet') base_model.trainable = False model = Sequential([ base_model, GlobalAveragePooling2D(), Dense(1, activation='sigmoid') ])
The Keras API in TensorFlow provides a powerful and flexible way to build, train, and evaluate neural networks. By understanding its core concepts and following best practices, you can create robust deep learning models for a wide range of applications.
06/12/2024 | Python
06/10/2024 | Python
05/10/2024 | Python
15/11/2024 | Python
15/10/2024 | Python
26/10/2024 | Python
14/11/2024 | Python
15/11/2024 | Python
06/10/2024 | Python
15/11/2024 | Python
15/11/2024 | Python
25/09/2024 | Python