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
= np.random.normal(size=(1000, 20)) # 1000 samples, 20 features
X_train = (X_train.sum(axis=1) > 0).astype(int) # Binary classification
y_train
# Step 1: Create model
= Sequential([
model 64, activation='relu', input_shape=(20,)),
layers.Dense(32, activation='relu'),
layers.Dense(1, activation='sigmoid')
layers.Dense(
])
# Step 2: Compile
compile(
model.='adam', # Algorithm to update weights
optimizer='binary_crossentropy', # How to measure errors
loss=['accuracy'] # What to report during training
metrics
)
# Step 3: Fit
= model.fit(
history
X_train, y_train,=10, # Number of training passes
epochs=32, # Samples per gradient update
batch_size=0.2 # 20% of data for validation
validation_split
)
# Step 4: Predict
= np.random.normal(size=(100, 20)) # New data
X_new = model.predict(X_new) # Get model outputs predictions
Section 1.41 - Key Parameters Explained
Compilation Options
compile(
model.='adam', # Common choices: 'adam', 'sgd', 'rmsprop'
optimizer='mse', # For regression: 'mse', 'mae'
loss# For classification: 'binary_crossentropy', 'categorical_crossentropy'
=['accuracy'] # What to track: 'accuracy', 'mae', custom metrics
metrics )
Training Parameters
model.fit(
X_train, y_train, =10, # More epochs = more training (sometimes too much)
epochs=32, # Smaller = slower but more stable
batch_size=0.2, # Hold out data to check performance
validation_split=True, # Mix data between epochs
shuffle=1 # 0=silent, 1=progress bar, 2=one line per epoch
verbose )
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_split
or 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)
= Sequential([
model 64, activation='relu', input_shape=(n_features,)),
layers.Dense(1) # No activation for regression
layers.Dense(
])compile(optimizer='adam', loss='mse', metrics=['mae']) model.
Binary Classification (Yes/No)
= Sequential([
model 64, activation='relu', input_shape=(n_features,)),
layers.Dense(1, activation='sigmoid') # Output between 0 and 1
layers.Dense(
])compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) model.
Multiclass Classification (Categories)
= Sequential([
model 64, activation='relu', input_shape=(n_features,)),
layers.Dense(='softmax') # One probability per class
layers.Dense(n_classes, activation
])compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) model.
Important
These templates provide starting points - you’ll learn to customize them as we progress through the course.