p5
p5
2024-2025 Practical-5
Practical-5
Aim: Write a python program to create a simple 3-layer neural network for implementation of binary
function.
3 Layers: Input Layer, 1 Hidden Layer, 1 Output Layer
Create neural_network class
For input of Boolean function: use two input and 3 neurons in hidden layer
Boolean Function: XOR
Use Sigmoid function as activation function in all neurons
Choose appropriate error function as Loss Function and compare
Initialize weight and bias randomly in range of (-1, 1)
Use Backpropagation algorithm to train neural network
Don’t use any python package except numpy
Print all parameters (Weight and bias) after training
Print output of neural network after training for all possible inputs
Code:
import numpy as np
class NeuralNetwork:
def __init__(self):
self.input_size = 2
self.hidden_size = 3
self.output_size = 1
return 1 / (1 + np.exp(-x))
return x * (1 - x)
# Forward pass
self.hout = self.sigmoid(self.hin)
self.oout = self.sigmoid(self.oin)
return self.oout
# Forward pass
y_pred = self.forward(X)
# Calculate loss
# Backpropagation
return self.forward(X)
nn = NeuralNetwork()
learning_rate = 0.9
epochs = 10000
for i in range(len(X)):
Output: