HomeAI

Simple Neural Network For XOR Gate

 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")

Name

Accident Alert,1,AI,2,Array,1,Aurdino,1,C,2,Computer Graphics,9,Data Science,3,Dataset,1,Decoratot,1,Django,1,ESP32,1,Fixed point/iteration method,1,Greater or smaller,1,html,1,Image Processing,1,JAVA,1,Javascript,22,Machine Learning,1,Matlab,3,Numerical Method,13,OOP,1,Other,3,PHP,1,Point operation,1,Python,11,Raspberry pi,1,Recommendation System,1,Regression,1,Reservation System,1,Robotics,1,Simulation,2,sine wave,1,String Handling Function,1,Web scrap,1,Webpage,1,
ltr
item
COMPUTER PROGRAMMING: Simple Neural Network For XOR Gate
Simple Neural Network For XOR Gate
COMPUTER PROGRAMMING
https://computerprogram4ru.blogspot.com/2020/07/simple-neural-network-for-xor-gate.html
https://computerprogram4ru.blogspot.com/
https://computerprogram4ru.blogspot.com/
https://computerprogram4ru.blogspot.com/2020/07/simple-neural-network-for-xor-gate.html
true
8672391763020279633
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy