This document provides a detailed explanation of a single-neuron linear regression model implemented from scratch in a C# Console Application.
| Category | Detail |
|---|---|
| Model Type | Linear Regression |
| Learning Algorithm | Stochastic Gradient Descent (SGD) |
| Architecture | Single Neuron (Perceptron without an Activation Function) |
| Function | Linear mapping: |
| Goal | To learn the weights ( |
The code demonstrates the fundamental machine learning process:
- Training: Trains a linear model to fit a set of input-output pairs by learning the optimal weights and bias.
- Optimization: Uses Gradient Descent to iteratively adjust these parameters to reduce the prediction error (specifically, the Mean Squared Error).
- Prediction: After training, the model uses the learned parameters to make predictions on new, previously unseen input values.
| Element | Detail | Purpose |
|---|---|---|
inputs |
2D array of 9 samples, 3 features each (e.g., |
Provides the independent variables used to train the model. |
outputs |
1D array of 9 target values. | Provides the dependent (true) values the model must learn to predict. |
-
weights(3 values): Initialized randomly. Each weight corresponds to one input feature ($x_1, x_2, x_3$ ). -
bias(1 value): Initialized randomly.
Why Random? Random initialization prevents the symmetry problem, ensuring that all three weights start with unique values and can learn different contributions during training.
The training is controlled by two nested loops:
- Outer Loop (
epochs = 1000): Repeats the learning process 1000 times over the entire dataset. - Inner Loop (Per Sample): Iterates through each of the 9 training samples.
| Training Step | Formula / Concept | Explanation |
|---|---|---|
| Predict | Performs the Forward Pass: Calculates the current output based on the input and the model's current parameters. | |
| Error | Calculates the residual, which is the direct measure of how wrong the current prediction is. | |
| Weights Update |
Gradient Descent: Adjusts the weight by moving in the direction that decreases the loss, scaled by the learningRate ( |
|
| Bias Update | Adjusts the model's vertical offset based on the average error. | |
| Loss | Accumulates the Squared Error to monitor the model's performance over the epoch. |
- After the 1000 epochs, the final learned weights and bias are used to predict outputs for two new input vectors:
${5, 5, 5}$ and${20, 5, 6}$ . - The model generalizes the learned relationship (the weighted sum) to make predictions on unseen data.
| Function | Core Calculation | Role |
|---|---|---|
Predict |
Weighted Sum + Bias | The mathematical core of the model; computes the linear relationship. |
Loss |
Squared Error | The Cost Function; measures the penalty for poor predictions. |