In [1]:
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
matplotlib.style.use('ggplot')
from mpl_toolkits.mplot3d import Axes3D
import IPython.html.widgets as widg
from IPython.display import clear_output
import sys
import scipy.stats as sts
%matplotlib inline


:0: FutureWarning: IPython widgets are experimental and may change in the future.

In [2]:
class Network:
    def __init__(self, shape):
        """The base network class. This defines a simple feed-forward network with appropriate weights and biases.
        
        Arguments:
        shape (list-like): This defines the # of layers and # of neurons per layer in your network.
                           Each element of the array or list adds a new layer with the number neurons specified by the element.
        Variables:
        self.shape: see shape.
        self.weights: A list of numpy arrays containing the weights corresponding to each channel between neurons.
        self.biases: A list of numpy arrays containing the biases corresponding to each neuron.
        self.errors: A list of numpy arrays containing the error of each neurons in any iteration of the training process.
        self.eta: A float representing the learning rate.
        self.lam: A scale factor used in L2 regularization
        """
        
        self.shape = np.array(shape) #shape is array-like, i.e. (2,3,4) is a 2 input, 3 hidden node, 4 output network
        self.weights = [np.random.ranf((self.shape[i],self.shape[i-1]))*.1 for i in range(1,len(self.shape))]
        self.biases = [np.random.ranf((self.shape[i],))*.1 for i in range(1,len(self.shape))]
        self.errors = [np.random.ranf((self.shape[i],)) for i in range(1,len(self.shape))]
        self.eta = .1
        self.lam = .01
        self.wrong = 0
        self.total = 0
    def sigmoid(self, inputs):
        """Computes the sigmoid function of some input.
        
        Arguments:
        inputs (float or numpy array): The input or inputs to be fed through the sigmoid function.
        """
        
        return 1/(1+np.exp(-inputs))
    def feedforward(self, inputs):
        """Feeds inputs through the network and returns the output.
        
        Arguments:
        inputs (numpy array): The inputs to the network, must be the same size as the first(input) layer.
        
        Variables:
        self.activation: A list of numpy arrays corresponding to the output of each neuron in your network.
        """
        
        assert inputs.shape==self.shape[0] #inputs must feed directly into the first layer.
        self.activation = [np.zeros((self.shape[i],)) for i in range(len(self.shape))]
        self.activation[0] = inputs
        for i in range(1,len(self.shape)):
            self.activation[i]=self.sigmoid(np.dot(self.weights[i-1],self.activation[i-1])+self.biases[i-1])
        return self.activation[-1]
    def calc_learning_rate(self,grad):
        if grad>.85:
            self.eta=.1/grad**.1*1/(.25*(2*np.pi)**.5)*np.exp(-(grad)**2/(2*(.25)**2))
        else:
            self.eta=.1/grad**.6*1/(.4*(2*np.pi)**.5)*np.exp(-(grad)**2/(2*(.4)**2))*(grad+.08)
        self.wrong+=grad
        self.total+=grad
    def comp_error(self, answer):
        """Computes the errors of each neuron.(Typically called Back Propagation)
        
        Arguments:
        answers (numpy array): The expected output from the network.
        """
#         if (self.activation[-1]-answer).any>.15:
#             self.eta = .005
#         else: 
#             self.eta = .5
        self.calc_learning_rate(np.amax(np.abs((self.activation[-1]-answer))))
        #print(np.amax(np.abs((self.activation[-1]-answer))))
        assert answer.shape==self.activation[-1].shape
        self.errors[-1] = np.pi*np.tan(np.pi/2*(self.activation[-1]-answer))*1/np.cos(np.pi/2*(self.activation[-1]-answer))**2*np.exp(np.dot(self.weights[-1],self.activation[-2])+self.biases[-1])/(np.exp(np.dot(self.weights[-1],self.activation[-2])+self.biases[-1])+1)**2
        for i in range(len(self.shape)-2, 0, -1):
            self.errors[i-1] = self.weights[i].transpose().dot(self.errors[i])*np.exp(np.dot(self.weights[i-1],self.activation[i-1])+self.biases[i-1])/(np.exp(np.dot(self.weights[i-1],self.activation[i-1])+self.biases[i-1])+1)**2
    def grad_descent(self):
        """Changes each variable based on the gradient descent algorithm."""
        
        #for i in range(len(self.biases)):
         #   self.biases[i]=self.biases[i]-self.eta*self.errors[i]
        for i in range(len(self.weights)):
            self.biases[i]=self.biases[i]-self.eta*self.errors[i]
            for j in range(self.weights[i].shape[0]):
                for k in range(self.weights[i].shape[1]):
                    self.weights[i][j,k] = (1-self.eta*self.lam/1000)*self.weights[i][j,k] - self.eta*self.activation[i][k]*self.errors[i][j]
    def train(self, inputs, answer):
        """Trains the network.
        
        Arguments:
        inputs (numpy array): The inputs to the network, must be the same size as the first(input) layer.
        answers (numpy array): The expected output from the network, must be the same size as the last(output) layer.
        """
        
        self.feedforward(inputs)
        self.comp_error(answer)
        self.grad_descent()
    def get_fractional_err(self):
        return(self.wrong)

add piecewise def for learning rate


In [3]:
n1 = Network([2,15,1])
print n1.feedforward(np.array([1,2]))
for i in range(1000):
    n1.train(np.array([1,2]), np.array([.5]))
print n1.feedforward(np.array([1,2]))


[ 0.62458697]
[ 0.50024233]

In [4]:
from sklearn.datasets import load_digits
digits = load_digits()
print(digits.data[0]*.01)


[ 0.    0.    0.05  0.13  0.09  0.01  0.    0.    0.    0.    0.13  0.15
  0.1   0.15  0.05  0.    0.    0.03  0.15  0.02  0.    0.11  0.08  0.    0.
  0.04  0.12  0.    0.    0.08  0.08  0.    0.    0.05  0.08  0.    0.
  0.09  0.08  0.    0.    0.04  0.11  0.    0.01  0.12  0.07  0.    0.
  0.02  0.14  0.05  0.1   0.12  0.    0.    0.    0.    0.06  0.13  0.1   0.
  0.    0.  ]

In [5]:
num = []
for i in range(0,51):
    num.append(Network([64,7,10]))

In [6]:
# %timeit num.feedforward(digits.data[89]*.01)
# %timeit num.comp_error(np.eye(10)[digits.target[89]])
# %timeit num.grad_descent()

In [7]:
def Train_it(num, itera):
    iden = np.eye(10)
    acc = np.zeros((itera,))
    frac_err = np.zeros((itera,))
    trainer = zip(digits.data,digits.target)
    perm = np.random.permutation(trainer)
    trains = perm[:1000]
    test = perm[1001:]
    #num = Network([64, 14, 10])
    print num.feedforward(digits.data[89]*.01)
    for i in range(itera):
        print(float(100*i/(itera*1.0)))
        for dig, ans in trains:
            num.train(dig*.01,iden[ans])
        cor = 0
        tot = 0
        for dig, ans in test:
            if num.feedforward(dig*.01).argmax()==ans:
                cor += 1
            tot += 1
        acc[i] = cor/float(tot)
        frac_err[i] = num.get_fractional_err()
    return acc, frac_err

In [8]:
accu = np.zeros((50,1))
fracerr = np.zeros((50,1))
for i in range(50):
    print(i)
    accu[i], fracerr[i] = Train_it(num[i], 1)
print(accu)


0
[ 0.54665156  0.54156432  0.55756835  0.55958762  0.56182171  0.56829721
  0.56334126  0.56402282  0.57405461  0.55442191]
0.0
1
[ 0.54121921  0.56689558  0.56872768  0.55946418  0.57011306  0.54730528
  0.54462299  0.55660831  0.56218981  0.55190857]
0.0
2
[ 0.55901154  0.55175702  0.54113788  0.55684584  0.56502815  0.5657883
  0.56436593  0.5599947   0.55916243  0.56864924]
