Last active
June 25, 2017 03:45
-
-
Save prakritidev/27b0bbb53a1e259667c7d0e8013d4d45 to your computer and use it in GitHub Desktop.
XOR using Neural networks Using Numpy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
----------------Hidden Layer Weights---------------- | |
[[ 0.58249636 0.02403689 0.72590671] | |
[ 0.91357442 0.02758776 0.57130346]] | |
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx | |
----------------Output Layer Weights---------------- | |
[[ 0.55554919] | |
[ 0.72138457] | |
[ 0.31158356]] | |
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx | |
----------------Hidden Layer Output---------------- | |
[[ 0.5 0.5 0.5 ] | |
[ 0.71373104 0.5068965 0.63906389] | |
[ 0.64164162 0.50600893 0.67390639] | |
[ 0.81698772 0.5129033 0.78536508]] | |
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx | |
----------------Output---------------- | |
[[ 0.79425866] | |
[ 0.96130182] | |
[ 0.93146866] | |
[ 1.06858423]] | |
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx | |
----------------Error---------------- | |
[[-0.79425866] | |
[ 0.03869818] | |
[ 0.06853134] | |
[-1.06858423]] | |
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx | |
----------------Change in Error---------------- | |
[[-0.07942587] | |
[ 0.00386982] | |
[ 0.00685313] | |
[-0.10685842]] | |
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx | |
----------------Change in Output_Weights---------------- | |
[[ 0.4356935 ] | |
[ 0.63229294] | |
[ 0.19503918]] | |
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx | |
----------------Change in Hidden result---------------- | |
[[-0.00865133 -0.0125551 -0.00387279] | |
[ 0.00034449 0.0006116 0.0001741 ] | |
[ 0.00068656 0.00108314 0.00029373] | |
[-0.00696122 -0.01688021 -0.0035132 ]] | |
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx | |
----------------Change in Hidden Weights---------------- | |
[[ 0.5762217 0.00823982 0.72268724] | |
[ 0.90695769 0.01131915 0.56796436]] | |
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx | |
----------------Hidden Layer Output---------------- | |
[[ 0.5 0.5 0.5 ] | |
[ 0.71237721 0.50282976 0.63829333] | |
[ 0.64019756 0.50205994 0.67319849] | |
[ 0.81505233 0.50488959 0.78425746]] | |
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx | |
----------------Output---------------- | |
[[ 0.63151281] | |
[ 0.75280604] | |
[ 0.72767896] | |
[ 0.82731206]] | |
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx | |
----------------Error---------------- | |
[[-0.63151281] | |
[ 0.24719396] | |
[ 0.27232104] | |
[-0.82731206]] | |
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx | |
----------------Change in Error---------------- | |
[[-0.06315128] | |
[ 0.0247194 ] | |
[ 0.0272321 ] | |
[-0.08273121]] | |
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx | |
----------------Change in Output_Weights---------------- | |
[[ 0.37173106] | |
[ 0.58504897] | |
[ 0.13269181]] | |
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx | |
----------------Change in Hidden result---------------- | |
[[-0.00586882 -0.00923665 -0.00209491] | |
[ 0.00188278 0.0036154 0.00075728] | |
[ 0.00233178 0.00398296 0.00079497] | |
[-0.00463588 -0.01209929 -0.00185741]] | |
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx | |
----------------Change in Hidden Weights---------------- | |
[[ 5.73917597e-01 1.23488327e-04 7.21624805e-01] | |
[ 9.04204587e-01 2.83525363e-03 5.66864234e-01]] | |
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx | |
----------------Hidden Layer Output---------------- | |
[[ 0.5 0.5 0.5 ] | |
[ 0.71181278 0.50070881 0.6380393 ] | |
[ 0.63966665 0.50003087 0.67296471] | |
[ 0.81428878 0.50073968 0.78389133]] | |
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx | |
----------------Output---------------- | |
[[ 0.54473592] | |
[ 0.64220469] | |
[ 0.61962342] | |
[ 0.69966963]] | |
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx | |
----------------Error---------------- | |
[[-0.54473592] | |
[ 0.35779531] | |
[ 0.38037658] | |
[-0.69966963]] | |
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx | |
----------------Change in Error---------------- | |
[[-0.05447359] | |
[ 0.03577953] | |
[ 0.03803766] | |
[-0.06996696]] | |
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx | |
----------------Change in Output_Weights---------------- | |
[[ 0.3373207 ] | |
[ 0.55971207] | |
[ 0.09903527]] | |
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx | |
----------------Change in Hidden result---------------- | |
[[-0.00459377 -0.00762238 -0.0013487 ] | |
[ 0.00247581 0.00500655 0.00081834] | |
[ 0.00295743 0.00532253 0.00082907] | |
[-0.00356905 -0.00979032 -0.00117385]] | |
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx | |
----------------Change in Hidden Weights---------------- | |
[[ 0.57330598 -0.00434429 0.72128003] | |
[ 0.90311135 -0.00194851 0.56650873]] | |
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx | |
[[ 0.54473592] | |
[ 0.64220469] | |
[ 0.61962342] | |
[ 0.69966963]] | |
[Finished in 0.4s] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
epochs = 3 #Change and see the results | |
# Layers | |
inputLayerSize, hiddenLayerSize, outputLayerSize = 2,3,1 | |
#Learning Rate | |
L = 0.1 | |
#input | |
X = np.array([[0,0], [0,1], [1,0], [1,1]]) | |
#Output | |
Y = np.array([[0], [1], [1], [0]]) | |
#Activation | |
def sigmoid(x): return 1/(1+ np.exp(-x)) | |
# Derivative | |
def sigmoid_(x): return x*(1-x) | |
hidden_Weights = np.random.uniform(size=(inputLayerSize,hiddenLayerSize)) | |
print('----------------Hidden Layer Weights----------------') | |
print(hidden_Weights) | |
print('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx') | |
Output_Weights = np.random.uniform(size=(hiddenLayerSize,outputLayerSize)) | |
print('----------------Output Layer Weights----------------') | |
print(Output_Weights) | |
print('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx') | |
for i in range(epochs): | |
Hidden_result = sigmoid(np.dot(X,hidden_Weights)) | |
print('----------------Hidden Layer Output----------------') | |
print(Hidden_result) | |
print('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx') | |
Output = np.dot(Hidden_result,Output_Weights) | |
print('----------------Output----------------') | |
print(Output) | |
print('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx') | |
Error = Y - Output | |
print('----------------Error----------------') | |
print(Error) | |
print('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx') | |
dZ = Error*L | |
print('----------------Change in Error----------------') | |
print(dZ) | |
print('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx') | |
Output_Weights += Hidden_result.T.dot(dZ) | |
print('----------------Change in Output_Weights----------------') | |
print(Output_Weights) | |
print('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx') | |
dHidden_result = dZ.dot(Output_Weights.T) * sigmoid_(Hidden_result) | |
print('----------------Change in Hidden result----------------') | |
print(dHidden_result) | |
print('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx') | |
hidden_Weights += X.T.dot(dHidden_result) | |
print('----------------Change in Hidden Weights----------------') | |
print(hidden_Weights) | |
print('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx') | |
print(Output) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment