TensorFlow Advanced API - Keras
Keras is a high-level neural network API written in Python, capable of running with TensorFlow, CNTK, or Theano as backends. Keras is designed to be user-friendly, modular, and easy to extend.
Main Features of Keras
- Simple and Easy to Use: provides an intuitive and consistent interface, suitable for rapid prototyping
- Modularity: neural network layers, loss functions, optimizers, etc. are all pluggable modules
- Easy to Extend: new modules can be easily added to express new research ideas
- Multi-Backend Support: can seamlessly run on TensorFlow, CNTK, or Theano
Core Concepts of Keras
1. Model (Model)
The core data structure of Keras is the model, which is the way to organize neural network layers. Keras provides two main types of models:
- Sequential Model: a linear stack of layers
- Functional API: a directed acyclic graph for building complex models
2. Layer (Layer)
A layer is the basic building block of Keras; each layer receives input data, performs some computation, and outputs the result. Keras provides a variety of predefined layers:
- Core layers: Dense, Activation, Dropout, etc.
- Convolution layers: Conv2D, MaxPooling2D, etc.
- Recurrent layers: LSTM, GRU, etc.
- Others: Embedding, BatchNormalization, etc.
3. Activation Function (Activation Function)
The activation function determines the output of a neuron; common ones include:
- ReLU (Rectified Linear Unit)
- Sigmoid
- Tanh
- Softmax (multi-class classification problems)
Basic Keras Workflow
1. Define the model
Example
from
tensorflow.
keras
.
models
import
Sequential
from
tensorflow.
keras
.
layers
import
Dense
model
=
Sequential
(
[
Dense
(
64
,
activation
=
'relu'
,
input_shape
=
(
784
,
)
)
,
Dense
(
64
,
activation
=
'relu'
)
,
Dense
(
10
,
activation
=
'softmax'
)
]
)
2. Compile the model
Example
model.
compile
(
optimizer
=
'adam'
,
loss
=
'categorical_crossentropy'
,
metrics
=
[
'accuracy'
]
)
3. Train the model
Example
model.
fit
(
x_train
,
y_train
,
epochs
=
5
,
batch_size
=
32
)
4. Evaluate the model
Example
loss_and_metrics
=
model.
evaluate
(
x_test
,
y_test
,
batch_size
=
128
)
5. Make predictions
Example
classes
=
model.
predict
(
x_test
,
batch_size
=
128
)
Detailed Explanation of Common Keras Layers
1. Dense Fully Connected Layer
Example
Dense
(
units
,
activation
=
None
,
use_bias
=
True
,
kernel_initializer
=
'glorot_uniform'
,
bias_initializer
=
'zeros'
)
units: positive integer, the dimension of the output spaceactivation: activation functionuse_bias: whether to use a bias vectorkernel_initializer: initializer for the weight matrixbias_initializer: initializer for the bias vector
2. Conv2D Two-Dimensional Convolution Layer
Example
Conv2D
(
filters
,
kernel_size
,
strides
=
(
1
,
1
)
,
padding
=
'valid'
,
activation
=
None
)
filters: the number of convolution kernelskernel_size: the size of the convolution kernelsstrides: convolution stridepadding: padding method ('valid' or 'same')
3. LSTM Long Short-Term Memory Network Layer
Example
LSTM
(
units
,
activation
=
'tanh'
,
recurrent_activation
=
'hard_sigmoid'
,
return_sequences
=
False
)
units: positive integer, the dimension of the output spaceactivation: activation functionrecurrent_activation: activation function for the recurrent stepreturn_sequences: whether to return the full sequence
Keras Model Saving and Loading
1. Save the entire model
Example
model.
save
(
'my_model.h5'
)
Save the architecture, weights, and training configuration
2. Save only the architecture
Example
json_string
=
model.
to_json
(
)
Save as JSON
yaml_string
=
model.
to_yaml
(
)
Save as YAML
3. Save only the weights
Example
model.
save_weights
(
'my_model_weights.h5'
)
4. Load the model
Example
from
tensorflow.
keras
.
models
import
load_model
model
=
load_model
(
'my_model.h5'
)
Load the complete model
Keras Callbacks
Callbacks are functions called at specific points during training, used for:
- Model checkpoints
- Early stopping
- Learning rate adjustment
- Logging, etc.
Common Callbacks
Example
from
tensorflow.
keras
.
callbacks
import
ModelCheckpoint
,
EarlyStopping
callbacks
=
[
ModelCheckpoint
(
filepath
=
'best_model.h5'
,
monitor
=
'val_loss'
,
save_best_only
=
True
)
,
EarlyStopping
(
monitor
=
'val_loss'
,
patience
=
3
)
]
model.
fit
(
x_train
,
y_train
,
epochs
=
10
,
callbacks
=
callbacks
,
validation_data
=
(
x_val
,
y_val
)
)
Keras Practical Example: MNIST Handwritten Digit Recognition
Example
from
tensorflow.
keras
.
datasets
import
mnist
from
tensorflow.
keras
.
models
import
Sequential
from
tensorflow.
keras
.
layers
import
Dense
,
Dropout
,
Flatten
from
tensorflow.
keras
.
layers
import
Conv2D
,
MaxPooling2D
from
tensorflow.
keras
.
utils
import
to_categorical
Load data
(
x_train
,
y_train
)
,
(
x_test
,
y_test
)
=
mnist.
load_data
(
)
Data preprocessing
x_train
=
x_train.
reshape
(
60000
,
28
,
28
,
1
)
.
astype
(
'float32'
)
/
255
x_test
=
x_test.
reshape
(
10000
,
28
,
28
,
1
)
.
astype
(
'float32'
)
/
255
y_train
=
to_categorical
(
y_train
,
10
)
y_test
=
to_categorical
(
y_test
,
10
)
Build the model
model
=
Sequential
(
[
Conv2D
(
32
,
kernel_size
=
(
3
,
3
)
,
activation
=
'relu'
,
input_shape
=
(
28
,
28
,
1
)
)
,
Conv2D
(
64
,
(
3
,
3
)
,
activation
=
'relu'
)
,
MaxPooling2D
(
pool_size
=
(
2
,
2
)
)
,
Dropout
(
0.25
)
,
Flatten
(
)
,
Dense
(
128
,
activation
=
'relu'
)
,
Dropout
(
0.5
)
,
Dense
(
10
,
activation
=
'softmax'
)
]
)
Compile the model
model.
compile
(
loss
=
'categorical_crossentropy'
,
optimizer
=
'adam'
,
metrics
=
[
'accuracy'
]
)
Train the model
model.
fit
(
x_train
,
y_train
,
batch_size
=
128
,
epochs
=
12
,
verbose
=
1
,
validation_data
=
(
x_test
,
y_test
)
)
Evaluate the model
score
=
model.
evaluate
(
x_test
,
y_test
,
verbose
=
0
)
(
'Test loss:'
,
score
[
0
]
)
(
'Test accuracy:'
,
score
[
1
]
)
Advanced Keras Techniques
1. Custom layers
Example
from
tensorflow.
keras
import
backend
as
K
from
tensorflow.
keras
.
layers
import
Layer
class
MyLayer
(
Layer
)
:
def
init
(
self
,
output_dim
,
**kwargs
)
:
self
.
output_dim
=
output_dim
super
(
MyLayer
,
self
)
.
init
(
**kwargs
)
def
build
(
self
,
input_shape
)
:
self
.
kernel
=
self
.
add_weight
(
name
=
'kernel'
,
shape
=
(
input_shape
[
1
]
,
self
.
output_dim
)
,
initializer
=
'uniform'
,
trainable
=
True
)
super
(
MyLayer
,
self
)
.
build
(
input_shape
)
def
call
(
self
,
x
)
:
return
K.
dot
(
x
,
self
.
kernel
)
def
compute_output_shape
(
self
,
input_shape
)
:
return
(
input_shape
[
0
]
,
self
.
output_dim
)
2. Custom loss functions
Example
from
tensorflow.
keras
import
backend
as
K
def
custom_loss
(
y_true
,
y_pred
)
:
return
K.
mean
(
K.
square
(
y_pred - y_true
)
,
axis
=
1
)
model.
compile
(
optimizer
=
'adam'
,
loss
=
custom_loss
)
3. Learning rate scheduling
Example
from
tensorflow.
keras
.
callbacks
import
LearningRateScheduler
def
scheduler
(
epoch
,
lr
)
:
if
epoch
<
10
:
return
lr
else
:
return
lr * K.
exp
(
0.1
)
callback
=
LearningRateScheduler
(
scheduler
)
model.
fit
(
x_train
,
y_train
,
epochs
=
15
,
callbacks
=
[
callback
]
)
Common Keras Problems and Solutions
1. Overfitting problems
- Add Dropout layers
- Use L1/L2 regularization
- Increase training data
- Use data augmentation
2. Slow training speed
- Increase the batch size
- Use a simpler model
- Try different optimizers
- Use GPU acceleration
3. Vanishing/exploding gradients
- Use BatchNormalization
- Use appropriate weight initialization
- Use non-saturating activation functions such as ReLU
- Use gradient clipping
Summary
As a high-level API of TensorFlow, Keras provides a simple and intuitive interface for building and training deep learning models. Through this article, you should have mastered:
- The core concepts and basic workflow of Keras
- How to use common layers
- Model saving and loading
- The use of callbacks
- Application examples in real projects
- Advanced techniques and solutions to common problems
The power of Keras lies in its flexibility and ease of use, making the development of deep learning models more efficient. As you gain more practice, you will be able to build more complex neural network models to solve various real-world problems.