-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNAND.py
66 lines (44 loc) · 1.82 KB
/
NAND.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python
# coding: utf-8
# In[2]:
import numpy as np
def sigmoid (x):
return 1/(1 + np.exp(-x))
def sigmoid_derivative(x):
return x * (1 - x)
#Input datasets
inputs = np.array([[0,0],[0,1],[1,0],[1,1]])
expected_output = np.array([[1],[1],[1],[0]])
def NAND():
epochs = 50000
lr = 0.1
inputLayerNeurons, hiddenLayerNeurons, outputLayerNeurons = 2,2,1
#Random weights and bias initialization
hw = np.random.uniform(size=(inputLayerNeurons,hiddenLayerNeurons))
hb =np.random.uniform(size=(1,hiddenLayerNeurons))
ow = np.random.uniform(size=(hiddenLayerNeurons,outputLayerNeurons))
ob = np.random.uniform(size=(1,outputLayerNeurons))
#Training algorithm
for _ in range(epochs):
# Forward Propagation
hidden_layer_activation = np.dot(inputs,hw)
hidden_layer_activation += hb
hidden_layer_output = sigmoid(hidden_layer_activation)
output_layer_activation = np.dot(hidden_layer_output,ow)
output_layer_activation += ob
predicted_output = sigmoid(output_layer_activation)
# Backpropagation
error = expected_output - predicted_output
d_predicted_output = error * sigmoid_derivative(predicted_output)
error_hidden_layer = d_predicted_output.dot(ow.T)
d_hidden_layer = error_hidden_layer * sigmoid_derivative(hidden_layer_output)
# Updating Weights and Biases
ow += hidden_layer_output.T.dot(d_predicted_output) * lr
ob += np.sum(d_predicted_output,axis=0,keepdims=True) * lr
hw += inputs.T.dot(d_hidden_layer) * lr
hb += np.sum(d_hidden_layer,axis=0,keepdims=True) * lr
return predicted_output
print('Output of the NAND Gate',*expected_output)
print("Predicted output from neural network after given epochs: ",*NAND())
print('\n')
# In[ ]: