def learnUtil(self,input,target,learningRate=0.01): """ This method will learn the weights for the different layers. """ delta=[] lnCases=input.shape[0] # Run the newtwork self.run(input) #Calulate the error factor and update the weights for index in reversed(range(self.layerCount)): if index==self.layerCount-1: # The last layer just before the output output_delta=self._layerOutput[index]-target.T squareErr=output_delta**2 error=np.sum(squareErr) delta.append(output_delta*utils.sigm(self._layerInput[index],True)) else: # For other layers output_delta=self.weights[index+1].T.dot(delta[-1]) delta.append(output_delta[:-1,:]*utils.sigm(self._layerInput[index],True)) # Compute Weight Vector for 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]])]) weightedDelta=np.sum(layerOutput[None,:,:].transpose(2,0,1)*delta[delta_index][None,:,:].transpose(2,1,0),axis=0) self.weights[index]-=learningRate*weightedDelta return error
def run(self, input): """ This method will run the network for given input and return the output. """ lnCases = input.shape[0] #Clear out the previous run network data self._layerInput = [] self._layerOutput = [] #Run the network for index in range(self.layerCount): if index == 0: #Input layerInput = self.weights[0].dot( np.vstack([input.T, np.ones([1, lnCases])])) else: #Intermediate layers layerInput = self.weights[index].dot( np.vstack([ self._layerOutput[-1], np.ones([1, self._layerOutput[-1].shape[1]]) ])) self._layerInput.append(layerInput) self._layerOutput.append(utils.sigm(layerInput)) return self._layerOutput[-1].T
def learnUtil(self, input, target, learningRate=0.01): """ This method will learn the weights for the different layers. """ delta = [] lnCases = input.shape[0] # Run the newtwork self.run(input) #Calulate the error factor and update the weights for index in reversed(range(self.layerCount)): if index == self.layerCount - 1: # The last layer just before the output output_delta = self._layerOutput[index] - target.T squareErr = output_delta**2 error = np.sum(squareErr) delta.append(output_delta * utils.sigm(self._layerInput[index], True)) else: # For other layers output_delta = self.weights[index + 1].T.dot(delta[-1]) delta.append(output_delta[:-1, :] * utils.sigm(self._layerInput[index], True)) # Compute Weight Vector for 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]]) ]) weightedDelta = np.sum( layerOutput[None, :, :].transpose(2, 0, 1) * delta[delta_index][None, :, :].transpose(2, 1, 0), axis=0) self.weights[index] -= learningRate * weightedDelta return error
def run(self,input): """ This method will run the network for given input and return the output. """ lnCases=input.shape[0] #Clear out the previous run network data self._layerInput=[] self._layerOutput=[] #Run the network for index in range(self.layerCount): if index==0: #Input layerInput=self.weights[0].dot(np.vstack([input.T,np.ones([1,lnCases])])) else: #Intermediate layers layerInput=self.weights[index].dot(np.vstack([self._layerOutput[-1],np.ones([1,self._layerOutput[-1].shape[1]])])) self._layerInput.append(layerInput) self._layerOutput.append(utils.sigm(layerInput)) return self._layerOutput[-1].T