In [267]:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from IPython.html.widgets import interact

from sklearn.datasets import load_digits
digits = load_digits()

In [268]:
#64 inputs, 12 hidden, 10 out

weights = [np.random.rand(64, 12) * 2 - 1, np.random.rand(12, 10) * 2 - 1]
bias = [np.random.rand(64, 12) * 2 - 1, np.random.rand(12, 10) * 2 - 1]
trainingdata = digits.data[0:1200]
traininganswers = digits.target[0:1200]
lc = 0.02

traininganswervectors = np.zeros((1796,10))
for n in range(1796):
    traininganswervectors[n][digits.target[n]] = 1

In [269]:
def sigmoid(z):
    return 1/(1 + np.exp(-z))

def sig_prime(z):
    return sigmoid(z) * (1 - sigmoid(z))

In [270]:
def feedforward(a):
    #for each hidden, calculate the value based on the inputs
    b = np.zeros(12)
    for n in range(12):
        b[n] = np.dot(weights[0][0:,n], a) + np.dot(bias[0][0:,n], a)
    
    #for each output, calculate based on hidden
    c = np.zeros(10)
    for n2 in range(10):
        c[n2] = np.dot(weights[1][0:,n2], b) + np.dot(bias[1][0:,n2], b)
        
    return c
    
            
            
def train(weights, bias, data, answers, lc):
    for n in range(len(data)):
        error = answers[n] - result
        
        print(answers[n])
        print(weights[0][n])
        
        weights[1][n] += lc * np.dot(sigmoid(error), weights[1][n])

In [271]:
feedforward(digits.data[0])


Out[271]:
array([  45.73067821,  -65.51526518, -167.42363904,  -30.43917571,
       -161.57878804,   96.75479624,  -12.01219924,  107.82343039,
         26.2166886 ,   12.86878529])

In [272]:
rslt = [0]*20

for n in range(20):
    rslt[n] = feedforward(digits.data[n])
    
train(weights, bias, rslt, traininganswervectors[0:20], lc)


[ 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
[ 0.17223572  0.77487304  0.44161826 -0.70710714  0.62464562 -0.1421721
 -0.59791706  0.27531967  0.90303204  0.7956525   0.87955     0.31686617]
[ 0.  1.  0.  0.  0.  0.  0.  0.  0.  0.]
[ 0.50502184 -0.78051992  0.70373724 -0.47198262  0.04331817 -0.4857729
  0.73559997  0.84600831  0.66922309 -0.31186487 -0.356202   -0.53102676]
[ 0.  0.  1.  0.  0.  0.  0.  0.  0.  0.]
[ 0.40327404  0.30597222  0.18731097  0.87431711  0.72857996  0.68869006
  0.03910635  0.25895696  0.42356902  0.73710036 -0.04470945 -0.85469014]
[ 0.  0.  0.  1.  0.  0.  0.  0.  0.  0.]
[ 0.84499876  0.85786671  0.9300949  -0.55536741  0.96869833  0.56881235
 -0.63631002 -0.94854781  0.95498874 -0.70783425  0.60482855 -0.43006035]
[ 0.  0.  0.  0.  1.  0.  0.  0.  0.  0.]
[ 0.29733258  0.37350541  0.57030862 -0.21272455  0.69364891  0.307631
 -0.18279301 -0.2071291  -0.60867368 -0.26787461 -0.18049793 -0.77118644]
[ 0.  0.  0.  0.  0.  1.  0.  0.  0.  0.]
[ 0.04758651  0.91050722 -0.84952236 -0.00791262  0.03259276 -0.63196553
 -0.24030216 -0.45683171 -0.33863859  0.92616593 -0.34340886 -0.98023655]
[ 0.  0.  0.  0.  0.  0.  1.  0.  0.  0.]
[-0.84122863 -0.10197012  0.55107294  0.70254703 -0.91043956  0.76732974
  0.27459924 -0.78820463  0.5273853  -0.44320453  0.8098377   0.71073801]
[ 0.  0.  0.  0.  0.  0.  0.  1.  0.  0.]
[ 0.57842447  0.87499057  0.67349008  0.64336643 -0.3925641   0.81582663
 -0.50187774  0.2364034  -0.29497507  0.22362013  0.88011322  0.13894235]
[ 0.  0.  0.  0.  0.  0.  0.  0.  1.  0.]
[-0.13560598 -0.92957156 -0.20811626  0.76813814  0.53328373 -0.34029257
  0.8952066  -0.34814121 -0.10395488  0.09534092  0.40194885  0.59094896]
[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  1.]
[-0.92119953  0.74667134  0.84265834  0.87225345  0.74198966 -0.76693775
  0.83638421 -0.98279046  0.97740783 -0.72415117 -0.01481134  0.51981319]
[ 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
[-0.97255459 -0.93020343  0.57386959 -0.09331772  0.56729668 -0.60869018
  0.87270179 -0.11759648  0.8258432   0.69077848 -0.12008765 -0.73318091]
[ 0.  1.  0.  0.  0.  0.  0.  0.  0.  0.]
[-0.52903661 -0.81081832 -0.45408613 -0.5474011   0.81124744 -0.68286632
 -0.92777085 -0.74848073 -0.11676064 -0.40304484 -0.42705132  0.8347417 ]
[ 0.  0.  1.  0.  0.  0.  0.  0.  0.  0.]
[-0.00134142 -0.87068458  0.60077158 -0.5852186   0.9714261  -0.02093015
  0.51867965 -0.50508496  0.9563395   0.81649933  0.1787545   0.08859075]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-272-120bb09d8523> in <module>()
      4     rslt[n] = feedforward(digits.data[n])
      5 
----> 6 train(weights, bias, rslt, traininganswervectors[0:20], lc)

<ipython-input-270-b1bdecdcce3e> in train(weights, bias, data, answers, lc)
     21         print(weights[0][n])
     22 
---> 23         weights[1][n] += lc * np.dot(sigmoid(error), weights[1][n])
     24 
     25 

IndexError: index 12 is out of bounds for axis 0 with size 12

In [ ]: