Navigation

Source code for pymlp.mlp.SupervisedTrainer

"""Trains a neural network supervised."""importnumpyasnp

[docs]classSupervisedTrainer:"""Trains two neural networks, one with stochastic and one with non-stochastic sampling."""numberOfEpochs=500folderTemplate="%s_%s/"def__init__(self,network,trainingData,trainingTargets,testingData,testingTargets,folder,postProcessListener=None):""" Constructor. Parameters: network -- the network to train trainingData -- the training data trainingTargets -- the target values for the training data testingData -- the data for testing, used to measure the learning progress testingTargets -- the target values for the testingData folder -- folder to store the results in postProcessListener -- listener to be called after each learning and traning epoch """self.network=networkself.trainingData=trainingDataself.trainingTargets=trainingTargetsself.testingData=testingDataself.testingTargets=testingTargetsself.folder=folderself.fullFoldername=folder+(self.folderTemplate%(str(network.layout),network.alpha))self.postProcessListener=postProcessListenerifpostProcessListener!=NoneelseDummyListener()

[docs]deferrorAfterTraining(self,network,trainingData,trainingTargets,testingData,testingTargets):""" Calculates the mean error after a training epoch. Parameters: network -- the network trainingData -- the dataset for training trainingTargets -- the targets for training testingData -- the dataset for testing testingTargets -- the targets for the testing data returns -- the mean square error for the testing datasets """network.train(trainingData,trainingTargets,1)squareErrors=self.collectErrors(testingData,testingTargets)returnnp.mean(squareErrors)

[docs]deftrainNetworkStochastic(self):""" Trains the network stochastically. It shuffles the order in which the training datapoints are presented to the network. """meanSquareErrors=list()shuffledTrainingData=np.copy(self.trainingData)inputWidth=self.trainingData.shape[1]targetWidth=self.trainingTargets.shape[1]shuffledTrainingTargets=np.copy(self.trainingTargets)foriinxrange(self.numberOfEpochs):dataToShuffle=np.concatenate((shuffledTrainingData,shuffledTrainingTargets),axis=1)np.random.shuffle(dataToShuffle)shuffledTrainingData=dataToShuffle[:,0:inputWidth]shuffledTrainingTargets=dataToShuffle[:,inputWidth:targetWidth+inputWidth]meanSquareError=self.errorAfterTraining(self.network,shuffledTrainingData,shuffledTrainingTargets,self.testingData,self.testingTargets)meanSquareErrors.append(meanSquareError)self.postProcessListener.postProcess(self.network)returnmeanSquareErrors