Model Training Fundamentals
Cours
Fundamentals
Essential workflow for training neural networks in Keras, covering the basic steps from model creation to prediction.
Training Your First Model
Section 1.40 - The Training Workflow
Four Essential Steps
- Create the model
- Compile it with optimizer and loss
- Fit to training data
- Make predictions
Complete Training Example
import numpy as np
from keras import Sequential, layers
# Generate sample data
X_train = np.random.normal(size=(1000, 20)) # 1000 samples, 20 features
y_train = (X_train.sum(axis=1) > 0).astype(int) # Binary classification
# Step 1: Create model
model = Sequential([
layers.Dense(64, activation='relu', input_shape=(20,)),
layers.Dense(32, activation='relu'),
layers.Dense(1, activation='sigmoid')
])
# Step 2: Compile
model.compile(
optimizer='adam', # Algorithm to update weights
loss='binary_crossentropy', # How to measure errors
metrics=['accuracy'] # What to report during training
)
# Step 3: Fit
history = model.fit(
X_train, y_train,
epochs=10, # Number of training passes
batch_size=32, # Samples per gradient update
validation_split=0.2 # 20% of data for validation
)
# Step 4: Predict
X_new = np.random.normal(size=(100, 20)) # New data
predictions = model.predict(X_new) # Get model outputsSection 1.41 - Key Parameters Explained
Compilation Options
model.compile(
optimizer='adam', # Common choices: 'adam', 'sgd', 'rmsprop'
loss='mse', # For regression: 'mse', 'mae'
# For classification: 'binary_crossentropy', 'categorical_crossentropy'
metrics=['accuracy'] # What to track: 'accuracy', 'mae', custom metrics
)Training Parameters
model.fit(
X_train, y_train,
epochs=10, # More epochs = more training (sometimes too much)
batch_size=32, # Smaller = slower but more stable
validation_split=0.2, # Hold out data to check performance
shuffle=True, # Mix data between epochs
verbose=1 # 0=silent, 1=progress bar, 2=one line per epoch
)Section 1.42 - Quick Tips
Common Gotchas
- Input Shape:
- Must match your data
- Include batch dimension:
(batch_size, features)
- Data Types:
- Convert inputs to float32:
X_train = X_train.astype('float32') - Classification targets to int32:
y_train = y_train.astype('int32')
- Convert inputs to float32:
- Validation:
- Always use validation data to check for overfitting
- Either
validation_splitor separatevalidation_data=(X_val, y_val)
Note
We’ll dive deeper into:
- Loss function selection
- Optimizer tuning
- Batch size effects
- Validation strategies
- Overfitting prevention
in later sections of the course.
Section 1.43 - Task-Specific Templates
Regression (Predict Numbers)
model = Sequential([
layers.Dense(64, activation='relu', input_shape=(n_features,)),
layers.Dense(1) # No activation for regression
])
model.compile(optimizer='adam', loss='mse', metrics=['mae'])Binary Classification (Yes/No)
model = Sequential([
layers.Dense(64, activation='relu', input_shape=(n_features,)),
layers.Dense(1, activation='sigmoid') # Output between 0 and 1
])
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])Multiclass Classification (Categories)
model = Sequential([
layers.Dense(64, activation='relu', input_shape=(n_features,)),
layers.Dense(n_classes, activation='softmax') # One probability per class
])
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
Important
These templates provide starting points - you’ll learn to customize them as we progress through the course.