Skip to content

About

A neural network from scratch in C; a learning exercise.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 

Repository files navigation

Neural Network in C

A simple, beginner-friendly neural network implementation in pure C that predicts house prices based on features like number of rooms, square footage, and distance to market.

🧠 What is This?

This is a complete neural network implementation from scratch in C, with no external dependencies. The code is extensively commented to help beginners understand every concept, from activation functions to backpropagation.

Network Architecture

Input Layer (3 neurons) → Hidden Layer (4 neurons) → Output Layer (1 neuron)
     [rooms]                    [ReLU]                  [price]
     [sqft]                   activation
   [distance]

📋 Features

  • Pure C implementation - No ML libraries required
  • Heavily commented - Every line explained for beginners
  • Forward propagation - Making predictions
  • Backpropagation - Learning from mistakes
  • ReLU activation - Non-linear activation function
  • Gradient descent - Optimization algorithm
  • Data normalization - Preprocessing for better training

🏠 Training Data

The network learns from 8 sample houses:

Rooms Sqft Distance (km) Price ($k)
3 1500 2.0 300
4 2000 1.5 400
2 1000 3.0 200
5 2500 1.0 500
3 1800 2.5 350
4 2200 1.2 450
2 900 4.0 180
6 3000 0.8 600

🚀 Getting Started

Compile

gcc neural_network.c -o neural_network -lm

Run

./neural_network

Expected Output

Training Neural Network for House Price Prediction
Architecture: 3 → 4 → 1

Epoch 0, Loss: X.XXXX
Epoch 100, Loss: X.XXXX
...

=== Testing Predictions ===
Sample 1: Predicted $XXX.XXk, Actual $300.00k
...

=== New Prediction ===
House: 3 rooms, 1600 sqft, 1.8km from market
Predicted price: $XXX.XXk

📚 Key Concepts Explained

Forward Propagation

The process of passing inputs through the network to get a prediction:

  1. Multiply inputs by weights
  2. Add biases
  3. Apply activation function (ReLU)
  4. Repeat for each layer

Backpropagation

How the network learns from mistakes:

  1. Calculate error (prediction - actual)
  2. Propagate error backwards through layers
  3. Adjust weights to reduce future errors

ReLU (Rectified Linear Unit)

A simple activation function:

  • If input > 0: output = input
  • If input ≤ 0: output = 0

Normalization

Scaling all inputs to 0-1 range so features with larger values don't dominate training.

⚙️ Hyperparameters

Parameter Value Description
INPUT_SIZE 3 Number of input features
HIDDEN_SIZE 4 Neurons in hidden layer
OUTPUT_SIZE 1 Number of outputs
LEARNING_RATE 0.0001 Step size for gradient descent
EPOCHS 1000 Training iterations

🙏 Credits

Original code by @TheVixhal

Source: Original Tweet

Comments and documentation added for educational purposes.

📄 License

This project is shared for educational purposes. Please credit the original author @TheVixhal if you use or modify this code.

About

A neural network from scratch in C; a learning exercise.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages