Skip to main content

(AI #5) : Programming a Neural Network !!!

After discussing about the fundamentals of Neural Networks and Deep Learning, we have arrived to an exciting stage where we can learn how we program a Neural Network. 

I have created some simple programs to get some basic idea on how to program a Neural Network and I have used a Python library called PyTorch to program it.


Program 1 : Calculate total numbers of parameters in a neural network

Points to remember :

  • We need to import nn submodule from main module torch
  • We need to inherit the Module class available in torch.nn submodule
  • We should use self otherwise method doesn't get class objects data
  • Do not confuse about forward(), we don't call it directly, it will be called via constructor in super class. Hence using self.
  • p.numel() return the elements in the Model. Please see my explanation in the downloaded code
  • Need basics of Oops concepts in Python

# importing modules torch, nn(neural network): nn is a sub module in main module torch
import torch
import torch.nn as nn

class CalculateParams(nn.Module):
def __init__(self): # Constructor used to define layers and parameters
super().__init__() # Calls parent nn.Module constructor, it will register
our model so PyTorch can track prameters, load weights etc.
self.fc1 = nn.Linear(3, 5) # Fully connected layer1
self.fc2 = nn.Linear(5, 2) # Fully connected layer2

# forward() defines how a input flows through a network
# we never call it directly, it will be automatically called when we do
'output = Model(input)'
def forward(self, x):
x = self.fc1(x)
x = self.fc2(x)

return x

model = CalculateParams()

print()
# p.numel() returns number of elements in that tensor
total_params = sum(p.numel() for p in model.parameters())
print("Total parameters:", total_params)

for name, param in model.named_parameters():
print(name, param.shape, param.numel())

Output :
Total parameters: 32 fc1.weight torch.Size([5, 3]) 15 fc1.bias torch.Size([5]) 5 fc2.weight torch.Size([2, 5]) 10 fc2.bias torch.Size([2]) 2

GitHub location : https://github.com/amathe1/GenAI-AgenticAI-Hub/blob/main/Calulate_Params_in_Neural_Network.ipynb

Also, please find more explanation in the program once you download. Finally, it printed total number of parameters in the entire Neural Network. We could also see weight and bias values of our ML model.



Program 2 : House price prediction based on house size and no. of rooms

This is a full length NN programming with entire flow.

Points to remember :

  • In real time, we get the data either from files, cloud etc. and we need to segregate it properly
    • this is whole different activity!
    • there are ML teams dedicated only to do this activity, imagine the level of complexity and depth in segregating data
    • we need to remove outliers etc. before injecting this data to ML models
    • for ease of understand, we considered simple data X, Y(tensors)
    • X contains data like house size in sqft & no. of bedrooms, it have 5 records
    • Y represents actual price, we need to come close to these values using our ML model
  • Please ignore logic like model knows what is size and what is price, we need to understand the concept of co-relation for it, for now I recommend you to concentrate on the programming part and understand the logic behind a NN


import torch
import torch.nn as nn
import torch.optim as optim
# optim provides optimizers that update model
# weights so your neural network learns from data.

# Creating data sets
X = torch.tensor([
[800,2],
[1000,3],
[1200,3],
[1500,4],
[1800,4]
], dtype=torch.float32)

y = torch.tensor([[200],[260],[300],[360],[420]], dtype=torch.float32)


# Normalizing inputs
X = X / X.max(dim=0).values
y = y / y.max()


# Define model
model = nn.Sequential(
nn.Linear(2,8),
nn.ReLU(),
nn.Linear(8,1)
)


# Loss & Optimizer
criterion = nn.MSELoss()
optimizer = optim.Adam(model.parameters(), lr=0.01)


# Traning Loop
for epoch in range(500):
pred = model(X)
loss = criterion(pred,y)

optimizer.zero_grad()
loss.backward()
optimizer.step()

if epoch % 50 == 0:
print(f"Epoch {epoch}, Loss {loss.item():.4f}")



# Test Prediction
test = torch.tensor([[1600,4]], dtype=torch.float32)
test = test / X.max(dim=0).values

prob = model(test)
print("Pass probability:", prob.item())
print("Prediction:", 1 if prob>0.5 else 0)

print("Predicted price, Y :", model(test)*420)


Output :
Epoch 0, Loss 0.3705 Epoch 50, Loss 0.0028 Epoch 100, Loss 0.0011 Epoch 150, Loss 0.0007 Epoch 200, Loss 0.0006 Epoch 250, Loss 0.0005 Epoch 300, Loss 0.0005 Epoch 350, Loss 0.0004 Epoch 400, Loss 0.0004 Epoch 450, Loss 0.0003 Pass probability: 943.6936645507812 Prediction: 1 Predicted price, Y : tensor([[396351.3438]], grad_fn=<MulBackward0>)


Normalising input data for your reference (it is sample data, not from above programs input data X, Y)

Please download below program for more clarity on explanation. Observe that for 500 epochs, model started learning and came up with lower loss.


GitHub location : https://github.com/amathe1/GenAI-AgenticAI-Hub/blob/main/House_Price_Prediction.ipynb

Please do visit below location for understanding the basics of PyTorch :  https://github.com/amathe1/Neural_Networks/tree/main


Conclusion : 

I am coming up with more programs on building a ML model for different problem statements. Incase if you are interested, then please watch out this GitHub space : 

  • https://github.com/amathe1/GenAI-AgenticAI-Hub/tree/main
  • https://github.com/amathe1/Neural_Networks/tree/main


Thank you for reading this blog !

Arun Mathe

Comments

Popular posts from this blog

(AI #1) Deep Learning and Neural Networks

I was curious to learn Artificial Intelligence and thinking what is the best place to start learning, and then realized that Deep Learning and Neural Networks is the heart of AI. Hence started diving into AI from this point. Starting from today, I will write continuous blogs on AI, especially Gen AI & Agentic AI. Incase if you are interested on above topics then please watch out this space. What is Artificial Intelligence, Machine Learning & Deep Learning ? AI can be described as the effort to automate intellectual tasks normally performed by Humans. Is this really possible ? For example, when we see an image with our eyes, we will identify it within a fraction of milliseconds. Isn't it ? For a computer, is it possible to do the same within same time limit ? That's the power we are talking about. To be honest, things seems to be far advanced than we actually thing about AI.  BTW, starting from this blog, it is not just a technical journal, we talk about internals here. ...

(AI #3) Deep Learning Foundations - Activation & Loss Functions, Gradient Descent algorithms & Optimization techniques

It is extremely important to have a deep knowledge while designing a machine learning model, otherwise we will end up creating ML models which are of no use. We have to have a clear understanding on certain techniques to confidently build a ML model, train it using "training data", finalize the model and to deploy it in production. So far, from blog #1, #2, we have seen about the fundamentals of Deep Learning and Neural Network, architecture of a Neural Network, internal layers and components etc.  Providing the links of Blogs #1 , #2 below for quick reference. Deep Learning & Neural Networks : https://arunsdatasphere.blogspot.com/2026/01/deep-learning-and-neural-networks.html Building a real world neural network: A practical usecase explained : https://arunsdatasphere.blogspot.com/2026/01/building-real-world-neural-network.html Now let's dive through below concepts/criteria to help gaining confidence on building your ML model: Activation Functions (Forward Propaga...

(AI #2) Building a Real-World Neural Network: A Practical Use Case Explained

This blog will explain a clear picture on what will happen inside a Neural Network(NN).  But before going through NN, we need to have some knowledge on some of the basic concepts in Calculus(Maths) & architecture of a Neural Network.  Note :   I recommend you to read the following blog(link mentioned below) and then start reading this blog. Previous blog link :  https://arunsdatasphere.blogspot.com/2026/01/deep-learning-and-neural-networks.html   At-least try to  understand the basic layers of NN, weights, biases, activation function, loss function etc. Lets start with Derivatives. Derivatives :                      Derivatives are originally a core concept of calculus (maths) . They answer one question which is  “How fast is something changing?”  Why derivatives appear in Machine Learning ? Machine Learning uses Math as its foundation. In ML, derivatives help answer : If I sligh...