# 단일 뉴런 (Single Neuron) - 다중 입력 - 통합된 Numerical Derivative¶

## Gate Neuron¶

In [1]:
import numpy as np
import random
import math

In [2]:
class GateNeuron:
def __init__(self):
self.w = np.array([0.0, 0.0])   # weight of one input
self.b = np.array([0.0])   # bias
print("Initial w: {0}, b: {1}".format(self.w, self.b))

def u(self, x):
return np.dot(self.w, x) + self.b

def f(self, u):
return max(0.0, u)

def z(self, x):
u = self.u(x)
return self.f(u)

def squared_error(self, x, z_target):
return 1.0 / 2.0 * math.pow(self.z(x) - z_target, 2)

def numerical_derivative(self, params, x, z_target):
delta = 1e-4 # 0.0001

for idx in range(params.size):
temp_val = params[idx]

#f(x + delta) 계산
params[idx] = params[idx] + delta
fxh1 = self.squared_error(x, z_target)

#f(x - delta) 계산
params[idx] = params[idx] - delta
fxh2 = self.squared_error(x, z_target)

#f(x + delta) - f(x - delta) / 2 * delta 계산
grad[idx] = (fxh1 - fxh2) / (2 * delta)
params[idx] = temp_val

def learning(self, alpha, maxEpoch, data):
for i in range(maxEpoch):
for idx in range(data.numTrainData):
x = data.training_input_value[idx]
z_target = data.training_z_target[idx]
self.w = self.w - alpha * self.numerical_derivative(self.w, x, z_target)
self.b = self.b - alpha * self.numerical_derivative(self.b, x, z_target)

sum = 0.0
for idx in range(data.numTrainData):
sum = sum + self.squared_error(data.training_input_value[idx], data.training_z_target[idx])
print("Epoch {0}: Error: {1}, w: {2}, b: {3}".format(i, sum / data.numTrainData, self.w, self.b))


### 1. And Gate¶

In [3]:
class Data:
def __init__(self):
self.training_input_value = np.array([(0.0, 0.0), (1.0, 0.0), (0.0, 1.0), (1.0, 1.0)])
self.training_z_target = np.array([0.0, 0.0, 0.0, 1.0])
self.numTrainData = len(self.training_input_value)

if __name__ == '__main__':
n = GateNeuron()
d = Data()
for idx in range(d.numTrainData):
input = d.training_input_value[idx]
z = n.z(input)
z_target = d.training_z_target[idx]
error = n.squared_error(input, z_target)
print("x: {0}, z: {1}, z_target: {2}, error: {3}".format(input, n.z(input), z_target, error))

n.learning(0.1, 100, d)

for idx in range(d.numTrainData):
input = d.training_input_value[idx]
z = n.z(input)
z_target = d.training_z_target[idx]
error = n.squared_error(input, z_target)
print("x: {0}, z: {1}, z_target: {2}, error: {3}".format(input, n.z(input), z_target, error))

