iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

MLP

TensorFlow MLP: Sequential + Dense + Dropout. The simplest deep model, the workhorse baseline, the building block of everything.

TensorFlow — MLP

EXAMPLE
import tensorflow as tf
from tensorflow.keras import Sequential, layers

# ===== Define =====
model = Sequential([
    layers.Input(shape=(4,)),
    layers.Dense(64, activation='relu'),
    layers.Dropout(0.2),
    layers.Dense(64, activation='relu'),
    layers.Dropout(0.2),
    layers.Dense(3, activation='softmax'),
])

# Compile:
model.compile(
    optimizer=tf.keras.optimizers.Adam(1e-3),
    loss='sparse_categorical_crossentropy',
    metrics=['accuracy'],
)

model.summary()

# ===== Train =====
import numpy as np
X = np.random.rand(1000, 4).astype('float32'); y = np.random.randint(0, 3, 1000)
Xv = np.random.rand(200, 4).astype('float32'); yv = np.random.randint(0, 3, 200)

history = model.fit(
    X, y,
    validation_data=(Xv, yv),
    batch_size=32,
    epochs=20,
    callbacks=[
        tf.keras.callbacks.EarlyStopping(patience=5, restore_best_weights=True),
        tf.keras.callbacks.ReduceLROnPlateau(patience=3, factor=0.5),
    ],
    verbose=0,
)

# ===== Evaluate =====
loss, acc = model.evaluate(Xv, yv, verbose=0)
print(f'val_loss={loss:.3f} val_acc={acc:.3f}')

# ===== Predict =====
proba = model.predict(Xv, verbose=0)        # shape (N, 3)
pred = proba.argmax(axis=1)

# ===== Regression variant =====
reg = Sequential([
    layers.Input(shape=(4,)),
    layers.Dense(64, activation='relu'),
    layers.Dropout(0.2),
    layers.Dense(1),                         # no activation for regression
])
reg.compile(optimizer='adam', loss='mse', metrics=['mae'])

# ===== Binary classification =====
binc = Sequential([
    layers.Input(shape=(4,)),
    layers.Dense(64, activation='relu'),
    layers.Dense(1, activation='sigmoid'),    # 1 unit + sigmoid
])
binc.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy', tf.keras.metrics.AUC()])

# ===== Normalisation layer (inside the model) =====
norm = layers.Normalization()
norm.adapt(X)        # learns mean + variance from training data

model_with_norm = Sequential([
    norm,
    layers.Dense(64, activation='relu'),
    layers.Dense(3, activation='softmax'),
])
# Saved with the model; consistent at inference.

# ===== L2 regularisation =====
from tensorflow.keras import regularizers
layers.Dense(64, activation='relu', kernel_regularizer=regularizers.l2(1e-4))

# ===== Tuning depth / width =====
# Start small: [64, 32]
# Add depth before width
# Use dropout 0.1-0.3 in mid layers
# Watch validation gap for overfit signs

# ===== Functional API alternative (more flexible) =====
inputs = tf.keras.Input(shape=(4,))
x = layers.Dense(64, activation='relu')(inputs)
x = layers.Dropout(0.2)(x)
outputs = layers.Dense(3, activation='softmax')(x)
model_fn = tf.keras.Model(inputs, outputs)

# ===== Patterns to internalise =====
# - input_shape only on the first layer
# - Dropout 0.1-0.3 in mid layers; not on the input or output
# - Normalisation layer for tabular numeric features
# - Use validation_data + EarlyStopping every time

# ===== Pitfalls =====
# - Softmax + binary_crossentropy mismatch -> nan loss
# - Dropout left ON at inference (Keras handles this automatically with model())
# - Forgetting to scale features for sensitive optimisers (use Normalization layer)
# - Class imbalance ignored -> use class_weight in fit()

Why it matters

A Keras MLP is Sequential + Dense + Dropout + Normalisation + EarlyStopping. The baseline that every other model gets compared against. Master this, and the rest of deep learning is variations on the theme: convs for images, RNNs/transformers for sequences, but the training loop and discipline remain.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
from tensorflow.keras import layers, Sequential
model = Sequential([
    layers.Input((784,)),
    layers.Dense(256, activation='relu'),
    layers.Dropout(0.3),
    layers.Dense(10, activation='softmax'),
])
Try it Yourself »

Discussion

Loading…