def forBP(self): # This function builds the lists diff[n_L], conn[l][n_l][n_(l-1)], and # end[n_l][mu_(l-1)] as ingredients for back propagation weights=self.weights outputs=self.outputs shape=self.shape t=self.answer o=list(self.outputs[shape[len(shape)-1]]) o.pop(0) self.o=o # Build dSig_matrix # dSig_matrix[l][nl] # This matrix is just a precalculation of the d(sigma)/dx evaluated at x_n_l. dSig_matrix=[0] # just to keep indicies in right place L=len(shape) l=1 while l<L: row=[] Nr=shape[l] nr=0 while nr<Nr: row.append(dSig(dotProd(outputs[l-1],weights[l][nr]))) nr+=1 dSig_matrix.append(row) l+=1 self.dSig_matrix=dSig_matrix # print "dSig_matrix: " # for i in range(len(dSig_matrix)): # print dSig_matrix[i] # print "" # Build diff diff=[] L=len(shape) N=shape[L-1] n=0 while n<N: diff.append(o[n]-t[n]) n+=1 self.diff=diff
def propagate(self): # Propagates an input across the ann. # Also regenerates a 2D ouputs matrix indexed outputs[layer][node] that is used in backpropagation. # Start building a new outputs matrix: outputs=[[1]] outputs[0].extend(self.inputs) # 0th element of outputs list are the inputs # Forward propagate across ann continuing to build outputs matrix shape=self.shape # layer l and node n indix bounds come from this. weights=self.weights # we will calculate arguments of sig from this. l=1 # Start at layer 1 to calculate outputs of layer 1 l_max=len(shape) while l<l_max: outputs_update=[1] n=0 while n<shape[l]: outputs_update.append(sig(dotProd(outputs[l-1],weights[l][n]))) n+=1 outputs.append(outputs_update) l+=1 self.outputs=outputs