Initial w: [ 0.  0.], b: [ 0.]
x: [ 0.  0.], z: 0.0, z_target: 0.0, error: 0.0
x: [ 1.  0.], z: 0.0, z_target: 0.0, error: 0.0
x: [ 0.  1.], z: 0.0, z_target: 0.0, error: 0.0
x: [ 1.  1.], z: 0.0, z_target: 1.0, error: 0.5
Epoch 0: Error: 0.09602056150328674, w: [ 0.04421211  0.04421222], b: [ 0.04556971]
Epoch 1: Error: 0.0786005214252835, w: [ 0.08407977  0.0842878 ], b: [ 0.0749896]
Epoch 2: Error: 0.06895927774776019, w: [ 0.11580851  0.11637514], b: [ 0.09236578]
Epoch 3: Error: 0.063299241904246, w: [ 0.14157015  0.14259201], b: [ 0.10118276]
Epoch 4: Error: 0.05968002027831627, w: [ 0.16293433  0.16446973], b: [ 0.10394969]
Epoch 5: Error: 0.05711593764738026, w: [ 0.18103622  0.18311647], b: [ 0.10247231]
Epoch 6: Error: 0.05510998730211147, w: [ 0.19669754  0.19933505], b: [ 0.09804916]
Epoch 7: Error: 0.05341380551826247, w: [ 0.21051379  0.21370792], b: [ 0.09161303]
Epoch 8: Error: 0.0519045109466406, w: [ 0.22291722  0.22665843], b: [ 0.08383301]
Epoch 9: Error: 0.05052183260337313, w: [ 0.23422227  0.23849511], b: [ 0.07518814]
Epoch 10: Error: 0.0492362436483778, w: [ 0.24465826  0.2494435 ], b: [ 0.06602048]
Epoch 11: Error: 0.048032973068380745, w: [ 0.2543931   0.25966921], b: [ 0.05657338]
Epoch 12: Error: 0.046904100021574135, w: [ 0.26355028  0.26929451], b: [ 0.04701913]
Epoch 13: Error: 0.045844728207354274, w: [ 0.27222117  0.27841033], b: [ 0.03747885]
Epoch 14: Error: 0.04485119507943988, w: [ 0.28047394  0.28708484], b: [ 0.02803688]
Epoch 15: Error: 0.04392027806399948, w: [ 0.28835993  0.29536978], b: [ 0.01875111]
Epoch 16: Error: 0.04304887625447657, w: [ 0.29591826  0.3033049 ], b: [ 0.00966044]
Epoch 17: Error: 0.04223390920342094, w: [ 0.30317919  0.31092121], b: [ 0.00079022]
Epoch 18: Error: 0.0414646165206046, w: [ 0.31016653  0.31824339], b: [-0.00784396]
Epoch 19: Error: 0.04070692842828488, w: [ 0.31693459  0.32532574], b: [-0.01657276]
Epoch 20: Error: 0.039958949190065235, w: [ 0.32352748  0.33221255], b: [-0.02538053]
Epoch 21: Error: 0.03922134745406425, w: [ 0.32997786  0.3389369 ], b: [-0.03422476]
Epoch 22: Error: 0.03849470809698691, w: [ 0.33630997  0.34552359], b: [-0.04307417]
Epoch 23: Error: 0.03777948276431167, w: [ 0.34254175  0.35199124], b: [-0.05190578]
Epoch 24: Error: 0.03707598649528966, w: [ 0.34868653  0.35835393], b: [-0.06070276]
Epoch 25: Error: 0.03638441330776309, w: [ 0.35475422  0.36462232], b: [-0.06945286]
Epoch 26: Error: 0.03570485753911157, w: [ 0.36075217  0.37080458], b: [-0.07814718]
Epoch 27: Error: 0.03503733493323291, w: [ 0.36668588  0.37690698], b: [-0.08677934]
Epoch 28: Error: 0.03438180108305826, w: [ 0.37255946  0.38293439], b: [-0.0953448]
Epoch 29: Error: 0.03373816658331262, w: [ 0.37837597  0.38889067], b: [-0.1038404]
Epoch 30: Error: 0.033106309028759856, w: [ 0.38413777  0.39477887], b: [-0.11226398]
Epoch 31: Error: 0.03248608229138079, w: [ 0.38984661  0.40060148], b: [-0.12061413]
Epoch 32: Error: 0.03187732357803908, w: [ 0.39550387  0.40636053], b: [-0.12889001]
Epoch 33: Error: 0.03127985873738791, w: [ 0.4011106   0.41205777], b: [-0.13709117]
Epoch 34: Error: 0.030693506215272406, w: [ 0.40666767  0.41769464], b: [-0.14521747]
Epoch 35: Error: 0.030118079981957854, w: [ 0.41217575  0.42327243], b: [-0.15326898]
Epoch 36: Error: 0.02955339168507925, w: [ 0.41763541  0.42879228], b: [-0.16124594]
Epoch 37: Error: 0.028999252223767744, w: [ 0.42304714  0.43425521], b: [-0.16914872]
Epoch 38: Error: 0.02845547289237579, w: [ 0.42841137  0.43966216], b: [-0.17697775]
Epoch 39: Error: 0.027921866205455132, w: [ 0.43372848  0.44501401], b: [-0.18473353]
Epoch 40: Error: 0.02739824648742231, w: [ 0.43899882  0.45031155], b: [-0.19241663]
Epoch 41: Error: 0.026884430288943884, w: [ 0.44422272  0.45555557], b: [-0.20002761]
Epoch 42: Error: 0.026380236676008875, w: [ 0.44940051  0.4607468 ], b: [-0.20756709]
Epoch 43: Error: 0.02588548742564869, w: [ 0.4545325   0.46588594], b: [-0.21503568]
Epoch 44: Error: 0.02540000715335366, w: [ 0.45961898  0.47097368], b: [-0.222434]
Epoch 45: Error: 0.024923623390628077, w: [ 0.46466026  0.47601065], b: [-0.22976268]
Epoch 46: Error: 0.024456166626245723, w: [ 0.46965663  0.48099751], b: [-0.23702236]
Epoch 47: Error: 0.02399747032116805, w: [ 0.47460841  0.48593487], b: [-0.24421368]
Epoch 48: Error: 0.023547370904439806, w: [ 0.47951588  0.49082332], b: [-0.25133725]
Epoch 49: Error: 0.023105707755423574, w: [ 0.48437936  0.49566347], b: [-0.25839372]
Epoch 50: Error: 0.022672323176304854, w: [ 0.48919914  0.50045588], b: [-0.2653837]
Epoch 51: Error: 0.022247062357742042, w: [ 0.49397552  0.50520111], b: [-0.27230782]
Epoch 52: Error: 0.021829773339767274, w: [ 0.49870883  0.50989971], b: [-0.2791667]
Epoch 53: Error: 0.021420306969474785, w: [ 0.50339936  0.51455223], b: [-0.28596094]
Epoch 54: Error: 0.02101851685661553, w: [ 0.50804743  0.51915919], b: [-0.29269115]
Epoch 55: Error: 0.020624259327915596, w: [ 0.51265334  0.52372111], b: [-0.29935794]
Epoch 56: Error: 0.02023739338070928, w: [ 0.51721742  0.52823849], b: [-0.3059619]
Epoch 57: Error: 0.01985778063631565, w: [ 0.52173997  0.53271184], b: [-0.31250363]
Epoch 58: Error: 0.019485285293463064, w: [ 0.52622131  0.53714164], b: [-0.3189837]
Epoch 59: Error: 0.019119774081991288, w: [ 0.53066176  0.54152838], b: [-0.3254027]
Epoch 60: Error: 0.018761116216979557, w: [ 0.53506163  0.54587253], b: [-0.3317612]
Epoch 61: Error: 0.018409183353410685, w: [ 0.53942124  0.55017455], b: [-0.33805978]
Epoch 62: Error: 0.018063849541448983, w: [ 0.54374091  0.55443491], b: [-0.344299]
Epoch 63: Error: 0.017724991182386428, w: [ 0.54802096  0.55865405], b: [-0.35047941]
Epoch 64: Error: 0.01739248698528302, w: [ 0.5522617   0.56283242], b: [-0.35660158]
Epoch 65: Error: 0.01706621792432101, w: [ 0.55646345  0.56697045], b: [-0.36266604]
Epoch 66: Error: 0.016746067196897516, w: [ 0.56062654  0.57106857], b: [-0.36867335]
Epoch 67: Error: 0.016431920182430417, w: [ 0.56475127  0.5751272 ], b: [-0.37462405]
Epoch 68: Error: 0.016123664401906913, w: [ 0.56883796  0.57914677], b: [-0.38051866]
Epoch 69: Error: 0.015821189478153037, w: [ 0.57288692  0.58312767], b: [-0.38635771]
Epoch 70: Error: 0.015524387096815688, w: [ 0.57689849  0.58707032], b: [-0.39214173]
Epoch 71: Error: 0.01523315096805939, w: [ 0.58087295  0.59097511], b: [-0.39787124]
Epoch 72: Error: 0.014947376788954438, w: [ 0.58481064  0.59484244], b: [-0.40354675]
Epoch 73: Error: 0.01466696220655811, w: [ 0.58871186  0.59867269], b: [-0.40916877]
Epoch 74: Error: 0.014391806781666523, w: [ 0.59257692  0.60246624], b: [-0.4147378]
Epoch 75: Error: 0.014121811953240391, w: [ 0.59640613  0.60622348], b: [-0.42025435]
Epoch 76: Error: 0.01385688100347322, w: [ 0.60019981  0.60994476], b: [-0.4257189]
Epoch 77: Error: 0.013596919023506345, w: [ 0.60395825  0.61363046], b: [-0.43113195]
Epoch 78: Error: 0.01334183287977772, w: [ 0.60768176  0.61728095], b: [-0.43649398]
Epoch 79: Error: 0.01309153118098377, w: [ 0.61137064  0.62089656], b: [-0.44180548]
Epoch 80: Error: 0.012845924245653197, w: [ 0.61502521  0.62447767], b: [-0.44706691]
Epoch 81: Error: 0.012604924070313115, w: [ 0.61864575  0.62802461], b: [-0.45227876]
Epoch 82: Error: 0.012368444298249988, w: [ 0.62223258  0.63153773], b: [-0.45744149]
Epoch 83: Error: 0.012136400188834683, w: [ 0.62578597  0.63501737], b: [-0.46255556]
Epoch 84: Error: 0.011908708587421598, w: [ 0.62930624  0.63846386], b: [-0.46762143]
Epoch 85: Error: 0.011685287895796686, w: [ 0.63279367  0.64187753], b: [-0.47263955]
Epoch 86: Error: 0.011466058043174236, w: [ 0.63624856  0.64525871], b: [-0.47761038]
Epoch 87: Error: 0.011250940457724805, w: [ 0.6396712   0.64860772], b: [-0.48253436]
Epoch 88: Error: 0.011039858038633377, w: [ 0.64306186  0.65192488], b: [-0.48741193]
Epoch 89: Error: 0.010832735128667375, w: [ 0.64642085  0.65521051], b: [-0.49224353]
Epoch 90: Error: 0.010629497487252941, w: [ 0.64974845  0.65846491], b: [-0.4970296]
Epoch 91: Error: 0.010430072264050017, w: [ 0.65304493  0.6616884 ], b: [-0.50177055]
Epoch 92: Error: 0.010234387973013563, w: [ 0.65631058  0.66488127], b: [-0.50646682]
Epoch 93: Error: 0.010042374466935122, w: [ 0.65954568  0.66804383], b: [-0.51111883]
Epoch 94: Error: 0.009853962912454545, w: [ 0.6627505   0.67117637], b: [-0.515727]
Epoch 95: Error: 0.009669085765531792, w: [ 0.66592532  0.6742792 ], b: [-0.52029173]
Epoch 96: Error: 0.009487676747377938, w: [ 0.6690704   0.67735259], b: [-0.52481345]
Epoch 97: Error: 0.00930967082082582, w: [ 0.67218603  0.68039684], b: [-0.52929254]
Epoch 98: Error: 0.009135004167139825, w: [ 0.67527247  0.68341222], b: [-0.53372942]
Epoch 99: Error: 0.008963614163257276, w: [ 0.67832998  0.68639903], b: [-0.53812448]
x: [ 0.  0.], z: 0.0, z_target: 0.0, error: 0.0
x: [ 1.  0.], z: [ 0.1402055], z_target: 0.0, error: 0.00982879044110745
x: [ 0.  1.], z: [ 0.14827455], z_target: 0.0, error: 0.01099267039936062
x: [ 1.  1.], z: [ 0.82660452], z_target: 1.0, error: 0.015032995812561034


