In [13]:
import numpy as np
# sigmoid function
def nonlin(x,deriv=False):
if(deriv==True):
return x*(1-x)
return 1/(1+np.exp(-x))
# input dataset
X = np.array([ [0,0,1],
[0,1,1],
[1,0,1],
[1,1,1] ])
# output dataset
y = np.array([[0,0,1,1]]).T
# seed random numbers to make calculation
# deterministic (just a good practice)
np.random.seed(1)
# initialize weights randomly with mean 0
syn0 = 2*np.random.random((3,1)) - 1
for iter in range(10000):
# forward propagation
l0 = X
l1 = nonlin(np.dot(l0,syn0))
# if iter == 0:
# print("The value of l1 on iter 0 is: {0}".format(l1))
# elif iter == 9999:
# print("The value of l1 on iter 9999 is: {0}".format(l1))
# if iter % 100 == 0:
# print("The value of l1 on iter {0} is {1}".format(iter,l1))
# how much did we miss?
l1_error = y - l1
if iter % 100 == 0:
print("The value of l1 error on iter {0} is {1}".format(iter,l1_error))
# multiply how much we missed by the
# slope of the sigmoid at the values in l1
l1_delta = l1_error * nonlin(l1,True)
# update weights
syn0 += np.dot(l0.T,l1_delta)
print("Output After Training:")
print(l1)
The value of l1 error on iter 0 is [[-0.2689864 ]
[-0.36375058]
[ 0.76237183]
[ 0.6737243 ]]
The value of l1 error on iter 100 is [[-0.10975431]
[-0.0914018 ]
[ 0.07391609]
[ 0.08910196]]
The value of l1 error on iter 200 is [[-0.07532702]
[-0.06151182]
[ 0.04977973]
[ 0.06113161]]
The value of l1 error on iter 300 is [[-0.06044451]
[-0.04912604]
[ 0.03979486]
[ 0.04907442]]
The value of l1 error on iter 400 is [[-0.05175173]
[-0.04198947]
[ 0.03403931]
[ 0.0420343 ]]
The value of l1 error on iter 500 is [[-0.04590957]
[-0.03722323]
[ 0.03019293]
[ 0.03730225]]
The value of l1 error on iter 600 is [[-0.0416479 ]
[-0.03375824]
[ 0.02739494]
[ 0.03384954]]
The value of l1 error on iter 700 is [[-0.03836706]
[-0.03109611]
[ 0.02524409]
[ 0.03119075]]
The value of l1 error on iter 800 is [[-0.03574306]
[-0.02896968]
[ 0.02352521]
[ 0.02906372]]
The value of l1 error on iter 900 is [[-0.03358382]
[-0.02722134]
[ 0.02211135]
[ 0.02731301]]
The value of l1 error on iter 1000 is [[-0.03176745]
[-0.02575143]
[ 0.02092221]
[ 0.02583995]]
The value of l1 error on iter 1100 is [[-0.03021241]
[-0.02449348]
[ 0.01990419]
[ 0.02457858]]
The value of l1 error on iter 1200 is [[-0.02886188]
[-0.02340124]
[ 0.01902 ]
[ 0.02348288]]
The value of l1 error on iter 1300 is [[-0.0276749 ]
[-0.02244142]
[ 0.01824281]
[ 0.0225197 ]]
The value of l1 error on iter 1400 is [[-0.02662111]
[-0.02158939]
[ 0.01755272]
[ 0.02166446]]
The value of l1 error on iter 1500 is [[-0.02567747]
[-0.02082646]
[ 0.01693465]
[ 0.0208985 ]]
The value of l1 error on iter 1600 is [[-0.02482613]
[-0.02013818]
[ 0.01637694]
[ 0.02020737]]
The value of l1 error on iter 1700 is [[-0.02405307]
[-0.01951317]
[ 0.0158704 ]
[ 0.0195797 ]]
The value of l1 error on iter 1800 is [[-0.02334702]
[-0.01894234]
[ 0.01540769]
[ 0.01900638]]
The value of l1 error on iter 1900 is [[-0.0226989 ]
[-0.01841832]
[ 0.01498285]
[ 0.01848002]]
The value of l1 error on iter 2000 is [[-0.02210122]
[-0.01793507]
[ 0.014591 ]
[ 0.01799459]]
The value of l1 error on iter 2100 is [[-0.0215478 ]
[-0.01748758]
[ 0.01422809]
[ 0.01754505]]
The value of l1 error on iter 2200 is [[-0.02103346]
[-0.01707166]
[ 0.01389075]
[ 0.01712722]]
The value of l1 error on iter 2300 is [[-0.02055383]
[-0.01668379]
[ 0.01357611]
[ 0.01673756]]
The value of l1 error on iter 2400 is [[-0.0201052 ]
[-0.01632096]
[ 0.01328175]
[ 0.01637304]]
The value of l1 error on iter 2500 is [[-0.01968437]
[-0.01598059]
[ 0.01300558]
[ 0.01603108]]
The value of l1 error on iter 2600 is [[-0.0192886 ]
[-0.01566047]
[ 0.01274581]
[ 0.01570947]]
The value of l1 error on iter 2700 is [[-0.01891551]
[-0.01535868]
[ 0.01250089]
[ 0.01540626]]
The value of l1 error on iter 2800 is [[-0.01856303]
[-0.01507353]
[ 0.01226945]
[ 0.01511978]]
The value of l1 error on iter 2900 is [[-0.01822932]
[-0.01480356]
[ 0.01205031]
[ 0.01484854]]
The value of l1 error on iter 3000 is [[-0.0179128 ]
[-0.01454746]
[ 0.01184242]
[ 0.01459125]]
The value of l1 error on iter 3100 is [[-0.01761203]
[-0.0143041 ]
[ 0.01164485]
[ 0.01434676]]
The value of l1 error on iter 3200 is [[-0.01732577]
[-0.01407246]
[ 0.01145677]
[ 0.01411403]]
The value of l1 error on iter 3300 is [[-0.01705288]
[-0.01385162]
[ 0.01127746]
[ 0.01389217]]
The value of l1 error on iter 3400 is [[-0.01679236]
[-0.01364078]
[ 0.01110625]
[ 0.01368036]]
The value of l1 error on iter 3500 is [[-0.0165433 ]
[-0.01343921]
[ 0.01094255]
[ 0.01347786]]
The value of l1 error on iter 3600 is [[-0.0163049 ]
[-0.01324625]
[ 0.01078583]
[ 0.013284 ]]
The value of l1 error on iter 3700 is [[-0.01607641]
[-0.0130613 ]
[ 0.01063561]
[ 0.01309821]]
The value of l1 error on iter 3800 is [[-0.01585718]
[-0.01288382]
[ 0.01049146]
[ 0.01291993]]
The value of l1 error on iter 3900 is [[-0.01564659]
[-0.01271334]
[ 0.01035297]
[ 0.01274866]]
The value of l1 error on iter 4000 is [[-0.01544409]
[-0.0125494 ]
[ 0.01021979]
[ 0.01258398]]
The value of l1 error on iter 4100 is [[-0.01524919]
[-0.01239159]
[ 0.01009159]
[ 0.01242546]]
The value of l1 error on iter 4200 is [[-0.01506141]
[-0.01223954]
[ 0.00996806]
[ 0.01227273]]
The value of l1 error on iter 4300 is [[-0.01488034]
[-0.01209292]
[ 0.00984893]
[ 0.01212546]]
The value of l1 error on iter 4400 is [[-0.01470559]
[-0.01195141]
[ 0.00973394]
[ 0.01198331]]
The value of l1 error on iter 4500 is [[-0.0145368 ]
[-0.01181471]
[ 0.00962287]
[ 0.01184601]]
The value of l1 error on iter 4600 is [[-0.01437364]
[-0.01168257]
[ 0.00951548]
[ 0.01171328]]
The value of l1 error on iter 4700 is [[-0.0142158 ]
[-0.01155473]
[ 0.0094116 ]
[ 0.01158488]]
The value of l1 error on iter 4800 is [[-0.01406301]
[-0.01143097]
[ 0.00931102]
[ 0.01146058]]
The value of l1 error on iter 4900 is [[-0.013915 ]
[-0.01131108]
[ 0.00921358]
[ 0.01134017]]
The value of l1 error on iter 5000 is [[-0.01377152]
[-0.01119485]
[ 0.00911911]
[ 0.01122344]]
The value of l1 error on iter 5100 is [[-0.01363236]
[-0.01108212]
[ 0.00902748]
[ 0.01111021]]
The value of l1 error on iter 5200 is [[-0.01349729]
[-0.01097269]
[ 0.00893854]
[ 0.01100032]]
The value of l1 error on iter 5300 is [[-0.01336613]
[-0.01086643]
[ 0.00885216]
[ 0.0108936 ]]
The value of l1 error on iter 5400 is [[-0.01323869]
[-0.01076317]
[ 0.00876822]
[ 0.01078991]]
The value of l1 error on iter 5500 is [[-0.01311479]
[-0.01066279]
[ 0.00868661]
[ 0.01068909]]
The value of l1 error on iter 5600 is [[-0.01299429]
[-0.01056514]
[ 0.00860723]
[ 0.01059103]]
The value of l1 error on iter 5700 is [[-0.01287701]
[-0.01047011]
[ 0.00852997]
[ 0.0104956 ]]
The value of l1 error on iter 5800 is [[-0.01276283]
[-0.01037758]
[ 0.00845475]
[ 0.01040269]]
The value of l1 error on iter 5900 is [[-0.01265162]
[-0.01028745]
[ 0.00838146]
[ 0.01031218]]
The value of l1 error on iter 6000 is [[-0.01254323]
[-0.01019961]
[ 0.00831005]
[ 0.01022398]]
The value of l1 error on iter 6100 is [[-0.01243757]
[-0.01011397]
[ 0.00824041]
[ 0.01013798]]
The value of l1 error on iter 6200 is [[-0.0123345 ]
[-0.01003044]
[ 0.00817249]
[ 0.01005411]]
The value of l1 error on iter 6300 is [[-0.01223394]
[-0.00994893]
[ 0.00810621]
[ 0.00997227]]
The value of l1 error on iter 6400 is [[-0.01213579]
[-0.00986936]
[ 0.00804151]
[ 0.00989238]]
The value of l1 error on iter 6500 is [[-0.01203993]
[-0.00979166]
[ 0.00797833]
[ 0.00981436]]
The value of l1 error on iter 6600 is [[-0.0119463 ]
[-0.00971576]
[ 0.0079166 ]
[ 0.00973815]]
The value of l1 error on iter 6700 is [[-0.0118548 ]
[-0.00964159]
[ 0.00785628]
[ 0.00966368]]
The value of l1 error on iter 6800 is [[-0.01176536]
[-0.00956907]
[ 0.00779731]
[ 0.00959088]]
The value of l1 error on iter 6900 is [[-0.0116779 ]
[-0.00949817]
[ 0.00773964]
[ 0.00951969]]
The value of l1 error on iter 7000 is [[-0.01159234]
[-0.0094288 ]
[ 0.00768322]
[ 0.00945005]]
The value of l1 error on iter 7100 is [[-0.01150863]
[-0.00936092]
[ 0.00762802]
[ 0.0093819 ]]
The value of l1 error on iter 7200 is [[-0.01142669]
[-0.00929448]
[ 0.00757398]
[ 0.0093152 ]]
The value of l1 error on iter 7300 is [[-0.01134646]
[-0.00922943]
[ 0.00752107]
[ 0.00924989]]
The value of l1 error on iter 7400 is [[-0.01126789]
[-0.00916572]
[ 0.00746924]
[ 0.00918593]]
The value of l1 error on iter 7500 is [[-0.01119091]
[-0.0091033 ]
[ 0.00741847]
[ 0.00912327]]
The value of l1 error on iter 7600 is [[-0.01111548]
[-0.00904213]
[ 0.00736871]
[ 0.00906186]]
The value of l1 error on iter 7700 is [[-0.01104155]
[-0.00898217]
[ 0.00731994]
[ 0.00900167]]
The value of l1 error on iter 7800 is [[-0.01096906]
[-0.00892339]
[ 0.00727212]
[ 0.00894266]]
The value of l1 error on iter 7900 is [[-0.01089797]
[-0.00886573]
[ 0.00722522]
[ 0.00888478]]
The value of l1 error on iter 8000 is [[-0.01082824]
[-0.00880917]
[ 0.00717921]
[ 0.00882801]]
The value of l1 error on iter 8100 is [[-0.01075981]
[-0.00875368]
[ 0.00713406]
[ 0.00877231]]
The value of l1 error on iter 8200 is [[-0.01069267]
[-0.00869921]
[ 0.00708975]
[ 0.00871764]]
The value of l1 error on iter 8300 is [[-0.01062675]
[-0.00864575]
[ 0.00704626]
[ 0.00866397]]
The value of l1 error on iter 8400 is [[-0.01056203]
[-0.00859325]
[ 0.00700355]
[ 0.00861127]]
The value of l1 error on iter 8500 is [[-0.01049847]
[-0.00854169]
[ 0.0069616 ]
[ 0.00855952]]
The value of l1 error on iter 8600 is [[-0.01043604]
[-0.00849105]
[ 0.00692039]
[ 0.00850869]]
The value of l1 error on iter 8700 is [[-0.0103747 ]
[-0.00844129]
[ 0.00687991]
[ 0.00845874]]
The value of l1 error on iter 8800 is [[-0.01031442]
[-0.00839239]
[ 0.00684012]
[ 0.00840966]]
The value of l1 error on iter 8900 is [[-0.01025517]
[-0.00834433]
[ 0.00680102]
[ 0.00836142]]
The value of l1 error on iter 9000 is [[-0.01019693]
[-0.00829707]
[ 0.00676257]
[ 0.008314 ]]
The value of l1 error on iter 9100 is [[-0.01013966]
[-0.00825061]
[ 0.00672476]
[ 0.00826737]]
The value of l1 error on iter 9200 is [[-0.01008334]
[-0.00820492]
[ 0.00668758]
[ 0.00822151]]
The value of l1 error on iter 9300 is [[-0.01002794]
[-0.00815997]
[ 0.00665101]
[ 0.00817639]]
The value of l1 error on iter 9400 is [[-0.00997344]
[-0.00811575]
[ 0.00661502]
[ 0.00813201]]
The value of l1 error on iter 9500 is [[-0.00991981]
[-0.00807224]
[ 0.00657962]
[ 0.00808834]]
The value of l1 error on iter 9600 is [[-0.00986703]
[-0.00802941]
[ 0.00654477]
[ 0.00804537]]
The value of l1 error on iter 9700 is [[-0.00981509]
[-0.00798726]
[ 0.00651047]
[ 0.00800306]]
The value of l1 error on iter 9800 is [[-0.00976394]
[-0.00794576]
[ 0.0064767 ]
[ 0.00796142]]
The value of l1 error on iter 9900 is [[-0.00971359]
[-0.0079049 ]
[ 0.00644344]
[ 0.00792041]]
Output After Training:
[[ 0.00966449]
[ 0.00786506]
[ 0.99358898]
[ 0.99211957]]
In [ ]:
Content source: MattLoftus/MattLoftus.github.io
Similar notebooks: