def main(base): stream = open('configurations/runConfigurations.yml', 'r', encoding='utf-8').read() settings = yaml.load(stream=stream, Loader=yaml.FullLoader) dataset, n_features = datasets.load_multiclass_base(base) epochs = settings['epochs'] initial_learning_rate = settings['initial_learning_rate'] final_learning_rate = settings['final_learning_rate'] realizations = settings['realizations'] total_execution_init = time.time() #n_neurons = model_utils.cross_validation(5, [5, 7, 9, 11, 13], deepcopy(dataset), n_features, n_labels, base) n_neurons = 10 execution_log = open('execution_logs/{}_{}_neurons.txt'.format(base, n_neurons), 'w') scenarios = [] rmses = [] execution_log.write('Base escolhida: {}\n'.format(base)) print('Número de neurônios escohidos: {}.'.format(n_neurons)) for i in range(realizations): training_base, test_base = train_test_split(dataset, test_size=settings['test_size']) start = time.time() elm_test = ELM(n_layers=2, n_neurons=[n_neurons, 1], n_features=n_features, output_layer_func='Linear') elm_test.training(training_base) end = time.time() total_time = (end - start) predicted_labels, activations = elm_test.predict(test_base, classify=False) rmse = model_utils.rmse(predicted_labels, test_base[:, n_features+1:]) scenarios.append({'rmse': rmse, 'ol_net': deepcopy(elm_test), 'training_base': training_base, 'test_base': test_base, 'predicted_labels': predicted_labels}) rmses.append(rmse) print('Realização: {} | Tempo de treinamento:{} | RMSE: {}'.format(i + 1, round(total_time, 2), rmse)) print('----------------------------------------------------------------------') execution_log.write('Realização: {} | Tempo de treinamento:{} | RMSE: {}\n'.format(i + 1, round(total_time, 2), rmse)) execution_log.write('----------------------------------------------------------------------\n') execution_log.write('RMSE médio: {}\nDesvio Padrão:{}\n'.format(round(float(np.mean(rmses)), 2), float(np.std(rmses)))) best_realization = log_utils.choose_best_realization_regression(scenarios, settings['criterion_choiced']) total_execution_final = time.time() print('Tempo de execução total:{} segundos'.format(round(total_execution_final-total_execution_init))) execution_log.write('Tempo de execução total:{} segundos'.format(round(total_execution_final-total_execution_init))) execution_log.close() plot_utils.plot_regression(predict=best_realization['predicted_labels'], test_base=best_realization['test_base'], model='ELM')
def main(): stream = open('configurations/runConfigurations.yml', 'r', encoding='utf-8').read() settings = yaml.load(stream=stream, Loader=yaml.FullLoader) neuron_type = settings['neuron_type'] dataset, n_features = datasets.load_multiclass_base(settings['chosen_base']) n_labels = len(list(set(dataset[:, -1]))) epochs = settings['epochs'] learning_rate = settings['learning_rate'] realizations = settings['realizations'] scenarios = [] total_time = 0 for i in range(realizations): start = time.time() training_base, test_base = train_test_split(dataset, test_size=settings['test_size'],stratify=dataset[:, -1]) training_base = dataset_utils.binarize_labels(training_base, neuron_type) test_base = dataset_utils.binarize_labels(test_base, neuron_type) one_layer_net = olp.GenericOLN(n_labels=n_labels, n_weights=n_features + 1, neuron_type=neuron_type) one_layer_net.new_training(epochs=epochs, learning_rate=learning_rate, training_base=training_base) hit_rate, predict = one_layer_net.hit_rate(test_base) end = time.time() total_time += (end - start) print('Realization {}, Hit rate:{}%.'.format(i+1, round(hit_rate, 1))) scenarios.append({'hit_rate': hit_rate, 'ol_net': deepcopy(one_layer_net), 'training_base': training_base, 'test_base': test_base}) del one_layer_net print('\n--------Statistics---------') print('Mean execution time: {}'.format(total_time / realizations)) best_realization = log_utils.choose_realization(scenarios, settings['criterion_choiced']) plt_ut.plot_epochs_error(realization=best_realization, chosen_base=settings['chosen_base'], neuron_type=neuron_type) plt_ut.plot_decision_region_mult(realization=best_realization, n_classes=n_labels, choosen_base=settings['chosen_base'], neuron_type=neuron_type)
def main(chosen_base): stream = open('configurations/runConfigurations.yml', 'r', encoding='utf-8').read() settings = yaml.load(stream=stream, Loader=yaml.FullLoader) dataset, n_features = datasets.load_multiclass_base(chosen_base) n_labels = len(list(set(dataset[:, -1]))) n_output_neurons = 1 if n_labels == 2 else n_labels epochs = settings['epochs'] initial_learning_rate = settings['initial_learning_rate'] final_learning_rate = settings['final_learning_rate'] realizations = settings['realizations'] total_execution_init = time.time() n_neurons = model_utils.cross_validation(5, [5, 7, 9, 11, 13], deepcopy(dataset), n_features, n_labels, chosen_base) execution_log = open( 'execution_logs/MLP_{}_{}_neurons.txt'.format(chosen_base, n_neurons), 'w') scenarios = [] hit_rates = [] execution_log.write('Base escolhida: {}\n'.format(chosen_base)) print('Número de neurônios escohidos: {}.'.format(n_neurons)) for i in range(realizations): training_base, test_base = train_test_split( dataset, test_size=settings['test_size'], stratify=dataset[:, -1]) if n_labels > 2: training_base = dataset_utils.binarize_labels(training_base) test_base = dataset_utils.binarize_labels(test_base) start = time.time() mlp_test = MLP(n_layers=2, n_neurons=[n_neurons, n_output_neurons], n_features=n_features) mlp_test.training(training_base, epochs, initial_learning_rate=initial_learning_rate, final_learning_rate=final_learning_rate) end = time.time() total_time = (end - start) predicted_labels, activations = mlp_test.predict(test_base) hit_rate = model_utils.hit_rate(predicted_labels, test_base[:, n_features + 1:], activations) scenarios.append({ 'hit_rate': hit_rate, 'ol_net': deepcopy(mlp_test), 'training_base': training_base, 'test_base': test_base, 'predict': predicted_labels }) hit_rates.append(hit_rate) print('Realização: {} | Tempo de treinamento:{} | Taxa de acerto: {}%'. format(i + 1, round(total_time, 2), hit_rate)) print( '----------------------------------------------------------------------' ) execution_log.write( 'Realização: {} | Tempo de treinamento:{} | Taxa de acerto: {}%\n'. format(i + 1, round(total_time, 2), hit_rate)) execution_log.write( '----------------------------------------------------------------------\n' ) execution_log.write('Acurácia: {}%\nDesvio Padrão:{}\n'.format( round(float(np.mean(hit_rates)), 2), round(float(np.std(hit_rates)), 2))) best_realization = log_utils.choose_realization( scenarios, settings['criterion_choiced']) total_execution_final = time.time() plot_utils.plot_conf_matrix( predict=best_realization['predict'], desired_label=best_realization['test_base'][:, n_features + 1:], chosen_base=chosen_base, n_labels=n_labels, model='MLP') print('Tempo de execução total:{} segundos'.format( round(total_execution_final - total_execution_init))) execution_log.write('Tempo de execução total:{} segundos'.format( round(total_execution_final - total_execution_init))) execution_log.close()