0.0
3
[ 0.55917745  0.54453012  0.55526192  0.55863285  0.5379503   0.56151904
  0.56670689  0.57082805  0.54486903  0.56068579]
0.0
4
[ 0.54125711  0.56592521  0.57502239  0.57689911  0.57093059  0.54813225
  0.55103731  0.56140474  0.57280598  0.54704628]
0.0
5
[ 0.54728482  0.5724975   0.55604339  0.57057547  0.55428472  0.54041472
  0.53548754  0.57636423  0.57128744  0.5464022 ]
0.0
6
[ 0.53004333  0.57674196  0.5487789   0.56193113  0.56604061  0.56953895
  0.55927519  0.54814158  0.56449464  0.55464425]
0.0
7
[ 0.5796739   0.57383721  0.57183371  0.59258735  0.54491452  0.56541129
  0.54023383  0.57692561  0.5608153   0.56069028]
0.0
8
[ 0.57230377  0.55347389  0.55028405  0.57155498  0.55355967  0.56370485
  0.54275443  0.54744635  0.55876524  0.54213897]
0.0
9
[ 0.57187154  0.544692    0.5737806   0.56424306  0.54711908  0.55772292
  0.57597888  0.55742106  0.55136002  0.55978215]
0.0
10
[ 0.59190494  0.53000075  0.59342108  0.55003061  0.56340202  0.55626523
  0.55443021  0.57568003  0.57589821  0.54234447]
0.0
11
[ 0.58021555  0.57560995  0.55196331  0.56367531  0.54773936  0.58883329
  0.56728928  0.56425398  0.54930022  0.56692972]
0.0
12
[ 0.57459372  0.55866825  0.53682192  0.55202439  0.56578867  0.58353848
  0.55689946  0.55883857  0.56133417  0.59049888]
0.0
13
[ 0.57501573  0.58331586  0.55277459  0.56498032  0.54931226  0.5719232
  0.57277109  0.55525682  0.55046927  0.5310004 ]
0.0
14
[ 0.57018286  0.54547413  0.57544712  0.5625147   0.55490903  0.57112999
  0.56505078  0.56201253  0.57839689  0.53868175]
0.0
15
[ 0.53639375  0.57573624  0.57156178  0.57695698  0.54822509  0.56934308
  0.54956132  0.56919887  0.56480985  0.54965895]
0.0
16
[ 0.5526197   0.56645952  0.55332398  0.56387049  0.56208553  0.56110087
  0.55066305  0.57021908  0.57861176  0.55796251]
0.0
17
[ 0.53982817  0.54163486  0.57448822  0.54485174  0.53495854  0.56531194
  0.56661196  0.56501923  0.57878     0.57785052]
0.0
18
[ 0.56760227  0.56960921  0.55129734  0.55094453  0.53895353  0.55440465
  0.57204858  0.55501227  0.58103577  0.55259152]
0.0
19
[ 0.57097864  0.55416701  0.56633558  0.53583852  0.54650017  0.56978276
  0.54893667  0.54556346  0.5724478   0.52544716]
0.0
20
[ 0.56105664  0.56170373  0.54476819  0.57940858  0.55944432  0.56075497
  0.55221355  0.57175692  0.55794345  0.55672465]
0.0
21
[ 0.56632935  0.54937391  0.58453425  0.56520587  0.5802594   0.54566141
  0.5649398   0.55616641  0.54776504  0.56662176]
0.0
22
[ 0.53829787  0.55712534  0.57897682  0.56791218  0.56167056  0.56520284
  0.60546569  0.54587726  0.57271992  0.55258519]
0.0
23
[ 0.55609715  0.55603347  0.54433031  0.55613698  0.54513887  0.56946333
  0.56131099  0.53398503  0.55163382  0.55973209]
0.0
24
[ 0.55234565  0.54803478  0.58635658  0.56368516  0.56899453  0.5574267
  0.57058338  0.56847693  0.5446054   0.54192158]
0.0
25
[ 0.55230396  0.56675793  0.55806971  0.54899865  0.57304589  0.55704237
  0.57707217  0.56246619  0.56960331  0.56082531]
