Neural network of one hidden layer with five neurons import numpy as np print("\n--------------------Simple neural network for XOR gat...
Neural network of one hidden layer with five neurons
import numpy as np
print("\n--------------------Simple neural network for XOR gate--------------------\n\n")
learning_rate = float(input("Enter leraning rate for model. Example 0.01\n"))
epochs = int(input("\nEnter no of epoches. Example 5000\n"))
x=np.array([[0,0,1,1],[0,1,0,1]])
y=np.array([[0,1,1,0]])
m = x.shape[1]
no_of_input = 2
no_of_output = 1
no_of_neurons = 5
np.random.seed(2)
w1 = np.random.rand(no_of_neurons,no_of_input) # Weight matrix for hidden layer
w2 = np.random.rand(no_of_output,no_of_neurons) # Weight matrix for output layer
b1 = np.zeros((no_of_neurons, 1))
b2 = np.zeros((no_of_output, 1))
# sigmoid activation function
def sigmoid(z):
z= 1/(1+np.exp(-z))
return z
# Forward propagation
def forward_propagation(w1, w2, x, b1, b2):
z1 = np.dot(w1,x) + b1
a1 = sigmoid(z1)
z2 = np.dot(w2,a1) + b2
a2 = sigmoid(z2)
return z1,a1,z2,a2
# Backward propagation
def backward_propagation(m, w1, w2, z1, a1, z2, a2, y):
dz2 = a2-y
dw2 = np.dot(dz2,a1.T)/m
db2 = np.sum(dz2, axis = 1, keepdims = True)
dz1 = np.dot(w2.T,dz2) * a1*(1-a1)
dw1 = np.dot(dz1,x.T)/m
dw1 = np.reshape(dw1,w1.shape)
db1 = np.sum(dz1, axis = 1, keepdims = True) / m
dw2 = np.reshape(dw2,w2.shape)
return dz2,dw2,dz1,dw1,db1,db2
for i in range(epochs):
z1,a1,z2,a2 = forward_propagation(w1,w2,x,b1,b2)
loss = -(1/m)*np.sum(y*np.log(a2)+(1-y)*np.log(1-a2))
da2,dw2,dz1,dw1,db1,db2 = backward_propagation(m,w1,w2,z1,a1,z2,a2,y)
w2 = w2-learning_rate*dw2
w1 = w1-learning_rate*dw1
b1 = b1-learning_rate*db1
b2 = b2-learning_rate*db2
output = np.where(a2 > 0.5, 1, 0) # setting threshold value 0.5 for prediction
print("Epoch--({}/{})------------------Loss--{}".format(i+1,epochs,loss))
print("\nHidden layer \nweights ----- {}\nbiases ----- {}".format(w1,b1))
print("\nOutput layer \nweights ----- {}\nbiases ----- {}".format(w2,b2))
print("\nPredicted ----- {}".format(output))
print("\n_______________________________________________________________________________________\n")
COMMENTS