Source code for pymlp.mlp.SupervisedTrainer

"""Trains a neural network supervised."""
import numpy as np


[docs]class SupervisedTrainer: """Trains two neural networks, one with stochastic and one with non-stochastic sampling.""" numberOfEpochs = 500 folderTemplate = "%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 = network self.trainingData = trainingData self.trainingTargets = trainingTargets self.testingData = testingData self.testingTargets = testingTargets self.folder = folder self.fullFoldername = folder + (self.folderTemplate % (str(network.layout), network.alpha)) self.postProcessListener = postProcessListener if postProcessListener != None else DummyListener()
[docs] def collectErrors(self, testingData, testingTargets): """ Collects the square error between the actual networks output and target values. Parameters: testingData -- the test data points testingTargets -- the target values for the testingData returns -- the square error between the actual results and target values """ results = self.network.forward(testingData) squareErrors = (results - testingTargets) ** 2 return squareErrors
[docs] def errorAfterTraining(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) return np.mean(squareErrors)
[docs] def trainNetworkStochastic(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) for i in xrange(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) return meanSquareErrors
[docs] def trainNetworkBatch(self): """ Uses batch-learning to train the network. """ meanSquareErrors = list() for i in xrange(self.numberOfEpochs): self.network.train(self.trainingData, self.trainingTargets, 1, batch=True) meanSquareError = np.mean(self.collectErrors(self.testingData, self.testingTargets)) meanSquareErrors.append(meanSquareError) self.postProcessListener.postProcess(self.network) return meanSquareErrors
[docs] def trainNetwork(self): """ Uses normal online learning to train the network. """ meanSquareErrors = list() for i in xrange(self.numberOfEpochs): meanSquareError = self.errorAfterTraining(self.network, self.trainingData, self.trainingTargets, self.testingData, self.testingTargets) meanSquareErrors.append(meanSquareError) self.postProcessListener.postProcess(self.network) return meanSquareErrors
[docs] def clone(self): """ Clones this SupervisedTrainer. """ return SupervisedTrainer(self.network.clone(), np.copy(self.trainingData), np.copy(self.trainingTargets), np.copy(self.testingData), np.copy(self.testingTargets), self.folder, self.postProcessListener)
[docs]class DummyListener(): """ Doesn't do anything, but makes the post process optional. """
[docs] def postProcess(self, network): """ Is called after each training epoch. """ pass