### 2. Or Gate¶

In [4]:
class Data:
def __init__(self):
self.training_input_value = np.array([(0.0, 0.0), (1.0, 0.0), (0.0, 1.0), (1.0, 1.0)])
self.training_z_target = np.array([0.0, 1.0, 1.0, 1.0])
self.numTrainData = len(self.training_input_value)

if __name__ == '__main__':
n = GateNeuron()
d = Data()
for idx in range(d.numTrainData):
input = d.training_input_value[idx]
z = n.z(input)
z_target = d.training_z_target[idx]
error = n.squared_error(input, z_target)
print("x: {0}, z: {1}, z_target: {2}, error: {3}".format(input, n.z(input), z_target, error))

n.learning(0.1, 100, d)

for idx in range(d.numTrainData):
input = d.training_input_value[idx]
z = n.z(input)
z_target = d.training_z_target[idx]
error = n.squared_error(input, z_target)
print("x: {0}, z: {1}, z_target: {2}, error: {3}".format(input, n.z(input), z_target, error))

Initial w: [ 0.  0.], b: [ 0.]
x: [ 0.  0.], z: 0.0, z_target: 0.0, error: 0.0
x: [ 1.  0.], z: 0.0, z_target: 1.0, error: 0.5
x: [ 0.  1.], z: 0.0, z_target: 1.0, error: 0.5
x: [ 1.  1.], z: 0.0, z_target: 1.0, error: 0.5
Epoch 0: Error: 0.2150467841601992, w: [ 0.08928694  0.08815893], b: [ 0.12928176]
Epoch 1: Error: 0.13123956576982937, w: [ 0.1561592   0.15321646], b: [ 0.2206686]
Epoch 2: Error: 0.08738976965380288, w: [ 0.20586365  0.20156192], b: [ 0.28478355]
Epoch 3: Error: 0.0643342861691084, w: [ 0.24313571  0.23780564], b: [ 0.32928703]
Epoch 4: Error: 0.052107228933514285, w: [ 0.27139272  0.26527467], b: [ 0.35970261]
Epoch 5: Error: 0.0455257497948977, w: [ 0.29310126  0.28637036], b: [ 0.38001257]
Epoch 6: Error: 0.04189422091879892, w: [ 0.31004201  0.30282629], b: [ 0.3930875]
Epoch 7: Error: 0.03981028250817888, w: [ 0.32350088  0.31589431], b: [ 0.40099618]
Epoch 8: Error: 0.03854398378590385, w: [ 0.33440686  0.3264788 ], b: [ 0.40522909]
Epoch 9: Error: 0.03771497076790282, w: [ 0.34343141  0.33523337], b: [ 0.40685966]
Epoch 10: Error: 0.037124662167207646, w: [ 0.3510602  0.3426308], b: [ 0.40666051]
Epoch 11: Error: 0.03666901119691938, w: [ 0.35764484  0.34901332], b: [ 0.40518739]
Epoch 12: Error: 0.03629316197747596, w: [ 0.36344021  0.35462899], b: [ 0.40283962]
Epoch 13: Error: 0.035967882466639116, w: [ 0.36863135  0.35965788], b: [ 0.39990376]
Epoch 14: Error: 0.03567731478661393, w: [ 0.37335291  0.36423101], b: [ 0.3965851]
Epoch 15: Error: 0.03541260633179563, w: [ 0.37770314  0.36844396], b: [ 0.39303032]
Epoch 16: Error: 0.03516859628228479, w: [ 0.381754    0.37236671], b: [ 0.3893439]
Epoch 17: Error: 0.0349420895114883, w: [ 0.38555845  0.37605076], b: [ 0.38559992]
Epoch 18: Error: 0.03473095530553358, w: [ 0.3891557   0.37953421], b: [ 0.38185055]
Epoch 19: Error: 0.034533654838607895, w: [ 0.39257502  0.38284552], b: [ 0.37813223]
Epoch 20: Error: 0.03434899173589202, w: [ 0.39583848  0.38600609], b: [ 0.37447009]
Epoch 21: Error: 0.03417597892149431, w: [ 0.39896291  0.38903228], b: [ 0.37088111]
Epoch 22: Error: 0.03401376627393248, w: [ 0.40196136  0.3919367 ], b: [ 0.36737646]
Epoch 23: Error: 0.03386160025035084, w: [ 0.40484407  0.39472931], b: [ 0.36396315]
Epoch 24: Error: 0.033718800465161396, w: [ 0.40761928  0.39741807], b: [ 0.36064523]
Epoch 25: Error: 0.03358474538421383, w: [ 0.41029372  0.40000949], b: [ 0.3574246]
Epoch 26: Error: 0.03345886302215633, w: [ 0.41287304  0.40250902], b: [ 0.35430173]
Epoch 27: Error: 0.03334062446872781, w: [ 0.41536205  0.4049213 ], b: [ 0.35127601]
Epoch 28: Error: 0.0332295390802726, w: [ 0.41776494  0.4072504 ], b: [ 0.34834611]
Epoch 29: Error: 0.033125150702151114, w: [ 0.42008545  0.40949989], b: [ 0.34551024]
Epoch 30: Error: 0.03302703456715815, w: [ 0.42232693  0.41167303], b: [ 0.34276625]
Epoch 31: Error: 0.03293479466430809, w: [ 0.42449248  0.41377278], b: [ 0.34011181]
Epoch 32: Error: 0.03284806145351042, w: [ 0.42658495  0.41580189], b: [ 0.33754444]
Epoch 33: Error: 0.03276648984693443, w: [ 0.42860701  0.41776294], b: [ 0.33506163]
Epoch 34: Error: 0.03268975740394773, w: [ 0.43056118  0.41965835], b: [ 0.33266082]
Epoch 35: Error: 0.032617562702244134, w: [ 0.43244984  0.42149041], b: [ 0.33033949]
Epoch 36: Error: 0.032549623857635636, w: [ 0.43427527  0.42326132], b: [ 0.32809511]
Epoch 37: Error: 0.03248567717154244, w: [ 0.43603964  0.42497317], b: [ 0.32592524]
Epoch 38: Error: 0.03242547588973016, w: [ 0.43774503  0.42662797], b: [ 0.32382746]
Epoch 39: Error: 0.03236878905909094, w: [ 0.43939345  0.42822763], b: [ 0.32179942]
Epoch 40: Error: 0.032315400471666866, w: [ 0.44098683  0.42977403], b: [ 0.31983884]
Epoch 41: Error: 0.03226510768695503, w: [ 0.44252701  0.43126895], b: [ 0.31794349]
Epoch 42: Error: 0.032217721124932486, w: [ 0.44401579  0.4327141 ], b: [ 0.31611123]
Epoch 43: Error: 0.032173063223360124, w: [ 0.44545488  0.43411116], b: [ 0.31433997]
Epoch 44: Error: 0.03213096765378962, w: [ 0.44684596  0.43546172], b: [ 0.31262769]
Epoch 45: Error: 0.0320912785914354, w: [ 0.44819063  0.43676734], b: [ 0.31097242]
Epoch 46: Error: 0.0320538500346155, w: [ 0.44949045  0.43802952], b: [ 0.30937228]
Epoch 47: Error: 0.03201854516996556, w: [ 0.4507469   0.43924969], b: [ 0.30782543]
Epoch 48: Error: 0.0319852357800155, w: [ 0.45196145  0.44042927], b: [ 0.3063301]
Epoch 49: Error: 0.03195380169005094, w: [ 0.45313549  0.44156959], b: [ 0.30488458]
Epoch 50: Error: 0.03192413025146337, w: [ 0.45427037  0.44267198], b: [ 0.3034872]
Epoch 51: Error: 0.03189611585903519, w: [ 0.45536741  0.44373768], b: [ 0.30213637]
Epoch 52: Error: 0.03186965949982513, w: [ 0.45642785  0.44476792], b: [ 0.30083053]
Epoch 53: Error: 0.03184466833149737, w: [ 0.45745294  0.44576388], b: [ 0.2995682]
Epoch 54: Error: 0.0318210552881097, w: [ 0.45844384  0.4467267 ], b: [ 0.29834791]
Epoch 55: Error: 0.031798738711519954, w: [ 0.4594017   0.44765748], b: [ 0.29716827]
Epoch 56: Error: 0.03177764200670315, w: [ 0.46032762  0.44855728], b: [ 0.29602792]
Epoch 57: Error: 0.03175769331940778, w: [ 0.46122266  0.44942715], b: [ 0.29492556]
Epoch 58: Error: 0.031738825234656744, w: [ 0.46208786  0.45026806], b: [ 0.29385992]
Epoch 59: Error: 0.03172097449474404, w: [ 0.46292421  0.45108099], b: [ 0.29282978]
Epoch 60: Error: 0.031704081735436165, w: [ 0.46373267  0.45186687], b: [ 0.29183395]
Epoch 61: Error: 0.031688091239191384, w: [ 0.46451418  0.4526266 ], b: [ 0.2908713]
Epoch 62: Error: 0.0316729507042848, w: [ 0.46526962  0.45336104], b: [ 0.28994071]
Epoch 63: Error: 0.031658611028802505, w: [ 0.46599989  0.45407104], b: [ 0.28904112]
Epoch 64: Error: 0.03164502610853484, w: [ 0.4667058   0.45475741], b: [ 0.28817149]
Epoch 65: Error: 0.03163215264786926, w: [ 0.46738818  0.45542093], b: [ 0.28733084]
Epoch 66: Error: 0.03161994998282753, w: [ 0.46804781  0.45606237], b: [ 0.28651818]
Epoch 67: Error: 0.031608379915471545, w: [ 0.46868545  0.45668246], b: [ 0.2857326]
Epoch 68: Error: 0.03159740655893007, w: [ 0.46930183  0.45728192], b: [ 0.28497319]
Epoch 69: Error: 0.03158699619236143, w: [ 0.46989766  0.45786142], b: [ 0.28423907]
Epoch 70: Error: 0.03157711712520737, w: [ 0.47047362  0.45842163], b: [ 0.28352941]
Epoch 71: Error: 0.03156773957013585, w: [ 0.47103039  0.4589632 ], b: [ 0.28284338]
Epoch 72: Error: 0.031558835524110485, w: [ 0.4715686   0.45948674], b: [ 0.28218021]
Epoch 73: Error: 0.0315503786570598, w: [ 0.47208886  0.45999285], b: [ 0.28153913]
Epoch 74: Error: 0.031542344207657884, w: [ 0.47259178  0.46048212], b: [ 0.28091941]
Epoch 75: Error: 0.03153470888574876, w: [ 0.47307794  0.4609551 ], b: [ 0.28032033]
Epoch 76: Error: 0.03152745078099527, w: [ 0.47354789  0.46141234], b: [ 0.2797412]
Epoch 77: Error: 0.0315205492773436, w: [ 0.47400217  0.46185436], b: [ 0.27918137]
Epoch 78: Error: 0.031513984972929336, w: [ 0.47444131  0.46228166], b: [ 0.27864018]
Epoch 79: Error: 0.031507739605075115, w: [ 0.47486582  0.46269474], b: [ 0.27811702]
Epoch 80: Error: 0.03150179598005045, w: [ 0.47527617  0.46309407], b: [ 0.27761129]
Epoch 81: Error: 0.0314961379072872, w: [ 0.47567284  0.4634801 ], b: [ 0.2771224]
Epoch 82: Error: 0.031490750137763264, w: [ 0.4760563   0.46385328], b: [ 0.2766498]
Epoch 83: Error: 0.031485618306286565, w: [ 0.47642697  0.46421404], b: [ 0.27619295]
Epoch 84: Error: 0.031480728877428615, w: [ 0.47678529  0.46456279], b: [ 0.27575131]
Epoch 85: Error: 0.031476069094869906, w: [ 0.47713166  0.46489993], b: [ 0.27532438]
Epoch 86: Error: 0.03147162693394536, w: [ 0.47746649  0.46522585], b: [ 0.27491167]
Epoch 87: Error: 0.03146739105717457, w: [ 0.47779016  0.46554092], b: [ 0.27451272]
Epoch 88: Error: 0.031463350772592324, w: [ 0.47810304  0.46584549], b: [ 0.27412705]
Epoch 89: Error: 0.031459495994696475, w: [ 0.47840549  0.46613993], b: [ 0.27375423]
Epoch 90: Error: 0.03145581720784915, w: [ 0.47869786  0.46642457], b: [ 0.27339382]
Epoch 91: Error: 0.031452305431966136, w: [ 0.47898049  0.46669972], b: [ 0.27304543]
Epoch 92: Error: 0.031448952190358884, w: [ 0.4792537   0.46696572], b: [ 0.27270863]
Epoch 93: Error: 0.031445749479582334, w: [ 0.4795178   0.46722286], b: [ 0.27238306]
Epoch 94: Error: 0.03144268974116564, w: [ 0.4797731   0.46747144], b: [ 0.27206833]
Epoch 95: Error: 0.03143976583510402, w: [ 0.4800199   0.46771174], b: [ 0.27176409]
Epoch 96: Error: 0.03143697101499658, w: [ 0.48025846  0.46794405], b: [ 0.27146998]
Epoch 97: Error: 0.031434298904731976, w: [ 0.48048908  0.46816862], b: [ 0.27118567]
Epoch 98: Error: 0.03143174347661455, w: [ 0.48071201  0.46838571], b: [ 0.27091083]
Epoch 99: Error: 0.031429299030849955, w: [ 0.48092751  0.46859557], b: [ 0.27064514]
x: [ 0.  0.], z: [ 0.27064514], z_target: 0.0, error: 0.03662439593559476
x: [ 1.  0.], z: [ 0.75157265], z_target: 1.0, error: 0.030858073313985882
x: [ 0.  1.], z: [ 0.73924071], z_target: 1.0, error: 0.03399770354394306
x: [ 1.  1.], z: [ 1.22016822], z_target: 1.0, error: 0.024237023329876137


