def crossValidation(attributes, attributes_types, target_class, folds, b, k): config_file = './data/configs/network.txt' initial_weights_file = './data/configs/initial_weights.txt' dataset_file = './data/datasets/wine.txt' accuracy_values = [] precision_values = [] recall_values = [] fmeasure_values = [] for i in range(k): training_set_folds = list(folds) training_set_folds.remove(folds[i]) training_set = transformToList(training_set_folds) # bootstrap tem o tamanho do conjunto de treinamento bootstrap_size = len(training_set) test_set = folds[i] forest = [] for j in range(b): bootstrap = getBootstrap(training_set, bootstrap_size) neurons_per_layer = [1, 2, 1] network = NeuralNetwork(config_file=config_file, dataset_file=dataset_file, initial_weights_file=initial_weights_file, neurons_per_layer=neurons_per_layer) network.backpropagation() forest.append(network) # Usa o ensemble de B arvores para prever as instancias do fold i # (fold de teste) e avaliar desempenho do algoritmo true_positives, false_positives, false_negatives, true_negatives = evaluateForest( forest, test_set, target_class) accuracy_values.append( calculateAccuracy(true_positives, true_negatives, false_positives, false_negatives)) precision_value = calculatePrecision(true_positives, false_positives) precision_values.append(precision_value) recall_value = calculateRecall(true_positives, false_negatives) recall_values.append(recall_value) fmeasure_values.append( calculateF1Measure(precision_value, recall_value)) accuracy = sum(accuracy_values) / len(accuracy_values) precision = sum(precision_values) / len(precision_values) recall = sum(recall_values) / len(recall_values) fmeasure = sum(fmeasure_values) / len(fmeasure_values) return accuracy, precision, recall, fmeasure
def main(): config_file = './data/configs/network.txt' initial_weights_file = './data/configs/initial_weights.txt' dataset_file = './data/datasets/wine.txt' fileUtils = FileUtils(dataset_file=dataset_file, config_file=config_file) dataset = fileUtils.getDataset() #normalized_dataset = normalizeDataset(dataset) neurons_per_layer = [1, 2, 1] network = NeuralNetwork(config_file=config_file, dataset=dataset, initial_weights_file=initial_weights_file, neurons_per_layer=neurons_per_layer) network.backpropagation()
x = numpy.array([[0,0], [0,1], [1,0], [1,1]]) target = numpy.array([[0] ,[1] ,[1] ,[0]]) #setting number of inputs and number of outputs in the neural network _ , xColumns = x.shape _ , targetColumns = target.shape neuralNetwork = NeuralNetwork(learning_rate=0.1,n_in=xColumns,n_hidden=2,n_out=targetColumns,activation='tanh',momentum=0.9) neuralNetwork.initialize_weights() neuralNetwork.backpropagation(x,target,maxIterations=10000, batch=False) # Network result after training estimation = neuralNetwork .feed_forward(x) printSeparator = "----------------" print "Estimated values:" print estimation print printSeparator print "Target values:" print target print printSeparator estimationError = EstimationError(estimatedValues=estimation,targetValues=target) estimationError.computeErrors()
class NeuralNetworkTest(unittest.TestCase): def setUp(self): self.neuralNetwork = NeuralNetwork(learning_rate=0.15,n_hidden=2,momentum=0.95,activation='tanh') self.acceptanceEpsilon = 0.05 self.seed = 1 self.maxIterations = 11000 def tearDown(self): del self.neuralNetwork del self.acceptanceEpsilon del self.seed del self.maxIterations def retrieveEstimationError(self,x,target): #setting number of inputs and number of outputs in the neural network _ , xColumns = x.shape _ , targetColumns = target.shape self.neuralNetwork.n_in = xColumns self.neuralNetwork.n_out = targetColumns self.neuralNetwork.initialize_weights() self.neuralNetwork.backpropagation(x,target,maxIterations=self.maxIterations) # Network result after training estimation = self.neuralNetwork.feed_forward(x) estimationError = EstimationError(estimatedValues=estimation,targetValues=target) estimationError.computeErrors() totalError = estimationError.getTotalError() return totalError def testXOR(self): numpy.random.seed(seed=self.seed) x = numpy.array([[0,0], [0,1], [1,0], [1,1]]) target = numpy.array([[0] ,[1] ,[1] ,[0]]) totalError = self.retrieveEstimationError(x,target) print 'Error XOR:',totalError self.assertTrue(totalError<=self.acceptanceEpsilon) def testOR(self): numpy.random.seed(seed=self.seed) x = numpy.array([[0,0], [0,1], [1,0], [1,1]]) target = numpy.array([[0] ,[1] ,[1] ,[1]]) totalError = self.retrieveEstimationError(x,target) print 'Error OR:',totalError self.assertTrue(totalError<=self.acceptanceEpsilon) def testAND(self): numpy.random.seed(seed=self.seed) x = numpy.array([[0,0], [0,1], [1,0], [1,1]]) target = numpy.array([[0] ,[0] ,[0] ,[1]]) totalError = self.retrieveEstimationError(x,target) print 'Error AND:',totalError self.assertTrue(totalError<=self.acceptanceEpsilon) #test (x1 or x2) and x3 def testORAND(self): numpy.random.seed(seed=self.seed) x = numpy.array([[0,0,0], [0,0,1], [0,1,0], [1,0,0], [0,1,1], [1,1,0], [1,0,1], [1,1,1]]) target = numpy.array([[0] ,[0] ,[0] ,[0] ,[1] ,[0] ,[1] ,[1]]) totalError = self.retrieveEstimationError(x,target) print 'Error ORAND:',totalError self.assertTrue(totalError<=self.acceptanceEpsilon) #test (x1 and x2) or x3 def testANDOR(self): numpy.random.seed(seed=self.seed) x = numpy.array([[0,0,0], [0,0,1], [0,1,0], [1,0,0], [0,1,1], [1,1,0], [1,0,1], [1,1,1]]) target = numpy.array([[0] ,[1] ,[0] ,[0] ,[1] ,[1] ,[1] ,[1]]) totalError = self.retrieveEstimationError(x,target) print 'Error ANDOR:',totalError self.assertTrue(totalError<=self.acceptanceEpsilon)
training_data = zip(training_input, training_target) random.shuffle(training_data) training_input[:], training_target[:] = zip(*training_data) training_target = np.asarray(training_target) # Network configuration input_units = len(training_input[0]) output_units = 1 n_hidden = 200 momentum = 0.9 neuralNetwork = NeuralNetwork(learning_rate=0.01,n_in=input_units,n_hidden=n_hidden,n_out=output_units, momentum = momentum, activation='sigmoid') neuralNetwork.initialize_weights() results_file = ''.join(['results_lr',str(neuralNetwork.learning_rate),'_m',str(momentum),'_',str(n_hidden),"hidden",'_gender_classification']+['.out']) neuralNetwork.backpropagation(training_input,training_target,maxIterations=500, batch= False,file_name=results_file) network_file = ''.join(['trained_lr',str(neuralNetwork.learning_rate),'_m',str(momentum),'_',str(n_hidden),"hidden",'_gender_classification']+['.nn']) pickle.dump(neuralNetwork, file(network_file,'wb')) nn2 = pickle.load(file(network_file,'rb')) # Testing network on directories test_dir = 'test' male_dir = 'male' female_dir = 'female' print 'Test for %s directory' % (test_dir) getTestData(test_dir,nn2) print 'Test for %s directory' % (male_dir) getTestData(male_dir,nn2) print 'Test for %s directory' % (female_dir) getTestData(female_dir,nn2)
data = dataset.readlines() for entry in data: (x,y,area) = entry.split() x = float(x) y = float(y) area = int(area) if area==-1: area = 0 x_input.append([x,y]) target.append([area]) x_input = numpy.array(x_input) target = numpy.array(target) numpy.random.seed(seed=1) #using fixed seed for testing purposes _ , xColumns = x_input.shape _ , targetColumns = target.shape n_hidden = 8 momentum = 0 neuralNetwork = NeuralNetwork(learning_rate=0.01,n_in=xColumns,n_hidden=n_hidden,n_out=targetColumns, momentum = momentum) neuralNetwork.initialize_weights() results_file = ''.join(['results_lr',str(neuralNetwork.learning_rate),'_m',str(momentum),'_',str(n_hidden),"hidden",file_name.rsplit('.', 1)[0]]+['.out']) neuralNetwork.backpropagation(x_input,target,maxIterations=10000, batch= False,file_name=results_file) network_file = ''.join(['trained_lr',str(neuralNetwork.learning_rate),'_m',str(momentum),'_',str(n_hidden),"hidden",file_name.rsplit('.', 1)[0]]+['.nn']) pickle.dump(neuralNetwork, file(network_file,'wb')) print neuralNetwork.feed_forward(x_input) nn2 = pickle.load(file(network_file,'rb')) print network_file print nn2.feed_forward(x_input)