numpy - Index Out of Bounds Python in Neural Network script -


i new python , (unfortunately) first time i've wrote relatively longer section of code in it. have been following tutorial writing neural network have run across error message cannot seem resolve. searched stack overflow "indexerror: list index out of range" , understand it's error trying access n-th element of list has n-1 elements. however, can't determine list incorrect , how fix. appreciated , way run script give me more information list out of index super helpful. below error message , code...

i keep getting error message reads,

(2, 2, 1) [array([[ 0.09438987,  0.08228006, -0.00851927],        [-0.09384243, -0.07417094,  0.1341281 ]]), array([[-0.20913607,  0.02783653, -0.07682221]])] traceback (most recent call last):   file "backprop.py", line 126, in <module>     err = bpn.trainepoch(lvinput, lvtarget)   file "backprop.py", line 93, in trainepoch     weightdelta = np.sum(layeroutput[none,:,:].transpose(2, 0, 1) * delta[delta_index][none,:,:].transpose(2, 1, 0), axis = 0) indexerror: list index out of range 

...and code....

import numpy np$ $ class backpropagationnetwork:$     """a back-propagation network"""$ $     #$     # class members$     #$     layercount = 0$     shape = none$     weights = []$ $     #$     # class methods$     #$     def __init__(self, layersize):$         """initialize network"""$ $         # layer info$         self.layercount = len(layersize) - 1$         self.shape = layersize$ $         #input/output data last run$         self._layerinput = []$         self._layeroutput = []$ $         # create weight arrays$         (l1, l2) in zip(layersize[:-1], layersize[1:]):$             self.weights.append(np.random.normal(scale=0.1, size = (l2, l1+1)))$ $     #$     # run method$     #$     def run(self, input):$         """run network based on input data"""$ $         lncases = input.shape[0]$ $         #clear out previous intermediate value lists$         self._layerinput = []$         self._layeroutput = []$ $         # run it$ #$     def run(self, input):$         """run network based on input data"""$ $         lncases = input.shape[0]$ $         #clear out previous intermediate value lists$         self._layerinput = []$         self._layeroutput = []$ $         # run it$         index in range(self.layercount):$             # determine layer input$             if index == 0:$                 layerinput = self.weights[0].dot(np.vstack([input.t, np.ones([1, lncases])]))$             else:$                 layerinput = self.weights[index].dot(np.vstack([self._layeroutput[-1], np.ones([1, lncases])]))$ $             self._layerinput.append(layerinput)$             self._layeroutput.append(self.sgm(layerinput))$ $         return self._layeroutput[-1].t$ $     #$     # trainepoch method$     #$     def trainepoch(self, input, target, trainingrate = 0.2):$         """this method trains network 1 epoch"""$ $         delta = []$         lncases = input.shape[0]$ $         # first run network$         self.run(input)$ $         # calculate our deltas$         index in reversed(range(self.layercount)):$             if index == self.layercount - 1:$                 # compare target values$                 output_delta = self._layeroutput[index] - target.t$                 error = np.sum(output_delta**2)$                 delta.append(output_delta * self.sgm(self._layerinput[index], true))$             else:$                 # compare following layer's delta$                 delta_pullback = self.weights[index + 1].t.dot(delta[-1])$                 delta.append(delta_pullback[:-1, :] * self.sgm(self._layerinput[index], true))$             # compute weight deltas$             index in range(self.layercount):$                 delta_index = self.layercount - 1 - index$ $                 if index == 0:$                     layeroutput = np.vstack([input.t, np.ones([1, lncases])])$                 else:$                     layeroutput = np.vstack([self._layeroutput[index - 1], np.ones([1, self._layeroutput[index - 1].shape[1]])])$ $                 weightdelta = np.sum(layeroutput[none,:,:].transpose(2, 0, 1) * delta[delta_index][none,:,:].transpose(2, 1, 0), axis = 0)$                 self.weights[index] -= trainingrate * weightdelta$ $             return error$ $ $ $ $ $ $     # transfer functions$     def sgm(self, x, derivative=false):$         if not derivative:$             return 1/(1+np.exp(-x))$         else:$             out = self.sgm(x)$             return out*(1-out)$ $ $ #$ # if run script, create test object$ #$ if __name__ == "__main__":$     bpn = backpropagationnetwork((2,2,1))$     print(bpn.shape)$     print(bpn.weights)$ $     lvinput = np.array([[0, 0], [1, 1], [0,1], [1,0]])$     lvtarget = np.array([[0.05], [0.05], [0.95], [0.95]])$ $     lnmax = 100000$     lnerr = 1e-5$     in range(lnmax-1):$         err = bpn.trainepoch(lvinput, lvtarget)$         if % 2500 == 0:$             print("iteration {0}\terror: {1:0.6f}".format(i, err))$         if err <= lnerr:$             print("minimum error reached @ iteration {0}".format(i))$             break$ $     # display output$     lvoutput = bpn.run(lvinput)$     print("input: {0}\noutput: {1}".format(lvinput, lvoutput))$ $ 

it looks second loop in trainepoch indented far, , that's what's causing indexerror. in other words, line reads:

for index in range(self.layercount): 

should @ same indentation level other loop:

for index in reversed(range(self.layercount)): 

Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -