def app(): logger = logging.getLogger() logger.setLevel(logging.INFO) cfg = Configuration() cfg.parse() reader = Reader(cfg.snapshots_path) reader.do() repository = MySQLDriver(cfg.storage) repository.insert( list(divide_sequence_into_chunks(reader.users, cfg.batch_size)))
def evaluate(self, path_to_checkpoint, path_to_tfrecords_file, num_examples, global_step): batch_size = 50 num_batches = int(num_examples / batch_size) with tf.Graph().as_default(): images, labels = Reader.build_batch(path_to_tfrecords_file, file_length=num_examples, batch_size=batch_size, shuffled=False) logits = CNN.model(images) predictions = tf.argmax(logits, axis=1) accuracy, update_accuracy = tf.metrics.accuracy( labels=labels, predictions=predictions) tf.summary.scalar('validation_accuracy', accuracy) summary = tf.summary.merge_all() with tf.Session() as sess: sess.run([ tf.global_variables_initializer(), tf.local_variables_initializer() ]) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) restorer = tf.train.Saver() restorer.restore(sess, path_to_checkpoint) for _ in range(num_batches): sess.run(update_accuracy) accuracy_val, summary_val = sess.run([accuracy, summary]) self.summary_writer.add_summary(summary_val, global_step=global_step) coord.request_stop() coord.join(threads) return accuracy_val
k=k, linear=linear, cross=cross_validation) if cross_validation: p.crossValidation(exercise) else: p.algorithm(exercise) else: p = MultiLayerPerceptron(eta, beta, epochs, hidden_layers, nodes_per_layer, error_tolerance, adaptive_eta, delta_accuracy_error, training_size, momentum, adaptive_eta_increase, adaptive_eta_decrease, cross_validation, k) if cross_validation: r = Reader('Ej3') train_data, test_data = r.readFile(training_size, k, False, cross_validation) data = train_data last_partition = 0 split_number = 10 / k for i in range(1, k): partition = i * split_number test_data = data[int(last_partition):int(partition)] train_data1 = data[int(last_partition):] train_data2 = data[int(partition):] train_data = np.concatenate((train_data1, train_data2)) last_partition = partition p.algorithm_cross_validation("EVEN", train_data, test_data) else: p.algorithm("EVEN")
from convolutional_nn import CNN from meta import Meta if __name__ == '__main__': test_path = 'Input/test.tfrecords' batch_size = 100 meta_dict = Meta().load_dict('Input/batches_meta.json') num_examples = meta_dict['num_examples']['test'] num_batches = int(num_examples / batch_size) print('===> EVALUATING LAST MODEL ON TEST SAMPLES .....') with tf.Graph().as_default(): images, labels = Reader.build_batch(test_path, batch_size=batch_size, file_length=num_examples, shuffled=False) logits = CNN.model(images) predictions = tf.argmax(logits, axis=1) accuracy, update_accuracy = tf.metrics.accuracy( labels=labels, predictions=predictions) with tf.Session() as sess: sess.run([ tf.global_variables_initializer(), tf.local_variables_initializer() ]) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord)
def readCommonWords(self): """Reads the list of common words that should not be included in the scoring of each sentence such as \"and\" and \"or\".""" common_words = Reader("common_words.txt").open_file() return common_words
def algorithm(self, problem): # bias x y out if problem == "XOR": data = [[1.0, 1.0, 1.0, -1.0], [1.0, -1.0, 1.0, 1.0], [1.0, 1.0, -1.0, 1.0], [1.0, -1.0, -1.0, -1.0]] if problem == "EVEN": r = Reader('Ej3') data = r.readFile(size=self.training_set_size) self.M = self.total_layers - 1 # M sera el indice de la capa superior self.nodes_per_layer = max( self.nodes_per_layer, len(data[0]) - 1) # Cuantos nodos hay en las capas ocultas (incluye el del bias) self.exit_nodes = 1 # Cuantos nodos hay en la capa superior self.V = np.zeros((self.M + 1, self.nodes_per_layer)) # [capa, j] for i in range(1, self.M): self.V[i][0] = 1 # Bias para cada capa self.W = np.random.rand( self.M + 1, self.nodes_per_layer, self.nodes_per_layer) - 0.5 # [capa destino, dest, origen] w = np.random.rand(self.nodes_per_layer, len(data[0]) - 1) - 0.5 # [dest, origen] self.W[1, :, :] = np.zeros( (self.nodes_per_layer, self.nodes_per_layer)) self.d = np.zeros((self.M + 1, self.nodes_per_layer)) for orig in range(len(data[0]) - 1): for dest in range(self.nodes_per_layer): self.W[1, dest, orig] = w[dest, orig] error_min = len(data) * 2 positivity_margin = self.classification_margin total_error = 1 error_per_epoch = [] worst_error_per_epoch = [] accuracy = [] plotter = Plotter() if problem == "EVEN": test_data = r.readFile(size=self.training_set_size, test=True) else: test_data = [[1.0, 1.0, 1.0, -1.0], [1.0, -1.0, 1.0, 1.0], [1.0, 1.0, -1.0, 1.0], [1.0, -1.0, -1.0, -1.0]] test_error_per_epoch = [] test_worst_error_per_epoch = [] test_accuracy = [] # LOOP for epoch in range(1, self.iterations): total_error = 0 worst_error_this_epoch = 0 positives = 0 negatives = 0 # Randomize W every once in a while if (epoch % 100000 == 99999): self.W = np.random.rand( self.M + 1, self.nodes_per_layer, self.nodes_per_layer) - 0.5 # [capa destino, dest, origen] w = np.zeros( (nodes_per_layer, len(data[0]) - 1)) # [dest, origen] self.W[1, :, :] = np.zeros( (self.nodes_per_layer, self.nodes_per_layer)) for orig in range(len(data[0]) - 1): for dest in range(nodes_per_layer): self.W[1, dest, orig] = w[dest, orig] np.random.shuffle(data) for mu in range(len(data)): # Paso 2 (V0 tiene los ejemplos iniciales) for k in range(len(data[0]) - 1): self.V[0][k] = data[mu][k] # Paso 3A (Vi tiene los resultados de cada perceptron en la capa m) for m in range(1, self.M): for i in range(1, self.nodes_per_layer): hmi = self.h(m, i, self.nodes_per_layer, self.W, self.V) self.V[m][i] = self.g(hmi) # Paso 3B (En la ultima capa habra exit_nodes en vez de nodes_per_layer) for i in range(0, self.exit_nodes): hMi = self.h(self.M, i, self.nodes_per_layer, self.W, self.V) self.V[self.M][i] = self.g(hMi) upper_limit = data[mu][-1] + positivity_margin lower_limit = data[mu][-1] - positivity_margin if self.V[self.M][i] >= lower_limit and self.V[ self.M][i] <= upper_limit: positives += 1 else: negatives += 1 # Paso 4 (Calculo error para capa de salida M) for i in range(0, self.exit_nodes): hMi = self.h(self.M, i, self.nodes_per_layer, self.W, self.V) if self.exit_nodes == 1: self.d[self.M][i] = self.g_derivative(hMi) * ( data[mu][-1] - self.V[self.M][i]) else: self.d[self.M][i] = self.g_derivative(hMi) * ( data[mu][-1][i] - self.V[self.M][i]) # Paso 5 (Retropropagar error) for m in range(self.M, 1, -1): # m es la capa superior for j in range( 0, self.nodes_per_layer): # Por cada j en el medio hprevmi = self.h(m - 1, j, self.nodes_per_layer, self.W, self.V) # hj = hj del medio error_sum = 0 for i in range(0, self.nodes_per_layer ): # Por cada nodo en la capa superior error_sum += self.W[m, i, j] * self.d[m][ i] # sumo la rama de aca hasta arriba y multiplico por el error self.d[m - 1][j] = self.g_derivative(hprevmi) * error_sum # Paso 6 (Actualizar pesos) for m in range(1, self.M + 1): for i in range(self.nodes_per_layer): for j in range(self.nodes_per_layer): delta = self.alpha * self.d[m][i] * self.V[m - 1][j] self.W[m, i, j] = self.W[m, i, j] + delta # Paso 7 (Calcular error) for i in range(0, self.exit_nodes): if abs(data[mu][-1] - self.V[self.M][i]) > worst_error_this_epoch: worst_error_this_epoch = abs(data[mu][-1] - self.V[self.M][i]) if self.exit_nodes == 1: total_error += abs(data[mu][-1] - self.V[self.M][i]) else: total_error += abs(data[mu][-1][i] - self.V[self.M][i]) error_per_epoch.append(total_error / len(data)) worst_error_per_epoch.append(worst_error_this_epoch) accuracy.append(positives / (0.0 + positives + negatives)) if self.adaptive and epoch % 10 == 0: self.adjust_learning_rate(error_per_epoch) if total_error < error_min: error_min = total_error self.w_min = self.W if total_error <= self.error_tolerance * len( data) or epoch == self.iterations - 1: self.test_perceptron(test_data, self.w_min, epoch, test_error_per_epoch, test_worst_error_per_epoch, test_accuracy, positivity_margin, True) break else: self.test_perceptron(test_data, self.w_min, epoch, test_error_per_epoch, test_worst_error_per_epoch, test_accuracy, positivity_margin, False) plotter.create_plot_ej3(error_per_epoch, worst_error_per_epoch, test_error_per_epoch, test_worst_error_per_epoch) plotter.create_plot_ej3_accuracy(accuracy, test_accuracy) return
def train(self, batch_size=128, initial_patience=200, iter_check_loss=10): with tf.Graph().as_default(): images, labels = Reader.build_batch( Trainer.training_set, file_length=Trainer.num_examples['train'], batch_size=batch_size, shuffled=True) logits = CNN.model(images) loss = CNN.loss(logits, labels) global_step = tf.Variable(0, name='global_step', trainable=False) learning_rate = tf.train.exponential_decay(0.1, global_step=global_step, decay_steps=10000, decay_rate=0.01) optimizer = tf.train.GradientDescentOptimizer(learning_rate) train_op = optimizer.minimize(loss, global_step=global_step) predictions = tf.argmax(logits, axis=1) training_accuracy, up_training_accuracy = tf.metrics.accuracy( labels=labels, predictions=predictions) tf.summary.scalar('training_accuracy', training_accuracy) tf.summary.scalar('loss', loss) tf.summary.scalar('learning_rate', learning_rate) summary = tf.summary.merge_all() with tf.Session() as sess: summary_writer = tf.summary.FileWriter(Trainer.log_train_dir, sess.graph) evaluator = Evaluator( os.path.join(Trainer.log_train_dir, 'eval/val')) init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) sess.run(init_op) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) saver = tf.train.Saver() if self.start_from_specific_checkpoint is not None: saver.restore(sess, self.start_from_specific_checkpoint) print('Model restored from file: %s' % self.start_from_specific_checkpoint) if self.start_from_last_checkpoint: checkpoint_path = tf.train.latest_checkpoint('logs/train') saver.restore(sess, checkpoint_path) print('Model restored from last checkpoint') with open('./Utils/start.txt') as f: print(f.read()) patience = initial_patience best_accuracy = 0.0 duration = 0.0 while True: start_time = time.time() _, loss_val, accuracy_train, summary_val, global_step_val = sess.run( [ train_op, loss, training_accuracy, summary, global_step ]) duration += time.time() - start_time if global_step_val % iter_check_loss == 0: examples_per_sec = batch_size * iter_check_loss / duration duration = 0.0 print('=> %s: step %d, loss = %f (%.1f examples/sec)' % (datetime.now(), global_step_val, loss_val, examples_per_sec)) summary_writer.add_summary(summary_val, global_step=global_step_val) print( '---------> Evaluating on validation dataset... <---------' ) path_to_latest_checkpoint_file = saver.save( sess, os.path.join(Trainer.log_train_dir, 'latest.ckpt')) accuracy = evaluator.evaluate( path_to_latest_checkpoint_file, Trainer.val_set, Trainer.num_examples['val'], global_step_val) print( '=---------> accuracy_validation = %f, best accuracy %f <---------' % (accuracy, best_accuracy)) if accuracy > best_accuracy: path_to_checkpoint_file = saver.save( sess, os.path.join(Trainer.log_train_dir, 'model.ckpt'), global_step=global_step_val) print('=> Model saved to file: %s' % path_to_checkpoint_file) patience = initial_patience best_accuracy = accuracy else: patience -= 1 print('=> patience = %d' % patience) if patience == 0: break coord.request_stop() coord.join(threads) with open('./Utils/done.txt') as f: print(f.read())
def crossValidation(self, operand): r = Reader('Ej2') train_data, test_data, max_, min_ = r.readFile( 0, self.k, self.linear, self.cross) # agarramos los datos de los txt blocks = np.split(np.array(train_data.copy()), self.k) plotter = Plotter() init_weights = np.random.rand(len(train_data[0]) - 1, 1) weights = init_weights.copy() error_min = 100000000 error_this_epoch = 1 w_min = init_weights.copy() error_per_epoch = [] eta_per_epoch = [] test_error_per_epoch = [] selectedBlock = 0 for selectedBlock in range(self.k): test_data = blocks[selectedBlock].copy() train_data = [] for i in range(self.k): if i != selectedBlock: train_data.extend(blocks[i].copy()) for epoch in range(self.epochs): np.random.shuffle(train_data) np.random.shuffle(test_data) if error_this_epoch > 0: total_error = 0 for i in range(len(train_data)): sumatoria = np.dot(train_data[i][:-1], weights) activation = self.getActivation(sumatoria) error = train_data[i][-1] - activation fixed_diff = self.eta * error derivated = self.sigmoid_derivated(sumatoria) const = np.dot(fixed_diff, derivated) for j in range(len(weights)): weights[j] += (const * train_data[i][j]) total_error += error**2 error_this_epoch = total_error / len(train_data) if epoch > 1: error_per_epoch.append(error_this_epoch) #if self.adaptive and epoch % 10 == 0: # self.adjust_learning_rate(error_per_epoch_linear) eta_per_epoch.append(self.eta) if epoch == 0: error_min = error_this_epoch if error_this_epoch < error_min: error_min = error_this_epoch w_min = weights if epoch > 1: test_error_per_epoch.append( self.test_perceptron(test_data, w_min, max_, min_, print_=False)) print('*************** RESULTS ***************') print('Analysis for training set:') print('Epochs: {}'.format(epoch + 1)) print('Adaptive: {}'.format(self.adaptive)) print('Cross: {}'.format(self.cross)) print('Initial alpha linear: {}'.format(self.initial_eta)) print('End alpha linear: {}'.format(self.eta)) print('***************************************') self.test_perceptron(test_data, w_min, max_, min_, print_=False) print('***************************************') plotter.create_plot_ej2(error_per_epoch, test_error_per_epoch, eta_per_epoch, linear=False) #plotter.create_plot_ej2(error_per_epoch_non_linear, test_error_per_epoch_non_linear, alpha_per_epoch_non_linear, linear=False) weights = init_weights.copy() error_min = 100000000 error_this_epoch = 1 w_min = init_weights.copy() error_per_epoch = [] eta_per_epoch = [] test_error_per_epoch = [] return
def algorithm(self, operand): r = Reader('Ej2') train_data, test_data, max_, min_ = r.readFile( 0, self.k, self.linear, self.cross) # agarramos los datos de los txt plotter = Plotter() init_weights = np.random.rand(len(train_data[0]) - 1, 1) weights = init_weights.copy() error_min = 1000000 error_this_epoch = 1 w_min = init_weights.copy() error_per_epoch = [] eta_per_epoch = [] test_error_per_epoch = [] for epoch in range(self.epochs): np.random.shuffle(train_data) if error_this_epoch > 0: total_error = 0 for i in range(len(train_data)): # exitacion = x(i x,:) * w sumatoria = np.dot(train_data[i][:-1], weights) # activacion = signo(exitacion); activation = self.getActivation(sumatoria) # expected_value - my_output_value error = train_data[i][-1] - activation # ∆w = η * (y(1,i x) - activacion) * x(i x,:)’; donde (y(1,i x) - activacion) = error_linear fixed_diff = self.eta * error derivated = self.sigmoid_derivated(sumatoria) const = np.dot(fixed_diff, derivated) # w = w + ∆w --> actualizo los pesos for j in range(len(weights)): weights[j] += (const * train_data[i][j]) total_error += self.denormalize(error, max_, min_)**2 error_this_epoch = total_error / len(train_data) error_per_epoch.append(error_this_epoch) #if self.adaptive and epoch % 10 == 0: # self.adjust_learning_rate(error_per_epoch_linear) eta_per_epoch.append(self.eta) if epoch == 0: error_min = error_this_epoch if error_this_epoch < error_min: error_min = error_this_epoch w_min = weights test_error_per_epoch.append( self.test_perceptron(test_data, w_min, max_, min_, print_=False)) print('*************** RESULTS ***************') print('Analysis for training set:') print('Epochs: {}'.format(epoch + 1)) print('Adaptive: {}'.format(self.adaptive)) print('Cross: {}'.format(self.cross)) print('Initial alpha linear: {}'.format(self.initial_eta)) print('End alpha linear: {}'.format(self.eta)) print('***************************************') self.test_perceptron(test_data, w_min, max_, min_, print_=False) print('***************************************') plotter.create_plot_ej2(error_per_epoch, test_error_per_epoch, eta_per_epoch, linear=False) #plotter.create_plot_ej2(error_per_epoch_non_linear, test_error_per_epoch_non_linear, alpha_per_epoch_non_linear, linear=False) return
def algorithm(self, operand): r = Reader('Ej2') data_linear, data_non_linear, test_data_linear, test_data_non_linear, max_, min_ = r.readFile( size=self.size) # agarramos los datos de los txt #test_data_linear, test_data_non_linear, max_out, min_out = r.readFile(test=True) initial_weights = np.random.rand(len(data_linear[0]) - 1, 1) weights_linear = initial_weights.copy() weights_non_linear = initial_weights.copy() error_min_linear = len(data_linear) * 2 error_min_non_linear = len(data_non_linear) * 2 error_this_epoch_linear = 1 error_this_epoch_non_linear = 1 w_min_linear = initial_weights.copy() w_min_non_linear = initial_weights.copy() error_per_epoch_linear = [] error_per_epoch_non_linear = [] alpha_per_epoch_linear = [] alpha_per_epoch_non_linear = [] plotter = Plotter() test_error_per_epoch_linear = [] test_error_per_epoch_non_linear = [] for epoch in range(self.iterations): # COTA del ppt if error_this_epoch_linear > 0 and error_min_non_linear > 0: total_error_linear = total_error_non_linear = 0 for i in range(len(data_linear)): sumatoria_linear = self.get_sum(data_linear[i][:-1], weights_linear) sumatoria_non_linear = self.get_sum( data_non_linear[i][:-1], weights_non_linear) activation_linear = self.get_activation(sumatoria_linear, linear=True) activation_non_linear = self.get_activation( sumatoria_non_linear, linear=False) error_linear = data_linear[i][-1] - activation_linear error_non_linear = data_non_linear[i][ -1] - activation_non_linear fixed_diff_linear = self.alpha_linear * error_linear fixed_diff_non_linear = self.alpha_non_linear * error_non_linear derivative_linear = 1.0 derivative_non_linear = self.sigmoid_derivative( sumatoria_non_linear) const_linear = fixed_diff_linear * derivative_linear const_non_linear = fixed_diff_non_linear * derivative_non_linear[ 0] for j in range(len(weights_linear)): weights_linear[j] = weights_linear[j] + ( const_linear * data_linear[i][j]) for j in range(len(weights_non_linear)): weights_non_linear[j] = weights_non_linear[j] + ( const_non_linear * data_non_linear[i][j]) total_error_linear += error_linear**2 total_error_non_linear += self.denormalize( error_non_linear, max_, min_)**2 error_this_epoch_linear = self.error_function( total_error_linear) / len(data_linear) error_per_epoch_linear.append(error_this_epoch_linear) error_this_epoch_non_linear = self.error_function( total_error_non_linear) / len(data_non_linear) error_per_epoch_non_linear.append(error_this_epoch_non_linear) if self.adaptive and epoch % 10 == 0: self.adjust_learning_rate(error_per_epoch_linear, linear=True) self.adjust_learning_rate(error_per_epoch_non_linear, linear=False) alpha_per_epoch_linear.append(self.alpha_linear) alpha_per_epoch_non_linear.append(self.alpha_non_linear) if epoch == 0: error_min_linear = error_this_epoch_linear if error_this_epoch_linear < error_min_linear: error_min_linear = error_this_epoch_linear w_min_linear = weights_linear if error_this_epoch_non_linear < error_min_non_linear: error_min_non_linear = error_this_epoch_non_linear w_min_non_linear = weights_non_linear test_error_per_epoch_linear.append( self.test_perceptron(test_data_linear, w_min_linear, linear=True, max_out=None, min_out=None, print_=False)) test_error_per_epoch_non_linear.append( self.test_perceptron(test_data_non_linear, w_min_non_linear, linear=False, max_out=max_, min_out=min_, print_=False)) else: break print('*************** RESULTS ***************') print('Analysis for training set:') print('Epochs: {}'.format(epoch + 1)) print('Initial alpha linear: {}'.format(self.initial_alpha_linear)) print('Initial alpha non linear: {}'.format( self.initial_alpha_non_linear)) print('End alpha linear: {}'.format(self.alpha_linear)) print('End alpha non linear: {}'.format(self.alpha_non_linear)) print('***************************************') self.test_perceptron(test_data_linear, w_min_linear, linear=True, max_out=None, min_out=None, print_=True) print('***************************************') self.test_perceptron(test_data_non_linear, w_min_non_linear, linear=False, max_out=max_, min_out=min_, print_=True) print('***************************************') plotter.create_plot_ej2(error_per_epoch_linear, test_error_per_epoch_linear, alpha_per_epoch_linear, linear=True) plotter.create_plot_ej2(error_per_epoch_non_linear, test_error_per_epoch_non_linear, alpha_per_epoch_non_linear, linear=False) return
def algorithm(self, problem): # bias x y out if problem == "XOR": train_data = [[1.0, 1.0, 1.0, -1.0], [1.0, -1.0, 1.0, 1.0], [1.0, 1.0, -1.0, 1.0], [1.0, -1.0, -1.0, -1.0]] test_data = [[1.0, 1.0, 1.0, -1.0], [1.0, -1.0, 1.0, 1.0], [1.0, 1.0, -1.0, 1.0], [1.0, -1.0, -1.0, -1.0]] if problem == "EVEN": r = Reader('Ej3') train_data, test_data = r.readFile(self.training_set_size, self.cross_validation, self.k) # M es el índice de la capa superior self.M = self.total_layers - 1 # cuantos nodos hay en la capa oculta (incluyendo el bias) self.nodes_per_layer = max(self.nodes_per_layer, len(train_data[0]) - 1) # nodos en la capa superior self.exit_nodes = 1 # inicializo el estado de activación de todas las unidades en la capa oculta --> [capa, nro de nodo] self.V = np.zeros((self.M + 1, self.nodes_per_layer)) # el estado de activación de la primera unidad de todas las capas ocultas deben tener el mismo valor --> 1 en este caso (BIAS) for i in range(1, self.M): self.V[i][0] = 1 # PASO 1: inicializar el conjunto de pesos en valores pequeños al azar self.W = np.random.rand( self.M + 1, self.nodes_per_layer, self.nodes_per_layer) - 0.5 # [capa destino, dest, origen] w = np.random.rand(self.nodes_per_layer, len(train_data[0]) - 1) - 0.5 # [dest, origen] self.W[1, :, :] = np.zeros( (self.nodes_per_layer, self.nodes_per_layer)) # inicializo en 0 los errores en las capas self.d = np.zeros((self.M + 1, self.nodes_per_layer)) # creo las conexiones de los nodos de entrada y la capa oculta for nodo_origen in range(len(train_data[0]) - 1): for nodo_destino in range(self.nodes_per_layer): # capa destino, nodo destino, nodo origen self.W[1, nodo_destino, nodo_origen] = w[nodo_destino, nodo_origen] error_min = 1000000 delta_accuracy_error = self.delta_accuracy_error total_error = 1 train_error_per_epoch = [] test_error_per_epoch = [] train_worst_error_per_epoch = [] test_worst_error_per_epoch = [] train_accuracy = [] test_accuracy = [] plotter = Plotter() for epoch in range(1, self.epochs): total_error = 0 train_worst_error_this_epoch = 0 corrects = 0 incorrects = 0 # mezclo el conjunto de entrenamiento al azar para seleccionar el primero np.random.shuffle(train_data) # tomo un ejemplo (u) for u in range(len(train_data)): # Paso 2 (V0 tiene los ejemplos iniciales) for k in range(len(train_data[0]) - 1): # nodos de entrada self.V[0][k] = train_data[u][k] # PASO 3: propagar la entrada hasta la capa de salida self.propagate() # en la última capa hay distinta cantidad de nodos for i in range(0, self.exit_nodes): hMi = self.h(self.M, i, self.nodes_per_layer, self.W, self.V) self.V[self.M][i] = self.g(hMi) if self.V[self.M][i] >= train_data[u][ -1] - delta_accuracy_error and self.V[self.M][ i] <= train_data[u][-1] + delta_accuracy_error: corrects += 1 else: incorrects += 1 # PASO 4: calcular el error para la capa de salida for i in range(0, self.exit_nodes): # n[-1] es el último item de la lista n --> data[u][-1] es la salida deseada de mi ejemplo tomado hMi = self.h(self.M, i, self.nodes_per_layer, self.W, self.V) self.d[self.M][i] = self.g_derivative(hMi) * ( train_data[u][-1] - self.V[self.M][i]) # PASO 5: retropropagar el error error_sum = self.back_propagate() # PASO 6: actualizar los pesos de las conexiones self.update_weights() # PASO 7: Calcular el error. Si error > COTA, ir al paso 2 for i in range(0, self.exit_nodes): if abs(train_data[u][-1] - self.V[self.M][i]) > train_worst_error_this_epoch: train_worst_error_this_epoch = abs(train_data[u][-1] - self.V[self.M][i]) total_error += abs(train_data[u][-1] - self.V[self.M][i]) train_error_per_epoch.append(total_error / len(train_data)) train_worst_error_per_epoch.append(train_worst_error_this_epoch) train_accuracy.append(corrects / (0.0 + corrects + incorrects)) if self.adaptive and epoch % 10 == 0: self.adjust_learning_rate(train_error_per_epoch) if total_error < error_min: error_min = total_error self.w_min = self.W if total_error / len( train_data ) <= self.error_tolerance or epoch == self.epochs - 1: self.test_perceptron(test_data, self.w_min, epoch, test_error_per_epoch, test_worst_error_per_epoch, test_accuracy, delta_accuracy_error, True, train_accuracy, train_error_per_epoch) break else: self.test_perceptron(test_data, self.w_min, epoch, test_error_per_epoch, test_worst_error_per_epoch, test_accuracy, delta_accuracy_error, False, train_accuracy, train_error_per_epoch) plotter.ej3_errors(train_error_per_epoch, test_error_per_epoch) plotter.ej3_accuracy(train_accuracy, test_accuracy) return