0.0
26
[ 0.58845551  0.57472891  0.54702478  0.58172875  0.55104003  0.57573945
  0.57444203  0.56153257  0.55960916  0.54118662]
0.0
27
[ 0.56566493  0.58765113  0.56391398  0.54567908  0.57501326  0.57099286
  0.54807154  0.54795195  0.58005518  0.5335101 ]
0.0
28
[ 0.57308249  0.55118508  0.56630307  0.56053873  0.57514592  0.58227829
  0.56659452  0.58102013  0.55892269  0.55510058]
0.0
29
[ 0.54566133  0.55437093  0.55726672  0.5652053   0.54866676  0.56912734
  0.55149607  0.55150029  0.5575688   0.57367143]
0.0
30
[ 0.5538219   0.54327982  0.56514074  0.56669464  0.55888354  0.55720993
  0.55406573  0.54895573  0.5339182   0.58312418]
0.0
31
[ 0.55607438  0.57056652  0.55528655  0.54940326  0.5658841   0.58260264
  0.55028732  0.56275764  0.5688348   0.54397916]
0.0
32
[ 0.5654442   0.55307106  0.55807355  0.56706587  0.53564869  0.5619252
  0.56827306  0.56963812  0.54330592  0.55797482]
0.0
33
[ 0.57420078  0.55261915  0.58098833  0.56891774  0.5619806   0.57800713
  0.56337941  0.56067216  0.5814599   0.57133255]
0.0
34
[ 0.55385773  0.58473358  0.59201117  0.55390079  0.57202974  0.5706193
  0.54440922  0.55440551  0.55402195  0.56999125]
0.0
35
[ 0.54830407  0.54850456  0.56847768  0.57250668  0.55905219  0.55921268
  0.54566091  0.55320892  0.53878848  0.55465703]
0.0
36
[ 0.55857029  0.54652951  0.57100537  0.57243172  0.57079381  0.55361947
  0.56229006  0.55488502  0.56559071  0.55820006]
0.0
37
[ 0.56693501  0.56128978  0.52329901  0.56813066  0.56692332  0.53581981
  0.55075298  0.55976205  0.56842434  0.55725907]
0.0
38
[ 0.56353635  0.55596047  0.58116245  0.56417308  0.55522897  0.56111366
  0.56659779  0.55892024  0.55503338  0.57386407]
0.0
39
[ 0.56773782  0.54553972  0.55525937  0.56760775  0.56145225  0.55018423
  0.57587485  0.57051458  0.58440229  0.54216174]
0.0
40
[ 0.53886195  0.55297152  0.57690823  0.56707983  0.56035979  0.55793
  0.55900312  0.55110681  0.53894201  0.53460618]
0.0
41
[ 0.53170505  0.55488154  0.54395125  0.56880605  0.53076462  0.56727993
  0.57072407  0.54340115  0.54835825  0.56451998]
0.0
42
[ 0.55620623  0.54892967  0.57554445  0.55061905  0.57047591  0.56331612
  0.54580954  0.54643278  0.56598487  0.56818311]
0.0
43
[ 0.53486287  0.53588092  0.55710621  0.59068075  0.52929659  0.53974059
  0.56203798  0.58166806  0.57394315  0.58072686]
0.0
44
[ 0.54107146  0.54428966  0.55212444  0.57303166  0.54880705  0.55721958
  0.55890916  0.56335042  0.53816331  0.579062  ]
0.0
45
[ 0.55762219  0.55269976  0.55806707  0.54935196  0.55002362  0.55404651
  0.57429681  0.57118806  0.57420807  0.58058927]
0.0
46
[ 0.5509453   0.55149285  0.56860203  0.56506872  0.56141288  0.54951924
  0.56351815  0.53767164  0.56977565  0.56735624]
0.0
47
[ 0.56203669  0.56600769  0.56089017  0.54842827  0.57666673  0.58073516
  0.56296062  0.54087903  0.54232564  0.53678172]
