A Tiny Keras Model
See how few lines it takes to define and train a small neural network.
Syntax
keras.Sequential([...])High-level frameworks like Keras (part of TensorFlow) make defining a network almost as short as scikit-learn.
Three steps
- Define the layers with
Sequential. - Compile with a loss and optimiser.
- Fit on your data.
The code below is conceptual — install tensorflow to run it.
Example
from tensorflow import keras
from tensorflow.keras import layers
model = keras.Sequential([
layers.Dense(16, activation='relu', input_shape=(4,)),
layers.Dense(8, activation='relu'),
layers.Dense(1, activation='sigmoid'),
])
model.compile(optimizer='adam',
loss='binary_crossentropy',
metrics=['accuracy'])
# model.fit(X_train, y_train, epochs=10, batch_size=32)
model.summary()When to use it
- A beginner data scientist trains their first neural network on the MNIST digit dataset using Keras in under 15 lines, getting above 97% accuracy.
- A prototyping team uses Keras' Sequential API to build and compare three small network architectures in a single afternoon before committing to one.
- An ML educator uses Keras to demonstrate neural network training to students because the compile/fit/evaluate pattern mirrors the scikit-learn API.
More examples
Minimal Keras classifier
Defines, compiles, trains, and evaluates a tiny Keras classifier on Iris data in under 15 lines, showing the complete compile-fit-evaluate workflow.
from tensorflow import keras
from tensorflow.keras import layers
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
X, y = load_iris(return_X_y=True)
X = StandardScaler().fit_transform(X)
X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.2)
model = keras.Sequential([
layers.Dense(16, activation='relu', input_shape=(4,)),
layers.Dense(3, activation='softmax')
])
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.fit(X_tr, y_tr, epochs=30, verbose=0)
print('Test accuracy:', model.evaluate(X_te, y_te, verbose=0)[1])Inspect model architecture
Calls model.summary() to print each layer's name, output shape, and parameter count, giving a quick architectural overview.
from tensorflow import keras
from tensorflow.keras import layers
model = keras.Sequential([
layers.Dense(64, activation='relu', input_shape=(20,)),
layers.Dropout(0.3),
layers.Dense(32, activation='relu'),
layers.Dense(1, activation='sigmoid')
])
model.summary()Add validation data to training
Passes validation_data to fit so Keras reports both training and validation loss each epoch, making it easy to monitor overfitting during training.
from tensorflow import keras
from tensorflow.keras import layers
import numpy as np
X_tr = np.random.randn(800, 10)
y_tr = (np.random.randn(800) > 0).astype(int)
X_val = np.random.randn(200, 10)
y_val = (np.random.randn(200) > 0).astype(int)
model = keras.Sequential([
layers.Dense(32, activation='relu', input_shape=(10,)),
layers.Dense(1, activation='sigmoid')
])
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
history = model.fit(X_tr, y_tr, epochs=10, validation_data=(X_val, y_val), verbose=1)
Discussion