### 3. XOR Gate¶

In [5]:
class Data:
def __init__(self):
self.training_input_value = np.array([(0.0, 0.0), (1.0, 0.0), (0.0, 1.0), (1.0, 1.0)])
self.training_z_target = np.array([0.0, 1.0, 1.0, 0.0])
self.numTrainData = len(self.training_input_value)

if __name__ == '__main__':
n = GateNeuron()
d = Data()
for idx in range(d.numTrainData):
input = d.training_input_value[idx]
z = n.z(input)
z_target = d.training_z_target[idx]
error = n.squared_error(input, z_target)
print("x: {0}, z: {1}, z_target: {2}, error: {3}".format(input, n.z(input), z_target, error))

n.learning(0.1, 100, d)

for idx in range(d.numTrainData):
input = d.training_input_value[idx]
z = n.z(input)
z_target = d.training_z_target[idx]
error = n.squared_error(input, z_target)
print("x: {0}, z: {1}, z_target: {2}, error: {3}".format(input, n.z(input), z_target, error))

Initial w: [ 0.  0.], b: [ 0.]
x: [ 0.  0.], z: 0.0, z_target: 0.0, error: 0.0
x: [ 1.  0.], z: 0.0, z_target: 1.0, error: 0.5
x: [ 0.  1.], z: 0.0, z_target: 1.0, error: 0.5
x: [ 1.  1.], z: 0.0, z_target: 0.0, error: 0.0
Epoch 0: Error: 0.19643767374838308, w: [ 0.03928694  0.03815893], b: [ 0.08428176]
Epoch 1: Error: 0.1673659366191361, w: [ 0.06705137  0.06388834], b: [ 0.14714978]
Epoch 2: Error: 0.1514475008892959, w: [ 0.08560326  0.08071475], b: [ 0.19450273]
Epoch 3: Error: 0.14254737037484824, w: [ 0.09755647  0.09117965], b: [ 0.23059768]
Epoch 4: Error: 0.13742709476063045, w: [ 0.10479501  0.09711413], b: [ 0.25850753]
Epoch 5: Error: 0.13436841370926816, w: [ 0.10867645  0.09983687], b: [ 0.28045095]
Epoch 6: Error: 0.13245336933166502, w: [ 0.11017871  0.10029709], b: [ 0.29803038]
Epoch 7: Error: 0.13118706733906055, w: [ 0.11000586  0.09917754], b: [ 0.31240368]
Epoch 8: Error: 0.13029949201065982, w: [ 0.10866452  0.09696886], b: [ 0.32440797]
Epoch 9: Error: 0.12964103291933662, w: [ 0.1065189   0.09402316], b: [ 0.33464896]
Epoch 10: Error: 0.12912716095514487, w: [ 0.10383051  0.0905927 ], b: [ 0.34356535]
Epoch 11: Error: 0.12870895479932604, w: [ 0.10078684  0.08685781], b: [ 0.35147536]
Epoch 12: Error: 0.1283572751502124, w: [ 0.09752203  0.08294694], b: [ 0.35861021]
Epoch 13: Error: 0.12805418055959678, w: [ 0.09413171  0.07895122], b: [ 0.36513833]
Epoch 14: Error: 0.12778821119301353, w: [ 0.09068386  0.07493491], b: [ 0.37118277]
Epoch 15: Error: 0.12755175634123883, w: [ 0.08722644  0.07094291], b: [ 0.37683386]
Epoch 16: Error: 0.12733955686754578, w: [ 0.08379311  0.06700626], b: [ 0.38215821]
Epoch 17: Error: 0.12714783433329468, w: [ 0.08040713  0.06314599], b: [ 0.3872053]
Epoch 18: Error: 0.1269737719758422, w: [ 0.07708439  0.05937602], b: [ 0.39201221]
Epoch 19: Error: 0.12681519715925962, w: [ 0.07383543  0.05570514], b: [ 0.39660704]
Epoch 20: Error: 0.12667038179561463, w: [ 0.07066696  0.05213851], b: [ 0.40101133]
Epoch 21: Error: 0.12653791354417962, w: [ 0.06758299  0.04867869], b: [ 0.40524187]
Epoch 22: Error: 0.12641661056451745, w: [ 0.06458558  0.04532644], b: [ 0.409312]
Epoch 23: Error: 0.12630546375537846, w: [ 0.0616754   0.04208123], b: [ 0.41323249]
Epoch 24: Error: 0.12620359676130571, w: [ 0.05885217  0.03894166], b: [ 0.41701224]
Epoch 25: Error: 0.1261102377212422, w: [ 0.05611493  0.03590571], b: [ 0.42065878]
Epoch 26: Error: 0.12602469893138318, w: [ 0.05346225  0.03297099], b: [ 0.42417858]
Epoch 27: Error: 0.12594636193535652, w: [ 0.0508924   0.03013484], b: [ 0.42757733]
Epoch 28: Error: 0.12587466639309353, w: [ 0.04840343  0.02739446], b: [ 0.43086013]
Epoch 29: Error: 0.125809101616288, w: [ 0.04599329  0.02474696], b: [ 0.4340316]
Epoch 30: Error: 0.1257492000090629, w: [ 0.04365982  0.02218944], b: [ 0.43709601]
Epoch 31: Error: 0.12569453188613527, w: [ 0.04140086  0.019719  ], b: [ 0.44005732]
Epoch 32: Error: 0.12564470129889485, w: [ 0.03921424  0.01733279], b: [ 0.44291924]
Epoch 33: Error: 0.125599342608227, w: [ 0.0370978   0.01502801], b: [ 0.44568531]
Epoch 34: Error: 0.12555811761808436, w: [ 0.03504942  0.01280192], b: [ 0.44835886]
Epoch 35: Error: 0.125520713136316, w: [ 0.03306701  0.01065184], b: [ 0.45094307]
Epoch 36: Error: 0.12548683886627038, w: [ 0.03114852  0.0085752 ], b: [ 0.45344101]
Epoch 37: Error: 0.12545622555886538, w: [ 0.02929197  0.00656948], b: [ 0.4558556]
Epoch 38: Error: 0.1254286233734604, w: [ 0.0274954   0.00463224], b: [ 0.45818966]
Epoch 39: Error: 0.1254038004091914, w: [ 0.02575694  0.00276114], b: [ 0.4604459]
Epoch 40: Error: 0.12538154137799717, w: [ 0.02407474  0.0009539 ], b: [ 0.46262694]
Epoch 41: Error: 0.12536164639748987, w: [ 0.02244703 -0.00079168], b: [ 0.46473529]
Epoch 42: Error: 0.12534392988682203, w: [ 0.02087207 -0.00247771], b: [ 0.46677338]
Epoch 43: Error: 0.12532821955234266, w: [ 0.01934819 -0.00410627], b: [ 0.46874357]
Epoch 44: Error: 0.12531435545256173, w: [ 0.01787375 -0.00567931], b: [ 0.47064811]
Epoch 45: Error: 0.12530218913388858, w: [ 0.01644719 -0.00719876], b: [ 0.47248921]
Epoch 46: Error: 0.12529158283013542, w: [ 0.01506697 -0.00866646], b: [ 0.47426898]
Epoch 47: Error: 0.12528240871987206, w: [ 0.0137316  -0.01008418], b: [ 0.47598946]
Epoch 48: Error: 0.1252745482366138, w: [ 0.01243966 -0.01145366], b: [ 0.47765263]
Epoch 49: Error: 0.12526789142746525, w: [ 0.01118975 -0.01277654], b: [ 0.47926039]
Epoch 50: Error: 0.12526233635639092, w: [ 0.00998052 -0.01405444], b: [ 0.4808146]
Epoch 51: Error: 0.12525778854871367, w: [ 0.00881067 -0.01528888], b: [ 0.48231705]
Epoch 52: Error: 0.1252541604737755, w: [ 0.00767893 -0.01648138], b: [ 0.48376944]
Epoch 53: Error: 0.12525137106300915, w: [ 0.00658408 -0.01763336], b: [ 0.48517346]
Epoch 54: Error: 0.12524934526089077, w: [ 0.00552492 -0.01874622], b: [ 0.48653071]
Epoch 55: Error: 0.1252480136064705, w: [ 0.00450032 -0.0198213 ], b: [ 0.48784275]
Epoch 56: Error: 0.1252473118433689, w: [ 0.00350915 -0.02085989], b: [ 0.48911108]
Epoch 57: Error: 0.12524718055627992, w: [ 0.00255034 -0.02186325], b: [ 0.49033717]
Epoch 58: Error: 0.1252475648321633, w: [ 0.00162284 -0.02283257], b: [ 0.49152242]
Epoch 59: Error: 0.1252484139444542, w: [ 0.00072564 -0.02376903], b: [ 0.49266819]
Epoch 60: Error: 0.12524968105874268, w: [-0.00014223 -0.02467375], b: [ 0.4937758]
Epoch 61: Error: 0.1252513229584556, w: [-0.00098172 -0.02554782], b: [ 0.49484651]
Epoch 62: Error: 0.12525329978920557, w: [-0.00179376 -0.02639227], b: [ 0.49588156]
Epoch 63: Error: 0.12525557482055438, w: [-0.00257923 -0.02720813], b: [ 0.49688213]
Epoch 64: Error: 0.12525811422400385, w: [-0.00333899 -0.02799636], b: [ 0.49784937]
Epoch 65: Error: 0.12526088686614906, w: [-0.00407387 -0.02875792], b: [ 0.49878439]
Epoch 66: Error: 0.12526386411595564, w: [-0.00478469 -0.02949372], b: [ 0.49968827]
Epoch 67: Error: 0.12526701966522097, w: [-0.00547221 -0.03020462], b: [ 0.50056204]
Epoch 68: Error: 0.12527032936133833, w: [-0.00613721 -0.03089149], b: [ 0.50140671]
Epoch 69: Error: 0.12527377105153248, w: [-0.0067804  -0.03155513], b: [ 0.50222324]
Epoch 70: Error: 0.12527732443779177, w: [-0.0074025  -0.03219634], b: [ 0.50301257]
Epoch 71: Error: 0.12528097094179236, w: [-0.00800419 -0.03281589], b: [ 0.50377561]
Epoch 72: Error: 0.12528469357912633, w: [-0.00858614 -0.03341451], b: [ 0.50451323]
Epoch 73: Error: 0.1252884768422238, w: [-0.00914899 -0.03399291], b: [ 0.50522628]
Epoch 74: Error: 0.12529230659136487, w: [-0.00969336 -0.03455178], b: [ 0.50591558]
Epoch 75: Error: 0.12529616995325343, w: [-0.01021985 -0.03509179], b: [ 0.50658193]
Epoch 76: Error: 0.1253000552266401, w: [-0.01072905 -0.03561357], b: [ 0.50722607]
Epoch 77: Error: 0.1253039517944997, w: [-0.01122151 -0.03611775], b: [ 0.50784876]
Epoch 78: Error: 0.12530785004235473, w: [-0.01169779 -0.03660492], b: [ 0.50845071]
Epoch 79: Error: 0.12531174128229183, w: [-0.01215841 -0.03707566], b: [ 0.50903261]
Epoch 80: Error: 0.12531561768232052, w: [-0.01260389 -0.03753053], b: [ 0.50959512]
Epoch 81: Error: 0.1253194722006775, w: [-0.01303471 -0.03797007], b: [ 0.5101389]
Epoch 82: Error: 0.12532329852477575, w: [-0.01345136 -0.03839479], b: [ 0.51066456]
Epoch 83: Error: 0.12532709101445946, w: [-0.01385429 -0.03880521], b: [ 0.51117271]
Epoch 84: Error: 0.1253308446492735, w: [-0.01424397 -0.03920179], b: [ 0.51166394]
Epoch 85: Error: 0.12533455497949336, w: [-0.01462081 -0.03958502], b: [ 0.51213881]
Epoch 86: Error: 0.12533821808062642, w: [-0.01498524 -0.03995534], b: [ 0.51259785]
Epoch 87: Error: 0.12534183051118616, w: [-0.01533768 -0.0403132 ], b: [ 0.51304161]
Epoch 88: Error: 0.12534538927348218, w: [-0.0156785 -0.040659 ], b: [ 0.51347058]
Epoch 89: Error: 0.12534889177723627, w: [-0.01600809 -0.04099317], b: [ 0.51388527]
Epoch 90: Error: 0.1253523358058336, w: [-0.01632681 -0.0413161 ], b: [ 0.51428614]
Epoch 91: Error: 0.1253557194850133, w: [-0.01663503 -0.04162816], b: [ 0.51467366]
Epoch 92: Error: 0.1253590412538476, w: [-0.01693309 -0.04192973], b: [ 0.51504827]
Epoch 93: Error: 0.12536229983783573, w: [-0.01722132 -0.04222115], b: [ 0.51541041]
Epoch 94: Error: 0.12536549422398297, w: [-0.01750004 -0.04250277], b: [ 0.51576048]
Epoch 95: Error: 0.1253686236377128, w: [-0.01776957 -0.04277493], b: [ 0.51609889]
Epoch 96: Error: 0.12537168752149275, w: [-0.0180302  -0.04303794], b: [ 0.51642602]
Epoch 97: Error: 0.12537468551505218, w: [-0.01828224 -0.0432921 ], b: [ 0.51674226]
Epoch 98: Error: 0.12537761743709197, w: [-0.01852595 -0.04353773], b: [ 0.51704797]
Epoch 99: Error: 0.12538048326835516, w: [-0.01876162 -0.04377511], b: [ 0.51734349]
x: [ 0.  0.], z: [ 0.51734349], z_target: 0.0, error: 0.13382214371005338
x: [ 1.  0.], z: [ 0.49858188], z_target: 1.0, error: 0.12571006796023548
x: [ 0.  1.], z: [ 0.47356838], z_target: 1.0, error: 0.13856512276307884
x: [ 1.  1.], z: [ 0.45480677], z_target: 0.0, error: 0.10342459864005295