Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Shaheed Zulfikar Ali Bhutto Institute of Science &

Technology

COMPUTER SCIENCE DEPARTMENT

Total Marks: 04

Obtained Marks:

Artificial Intelligence
Lab Task ANN

Submitted To: Sir Naveed

Student Name: Ahmad ali

Reg Number: 2112156

AI BS(CS)-6B SZABIST-ISB
Shaheed Zulfikar Ali Bhutto Institute of Science &
Technology

COMPUTER SCIENCE DEPARTMENT

Lab task ANN


CODE:

import numpy as np

class NeuralNetwork:
def init (self, input_size, hidden_size1, hidden_size2, output_size):
# Initialize weights and biases
self.weights_input_hidden1 = np.random.randn(input_size, hidden_size1)
self.bias_input_hidden1 = np.zeros((1, hidden_size1))
self.weights_hidden1_hidden2 = np.random.randn(hidden_size1, hidden_size2)
self.bias_hidden1_hidden2 = np.zeros((1, hidden_size2))
self.weights_hidden2_output = np.random.randn(hidden_size2, output_size)
self.bias_hidden2_output = np.zeros((1, output_size))

def forward(self, inputs):


# Forward pass
hidden1_output = np.dot(inputs, self.weights_input_hidden1) + self.bias_input_hidden1
hidden1_activation = self.sigmoid(hidden1_output)

hidden2_output = np.dot(hidden1_activation, self.weights_hidden1_hidden2) +


self.bias_hidden1_hidden2
hidden2_activation = self.sigmoid(hidden2_output)

output = np.dot(hidden2_activation, self.weights_hidden2_output) +


self.bias_hidden2_output
output_activation = self.sigmoid(output)

return output_activation

def sigmoid(self, x):


return 1 / (1 + np.exp(-x))

# Example usage:
input_size = 2
hidden_size1 = 4
hidden_size2 = 3
output_size = 1

# Create neural network


model = NeuralNetwork(input_size, hidden_size1, hidden_size2, output_size)

# Input data

AI BS(CS)-6B SZABIST-ISB
Shaheed Zulfikar Ali Bhutto Institute of Science &
Technology

COMPUTER SCIENCE DEPARTMENT

inputs = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])

# Forward pass
output = model.forward(inputs)
print("Output:")
print(output)

OUTPUT:

AI BS(CS)-6B SZABIST-ISB

You might also like