0.0
48
[ 0.56258238  0.55848059  0.56804277  0.55326454  0.57310576  0.55265353
  0.55074499  0.58059774  0.54765031  0.56949556]
0.0
49
[ 0.57040246  0.54135069  0.55710542  0.54936774  0.55248734  0.56676824
  0.55361401  0.57215641  0.57221473  0.58778707]
0.0
[[ 0.11432161]
 [ 0.10050251]
 [ 0.09924623]
 [ 0.10301508]
 [ 0.09422111]
 [ 0.10552764]
 [ 0.10552764]
 [ 0.10427136]
 [ 0.09673367]
 [ 0.15577889]
 [ 0.08542714]
 [ 0.09798995]
 [ 0.11432161]
 [ 0.10175879]
 [ 0.09547739]
 [ 0.1080402 ]
 [ 0.09673367]
 [ 0.09170854]
 [ 0.09422111]
 [ 0.08542714]
 [ 0.09924623]
 [ 0.10427136]
 [ 0.09924623]
 [ 0.09045226]
 [ 0.10678392]
 [ 0.10301508]
 [ 0.09296482]
 [ 0.10678392]
 [ 0.09045226]
 [ 0.10427136]
 [ 0.09798995]
 [ 0.16708543]
 [ 0.09045226]
 [ 0.09924623]
 [ 0.1080402 ]
 [ 0.09422111]
 [ 0.09924623]
 [ 0.09547739]
 [ 0.09170854]
 [ 0.09547739]
 [ 0.08668342]
 [ 0.08417085]
 [ 0.09924623]
 [ 0.10552764]
 [ 0.09045226]
 [ 0.09673367]
 [ 0.10678392]
 [ 0.09296482]
 [ 0.10427136]
 [ 0.11557789]]

In [11]:
print(fracerr.argmax())
acc0, fracerr0 = Train_it(num[fracerr.argmax()], 200)


23
[ 0.3872491   0.3679      0.282111    0.3866667   0.34412149  0.30136423
  0.35301422  0.33283713  0.31035867  0.29758526]
0.0
0.5
1.0
1.5
2.0
2.5
3.0
3.5
4.0
4.5
5.0
5.5
6.0
6.5
7.0
7.5
8.0
8.5
9.0
9.5
10.0
10.5
11.0
11.5
12.0
12.5
13.0
13.5
14.0
14.5
15.0
15.5
16.0
16.5
17.0
17.5
18.0
18.5
19.0
19.5
20.0
20.5
21.0
21.5
22.0
22.5
23.0
23.5
24.0
24.5
25.0
25.5
26.0
26.5
27.0
27.5
28.0
28.5
29.0
29.5
30.0
30.5
31.0
31.5
32.0
32.5
33.0
33.5
34.0
34.5
35.0
35.5
36.0
36.5
37.0
37.5
38.0
38.5
39.0
39.5
40.0
40.5
41.0
41.5
42.0
42.5
43.0
43.5
44.0
44.5
45.0
45.5
46.0
46.5
47.0
47.5
48.0
48.5
49.0
49.5
50.0
50.5
51.0
51.5
52.0
52.5
53.0
53.5
54.0
54.5
55.0
55.5
56.0
56.5
57.0
57.5
58.0
58.5
59.0
59.5
60.0
60.5
61.0
61.5
62.0
62.5
63.0
63.5
64.0
64.5
65.0
65.5
66.0
66.5
67.0
67.5
68.0
68.5
69.0
69.5
70.0
70.5
71.0
71.5
72.0
72.5
73.0
73.5
74.0
74.5
75.0
75.5
76.0
76.5
77.0
77.5
78.0
78.5
79.0
79.5
80.0
80.5
81.0
81.5
82.0
82.5
83.0
83.5
84.0
84.5
85.0
85.5
86.0
86.5
87.0
87.5
88.0
88.5
89.0
89.5
90.0
90.5
91.0
91.5
92.0
92.5
93.0
93.5
94.0
94.5
95.0
95.5
96.0
96.5
97.0
97.5
98.0
98.5
99.0
99.5

