def run_with_crossvalidation(self, data, iterations=5): """ This function estimates the performance of the neural network using crossvalidation using a specified dataset. Args: :param ds (ClassificationData): the dataset used to crossvalidate the neural network. iterations (int, optional): number of iterations for the crossvalidation. :returns: error (float): the average percent error of the dataset, tested on the neural network using crossvalidation. """ x = data.X y = data.y n, m = x.shape errors = np.zeros(iterations) cv = cross_validation.KFold(n, iterations, shuffle=True) i = 0 for train_index, test_index in cv: x_train = x[train_index, :] y_train = y[train_index] x_test = x[test_index, :] y_test = y[test_index] ds_train = ClassificationData.conv2DS(x_train, y_train) ds_test = ClassificationData.conv2DS(x_test, y_test) trainer = BackpropTrainer( self.network, dataset=ds_train, learningrate=self.learningrate, momentum=self.momentum, verbose=self.verbose, batchlearning=self.batchlearning) trainer.trainUntilConvergence( dataset=ds_train, maxEpochs=self.max_epochs, continueEpochs=self.con_epochs) errors[i] = percentError( trainer.testOnClassData(dataset=ds_test), ds_test['class']) i += 1 print "Multi class NN cross-validation test errors: " % errors return np.average(errors)
# Karol Dzitkowski # [email protected] # 10-10-2014 from pylab import * from csv_data import ClassificationData from classification import ClassificationNeuralNetwork from pybrain.utilities import percentError d_train = ClassificationData('data/class_2_train.csv') nClasses = d_train.DS.nClasses nn = ClassificationNeuralNetwork(2, nClasses, 6) # print nn.network nn.apply_custom_network([6,6]) # print nn.network #RUN NETWORK FOR CLASSIFICATION t = nn.run_with_crossvalidation(d_train) print 'train error', nn.test(d_train) d_test = ClassificationData('data/class_2_tst.csv') print 'test error', nn.test(d_test) # get error # result = t.testOnClassData(dataset=d_train.DS) # error = percentError(result, d_train.DS['class'])