Keras
Keras is a high-level neural network API that runs on top of TensorFlow. It was designed with a focus on fast experimentation: you can go from idea to work...
Building Neural Networks with Keras
Keras is a high-level neural network API that runs on top of TensorFlow. It was designed with a focus on fast experimentation: you can go from idea to working model in minutes. Since TensorFlow 2.0, Keras is the official TensorFlow interface and is imported directly from tensorflow.keras.
Keras offers three ways to build models:
1. Sequential API: simplest, for linear stacks of layers: model = keras.Sequential([ keras.layers.Dense(256, activation='relu'), keras.layers.BatchNormalization(), keras.layers.Dropout(0.4), keras.layers.Dense(10, activation='softmax') # 10-class classification ])
2. Functional API: for models with multiple inputs/outputs or branching: inputs = keras.Input(shape=(784,)) x = keras.layers.Dense(256, activation='relu')(inputs) outputs = keras.layers.Dense(10, activation='softmax')(x) model = keras.Model(inputs, outputs)
3. Subclassing API: for full control, inheriting from keras.Model for research-level customization.
Common layer types: - Dense: fully connected layer - Conv2D / MaxPooling2D: convolutional layers for images - LSTM / GRU: recurrent layers for sequences - Embedding: converts integer word indices to dense vectors - BatchNormalization: stabilizes training by normalizing activations - Dropout: randomly zeros activations to prevent overfitting
Callbacks add powerful training behaviors: callbacks = [ keras.callbacks.EarlyStopping(monitor='val_loss', patience=5, restore_best_weights=True), keras.callbacks.ReduceLROnPlateau(factor=0.5, patience=3), keras.callbacks.ModelCheckpoint('best_model.h5', save_best_only=True) ] model.fit(X_train, y_train, epochs=100, callbacks=callbacks, validation_split=0.2)
Activation functions and when to use them: relu for hidden layers, sigmoid for binary output, softmax for multi-class output, linear for regression output.
Practical tip: Always use EarlyStopping with restore_best_weights=True. This saves you from manually selecting the best epoch and prevents overfitting without tuning a fixed number of epochs.
Exam tip: BatchNormalization layers behave differently during training and inference. Keras handles this automatically when you pass training=True or training=False, but when writing custom training loops you must set this explicitly.