def train_feedforward_model(
model: keras.Model,
X_train: np.ndarray,
y_train: np.ndarray,
X_val: np.ndarray,
y_val: np.ndarray
) -> keras.callbacks.History:
"""Train a feed-forward model with proper configuration.
Trains a feed-forward model using Adam optimizer with callbacks for early stopping
and learning rate reduction on plateau.
Args:
model: Compiled Keras model to train.
X_train: Training feature array of shape (n_samples, n_features).
y_train: Training target array of shape (n_samples, n_classes).
X_val: Validation feature array of shape (n_val_samples, n_features).
y_val: Validation target array of shape (n_val_samples, n_classes).
Returns:
keras.callbacks.History: Training history object containing loss and metrics per epoch.
Example:
```python
import numpy as np
import keras
X_train = np.random.rand(100, 20)
y_train = np.zeros((100, 3))
y_train[np.arange(100), np.random.randint(0, 3, 100)] = 1
X_val = np.random.rand(20, 20)
y_val = np.zeros((20, 3))
y_val[np.arange(20), np.random.randint(0, 3, 20)] = 1
model = create_basic_feedforward(input_dim=20, num_classes=3)
history = train_feedforward_model(model, X_train, y_train, X_val, y_val)
```
"""
# Compile model
model.compile(
optimizer=keras.optimizers.Adam(learning_rate=0.001),
loss='categorical_crossentropy',
metrics=['accuracy']
)
# Callbacks
callbacks = [
keras.callbacks.EarlyStopping(
monitor='val_loss',
patience=10,
restore_best_weights=True
),
keras.callbacks.ReduceLROnPlateau(
monitor='val_loss',
factor=0.5,
patience=5
)
]
logger.info("Starting model training...")
# Train
history = model.fit(
X_train, y_train,
validation_data=(X_val, y_val),
epochs=100,
batch_size=32,
callbacks=callbacks,
verbose=1
)
logger.info("Model training completed.")
return history