0% found this document useful (0 votes)
12 views

Ann 6

Uploaded by

Anushka Jadhav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Ann 6

Uploaded by

Anushka Jadhav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

In [1]: import numpy as np

# Define the sigmoid function for activation


def sigmoid(x):
return 1 / (1 + np.exp(-x))

# Define the derivative of sigmoid function


def sigmoid_derivative(x):
return x * (1 - x)

# Define the neural network class


class NeuralNetwork:

def __init__(self, x, y):


self.input = x
self.weights1 = np.random.rand(self.input.shape[1],4)
self.weights2 = np.random.rand(4,1)
self.y = y
self.output = np.zeros(self.y.shape)

def feedforward(self):
self.layer1 = sigmoid(np.dot(self.input, self.weights1))
self.output = sigmoid(np.dot(self.layer1, self.weights2))

def backprop(self):
d_weights2 = np.dot(self.layer1.T, (2*(self.y - self.output) * sigmoid_de
d_weights1 = np.dot(self.input.T, (np.dot(2*(self.y - self.output) * sigm
self.weights2.T) * sigmoid_der
self.weights1 += d_weights1
self.weights2 += d_weights2

# Define the training data


X = np.array([[0,0],[0,1],[1,0],[1,1]])
Y = np.array([[0],[1],[1],[0]])

# Initialize the neural network


nn = NeuralNetwork(X,Y)

# Train the neural network


for i in range(15000):
nn.feedforward()
nn.backprop()

# Print the output after training


print(nn.output)

[[0.01214789]
[0.99053993]
[0.98947693]
[0.00859045]]

In [ ]:

You might also like