Overview
TheCreditScoreModel is a fully connected neural network built with PyTorch for binary credit risk classification. It implements a flexible architecture with configurable hidden layers, activation functions, batch normalization, and dropout regularization.
Class Definition
Parameters
Configuration object that defines the model architecture, including:
input_size: Number of input featureshidden_layers: List of hidden layer dimensionsactivation_functions: List of activation function namesoutput_size: Number of output neurons (default: 1)dropout_rate: Dropout probability for regularization
Architecture
The model automatically constructs a sequential architecture with the following components for each hidden layer:- Linear Layer: Fully connected transformation
- Batch Normalization: Normalizes activations (mean=0, std=1) for stable training
- Activation Function: Non-linear transformation (ReLU, LeakyReLU, GELU, Sigmoid, Softmax, or Tanh)
- Dropout: Regularization to prevent overfitting
The model validates that the number of hidden layers matches the number of activation functions at initialization.
Weight Initialization
The model uses optimal weight initialization strategies based on the activation function:- He/Kaiming Initialization: For ReLU, LeakyReLU, and GELU activations
- Xavier/Glorot Initialization: For Sigmoid, Tanh, and Softmax activations
- Bias: Always initialized to zeros
Methods
forward()
Performs the forward pass through the network.Input tensor of shape
(batch_size, input_size)Raw logits of shape
(batch_size, output_size) before sigmoid activationExample
predict_probability()
Computes class probabilities for both Bad (0) and Good (1) credit risk.Input tensor of shape
(batch_size, input_size)Tensor of shape
(batch_size, 2) containing:- Column 0: Probability of Bad credit (Class 0)
- Column 1: Probability of Good credit (Class 1)
This method runs in inference mode with gradients disabled (
torch.no_grad()).Example
binary_prediction()
Produces binary credit risk predictions using a probability threshold.Input tensor of shape
(batch_size, input_size)Decision threshold for binary classification. Predictions with probability >= threshold are classified as Good (1), otherwise Bad (0).
Binary predictions of shape
(batch_size, 1) where:- 0 = Bad credit risk
- 1 = Good credit risk
Example
get_model_summary()
Returns a string representation of the model architecture.String describing all layers in the sequential model
Example
Training Example
Source Reference
Implementation:python-projects/credit-score/model/model.py:58-151