In [9]:
for i in range(50):
    plt.figure(figsize=(15,10))
    plt.plot(np.linspace(0,20,20),fracerr[i])


/usr/local/lib/python2.7/dist-packages/matplotlib/pyplot.py:424: RuntimeWarning: More than 20 figures have been opened. Figures created through the pyplot interface (`matplotlib.pyplot.figure`) are retained until explicitly closed and may consume too much memory. (To control this warning, see the rcParam `figure.max_open_warning`).
  max_open_warning, RuntimeWarning)

In [12]:
np.save("Accuracy_Data_run_13", acc0)

In [14]:
plt.plot(acc0)
print(np.amax(acc0))


0.968592964824

In [25]:
def plot_epochs(az_angle, eleva):
    fig = plt.figure(figsize=(15, 10))
    ax = fig.add_subplot(111, projection='3d')
    X, Y = np.meshgrid(np.linspace(0,200,200), np.linspace(0,10, 10))
    ax.plot_surface(np.linspace(0,100,100),fracerr, accu)
    ax.view_init(elev=eleva, azim=az_angle)

In [26]:
widg.interact(plot_epochs, az_angle=(0, 360, 1), eleva=(0,20,1))



In [10]:
acc_max = []
fract_err = []
for i in range(50):
    acc_max.append(np.amax(accu[i]))
    fract_err.append(fracerr[i][0])
print(acc_max,fract_err)


([0.92587939698492461, 0.914572864321608, 0.91080402010050254, 0.87185929648241201, 0.90577889447236182, 0.91708542713567842, 0.91331658291457285, 0.90829145728643212, 0.92462311557788945, 0.91959798994974873, 0.89698492462311563, 0.914572864321608, 0.914572864321608, 0.91708542713567842, 0.93341708542713564, 0.93090452261306533, 0.90703517587939697, 0.93341708542713564, 0.92085427135678388, 0.91959798994974873, 0.89447236180904521, 0.91959798994974873, 0.89949748743718594, 0.93090452261306533, 0.92713567839195976, 0.91959798994974873, 0.90326633165829151, 0.88316582914572861, 0.90954773869346739, 0.92085427135678388, 0.90954773869346739, 0.90201005025125625, 0.93216080402010049, 0.90075376884422109, 0.90201005025125625, 0.91331658291457285, 0.9120603015075377, 0.90703517587939697, 0.93341708542713564, 0.91080402010050254, 0.9233668341708543, 0.90577889447236182, 0.93090452261306533, 0.9346733668341709, 0.91080402010050254, 0.92085427135678388, 0.89949748743718594, 0.90577889447236182, 0.92587939698492461, 0.90075376884422109], [665.33322314041993, 665.41809088732896, 666.06267905868879, 666.56847251040801, 666.02718624895761, 667.34202131726477, 666.75480802379764, 667.39533920312567, 668.29842255438064, 667.78008432522154, 666.59077027312378, 666.13395852406961, 665.89302926450341, 666.13841333659832, 666.58842910850922, 667.00761074982711, 666.38500947741534, 666.41208685683114, 666.99551309933531, 667.81436626606649, 665.92767854810495, 667.1045077291692, 666.85205441500898, 667.88288114987688, 667.43811462042663, 666.15871852929138, 666.8903893175974, 667.06964723397266, 667.85430738000741, 667.44622115221068, 666.65595939117588, 665.63609119108696, 666.09162117950518, 667.24463085663047, 666.43128957735132, 666.8648824267907, 667.11468612780982, 665.92015534416475, 665.94647406932768, 665.90525780436087, 665.42224355225994, 668.62239217850686, 666.43283666937657, 667.28289945657843, 668.27742212730709, 667.71759134607771, 667.4240892267901, 666.41040892501974, 667.29040984259871, 666.57454135583691])

In [11]:
plt.plot(fract_err, acc_max, "ro")


Out[11]:
[<matplotlib.lines.Line2D at 0x7f355280edd0>]

In [12]:
sts.pearsonr(fract_err, acc_max)


Out[12]:
(0.050482614923083119, 0.72772045942019747)

In [ ]: