Deep Learning with Python
by François Chollet
Deep Learning with Python
Share this page
Getting Started with Keras
Introduction to Keras
Keras is a high-level neural network API written in Python. It provides a clean, intuitive interface for building and training deep learning models. Keras runs on top of TensorFlow, Theano, or CNTK, but TensorFlow is the most commonly used backend.
Why Keras?
- User-friendly: Simple and consistent API
- Modular: Easy to build complex models
- Extensible: Easy to write custom layers
- Fast: Optimized for both CPU and GPU
Installation
Install Keras and TensorFlow:
BASH
1
2 pip install tensorflow
# or with conda conda install tensorflow
Verify installation:
PYTHON
1
2 import tensorflow as tf
3 from tensorflow import keras
4 print(f"TensorFlow version: {tf.__version__}")
5 print(f"Keras version: {keras.__version__}")
Building Your First Model
The Sequential API
The simplest way to build models in Keras:
PYTHON
1
2 from tensorflow import keras
3 from tensorflow.keras import layers
4
5 # Create a sequential model
6 model = keras.Sequential([
7 layers.Dense(128, activation='relu', input_shape=(784,)),
8 layers.Dense(64, activation='relu'),
9 layers.Dense(10, activation='softmax')
10 ])
11
12 # Display model architecture
13 model.summary()
The Functional API
For more complex architectures:
PYTHON
1
2 inputs = keras.Input(shape=(784,))
3 x = layers.Dense(128, activation='relu')(inputs)
4 x = layers.Dense(64, activation='relu')(x)
5 outputs = layers.Dense(10, activation='softmax')(x)
6
7 model = keras.Model(inputs=inputs, outputs=outputs)
8 model.summary()
Compiling the Model
Configure the learning process:
PYTHON
1
2 model.compile(
3 optimizer='adam',
4 loss='sparse_categorical_crossentropy',
5 metrics=['accuracy']
6 )
Common optimizers:
- SGD
- RMSprop
- Adam
- Adagrad
Training the Model
Fit the model to training data:
PYTHON
1
2 history = model.fit(
3 x_train, y_train,
4 batch_size=32,
5 epochs=10,
6 validation_data=(x_val, y_val)
7 )
Evaluating the Model
Assess model performance:
PYTHON
1
2 # Evaluate on test data
3 test_loss, test_acc = model.evaluate(x_test, y_test)
4 print(f"Test accuracy: {test_acc:.4f}")
5
6 # Make predictions
7 predictions = model.predict(x_test)
8 predicted_classes = predictions.argmax(axis=1)
Working with Data
Data Preprocessing
PYTHON
1
2 from tensorflow.keras.preprocessing import image
3
4 # Load and preprocess images
5 train_datagen = image.ImageDataGenerator(
6 rescale=1./255,
7 rotation_range=20,
8 width_shift_range=0.2,
9 height_shift_range=0.2,
10 horizontal_flip=True
11 )
12
13 train_generator = train_datagen.flow_from_directory(
14 'data/train',
15 target_size=(150, 150),
16 batch_size=32,
17 class_mode='binary'
18 )
Text Data Processing
PYTHON
1
2 from tensorflow.keras.preprocessing.text import Tokenizer
3 from tensorflow.keras.preprocessing.sequence import pad_sequences
4
5 # Tokenize text
6 tokenizer = Tokenizer(num_words=10000)
7 tokenizer.fit_on_texts(texts)
8 sequences = tokenizer.texts_to_sequences(texts)
9
10 # Pad sequences
11 padded_sequences = pad_sequences(sequences, maxlen=100)
Common Layer Types
Dense Layers
Fully connected layers:
PYTHON
1
2 layers.Dense(128, activation='relu')
Convolutional Layers
For image processing:
PYTHON
1
2 layers.Conv2D(32, (3, 3), activation='relu', padding='same')
3 layers.MaxPooling2D((2, 2))
Recurrent Layers
For sequence data:
PYTHON
1
2 layers.LSTM(64, return_sequences=True)
3 layers.GRU(32)
Dropout Layers
For regularization:
PYTHON
1
2 layers.Dropout(0.5)
Batch Normalization
For stable training:
PYTHON
1
2 layers.BatchNormalization()
Saving and Loading Models
Save the entire model:
PYTHON
1
2 model.save('my_model.h5')
Load the model:
PYTHON
1
2 loaded_model = keras.models.load_model('my_model.h5')
Save only weights:
PYTHON
1
2 model.save_weights('my_weights.h5')
3 model.load_weights('my_weights.h5')
Callbacks
Monitor and control training:
PYTHON
1
2 from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint
3
4 callbacks = [
5 EarlyStopping(patience=3, restore_best_weights=True),
6 ModelCheckpoint('best_model.h5', save_best_only=True)
7 ]
8
9 model.fit(
10 x_train, y_train,
11 epochs=100,
12 callbacks=callbacks
13 )
Best Practices
- Start Simple: Begin with simple models
- Monitor Metrics: Track loss and accuracy
- Use Validation: Always have a validation set
- Regularize: Prevent overfitting early
- Experiment: Try different architectures
- Document: Keep track of experiments
Troubleshooting
Common issues and solutions:
- Model not learning
- Check learning rate
- Verify data preprocessing
- Ensure correct loss function
- Overfitting
- Add dropout
- Use regularization
- Get more data
- Slow training
- Use GPU
- Reduce batch size
- Optimize data pipeline
- Memory issues
- Reduce batch size
- Use data generators
- Implement gradient checkpointing