Building an Image Generating AI from Scratch in Python — Powered by My Custom CalCulus Engine
Introduction
Artificial Intelligence has transformed how machines create images — from simple patterns to complex visuals. Most modern AI systems rely on large external libraries like TensorFlow, PyTorch, or NumPy. While those tools are powerful, they hide the actual mathematics behind abstraction layers.
This project takes a fundamentally different approach.
Instead of relying on external machine learning libraries, I built an image-generating AI completely from scratch in pure Python. Even more importantly, I created my own calculus engine — CalCulus — to handle derivatives, gradients, and optimization.
This allows the neural network to learn using fully self-implemented mathematics.
CalCulus Repository:
https://github.com/LegedsDaD/CalCulus
This represents true low-level AI engineering — building both the intelligence and the mathematics behind it.
The Role of Calculus in Artificial Intelligence
Calculus is the foundation of neural networks.
Neural networks learn by adjusting weights based on error. This adjustment requires computing derivatives of the loss function with respect to each weight.
This process is called backpropagation.
Backpropagation relies on:
- Derivatives
- Partial derivatives
- Chain rule
- Gradients
- Optimization
Without calculus, neural networks cannot learn.
Most frameworks automate this process. In this project, the calculus is implemented manually.
Introducing CalCulus — My Custom Calculus Engine
CalCulus is a Python-based calculus engine built entirely from scratch.
It provides the mathematical backbone required to train neural networks without external ML libraries.
Core features include:
- Derivative computation
- Gradient calculation
- Chain rule implementation
- Optimization support
- Neural network training support
Instead of relying on automatic differentiation libraries, CalCulus computes derivatives manually using custom logic.
This gives full transparency and control over the learning process.
CalCulus enables building true independent AI systems driven entirely by custom mathematics.
System Architecture Overview
The image generator consists of three main components:
- Matrix Engine
- Neural Network
- CalCulus Calculus Engine
Each plays a critical role.
Matrix Engine
Neural networks operate on matrices.
Since external libraries like NumPy are not used, matrix operations are implemented manually.
Example matrix structure:
class Matrix:
def __init__(self, rows, cols):
self.rows = rows
self.cols = cols
self.data = [[0 for _ in range(cols)] for _ in range(rows)]
Matrix operations implemented include:
- Matrix multiplication
- Matrix addition
- Bias addition
- Activation transformations
Matrices represent:
- Inputs
- Weights
- Hidden layer values
- Output images
Neural Network Generator
The generator is responsible for producing images from random noise.
Basic architecture:
Random Noise
↓
Hidden Layer
↓
Hidden Layer
↓
Output Layer
↓
Generated Image
Each layer performs the operation:
Output = Activation(Weights × Input + Bias)
Activation functions introduce non-linearity, allowing the network to learn complex patterns
Role of CalCulus in Training
Training requires adjusting weights based on error.
This process follows these steps:
1.Generate an image
2.Compare generated image to target data
3.Compute loss
4.Compute derivative of loss using CalCulus
5.Compute gradients
6.Update weights
7.Weight update formula:
weight = weight - learning_rate × derivative(loss)
CalCulus computes these derivatives manually.
This allows the network to learn without relying on external gradient engines.
Image Generation Process
The image generation pipeline works as follows:
Step 1: Generate random input noise
noise = random values
Step 2: Pass noise through neural network
Step 3: Convert output values to pixel range
pixel_value = output × 255
Step 4: Save output as image file
Images are saved in simple formats such as .ppm without external imaging libraries.
The result is a fully AI-generated image.
Training Loop
Training improves image quality over time.
Training cycle:
Generate Image
↓
Compute Error
↓
Calculate Derivatives using CalCulus
↓
Update Weights
↓
Repeat
Each iteration improves the generator's ability to produce structured images.
Advantages of Using CalCulus
1.CalCulus provides several key advantages:
2.Complete mathematical control
3.No dependency on external ML frameworks
4.Lightweight and efficient
5.Fully transparent training process
6.Educational clarity
7.Fully customizable derivative logic
8.It enables true independent AI development.