for x, y in enumerate(base): oDataSet.add_sample_of_attribute( np.array(list(np.float32(y)) + [classes[x]])) oDataSet.attributes = oDataSet.attributes.astype(float) oDataSet.normalize_data_set() for j in range(20): print(j) oData = Data(len(oDataSet.labelsNames), 31, samples=50) oData.random_training_test_by_percent( np.unique(classes, return_counts=True)[1], 0.8) perc = Layered_perceptron_Logistic(learning_rate, len(oDataSet.labelsNames)) perc.train(oDataSet.attributes[oData.Training_indexes], oDataSet.labels[oData.Training_indexes], epochs) oData.model = perc oData.confusion_matrix = np.zeros( (len(oDataSet.labelsNames), len(oDataSet.labelsNames))) for i in oData.Testing_indexes: data = np.matrix(np.hstack(([-1], oDataSet.attributes[i]))).T predicted = perc.predict(data) oData.confusion_matrix[int(oDataSet.labels[i]), predicted] += 1 print(oData) oDataSet.append(oData) oExp.add_data_set( oDataSet, description=" Experimento Dermatologia LP 20 realizaçoes.".format()) oExp.save("Objects/EXP01_4_LP_20.gzip".format()) oExp = Experiment.load("Objects/EXP01_4_LP_20.gzip".format()) print(oExp)
model.fit(oDataSet.attributes[oData.Training_indexes], binarizer(oDataSet.labels[oData.Training_indexes]), batch_size=50, epochs=epochs, verbose=1) y_pred = model.predict( oDataSet.attributes[oData.Testing_indexes]).argmax(axis=1) y_true = oDataSet.labels[oData.Testing_indexes] print(accuracy_score(y_true, y_pred)) print(confusion_matrix(y_true, y_pred)) oData.confusion_matrix = confusion_matrix(y_true, y_pred) model.save('model.h5') myArr = None with open("model.h5", "rb") as binaryfile: myArr = bytearray(binaryfile.read()) oData.model = myArr, model.history.history['loss'] oData.params = { "k_fold": K_FOLD, "GRID_RESULT": grid_result, "GRID_VALUES_NEURON": GRID_NEURON, "GRID_VALUES_BETA": GRID_B, "LEARNING RATE": LEARNING_RATE, "EPOCHS": epochs } oDataSet.append(oData) print(oData) oExp.add_data_set( oDataSet, description=" Experimento cancer LP 20 realizaçoes.".format()) oExp.save("Objects/EXP01_5_LP_20.gzip".format())
oDataSet.labels[oData.Training_indexes[train]], epochs) y_pred = [] y_true = [] for i in test: y_pred.append(mpl.predict(oDataSet.attributes[oData.Training_indexes[i]])[0, 0]) y_true.append(oDataSet.labels[oData.Training_indexes[i]]) grid_result[g1, k_slice] = mean_squared_error(y_true, y_pred) k_slice += 1 print(grid_result) best_p = GRID[np.argmin(np.mean(grid_result, axis=1))] mpl = multi_Layered_perceptron_linear(LEARNING_RATE, (oDataSet.attributes.shape[1], best_p, 1)) mpl.train_regression(oDataSet.attributes[oData.Training_indexes], oDataSet.labels[oData.Training_indexes], epochs) y_pred = [] y_true = [] for i in oData.Testing_indexes: y_pred.append(mpl.predict(oDataSet.attributes[i])[0, 0]) y_true.append(oDataSet.labels[i]) plt.scatter(oDataSet.attributes[i], y_pred[-1], color='red') plt.scatter(oDataSet.attributes[i], y_true[-1], color='green') plt.show() oData.model = mpl oData.params = {"k_fold": K_FOLD, "GRID_RESULT": grid_result, "GRID_VALUES": GRID, "LEARNING RATE": LEARNING_RATE, "EPOCHS": epochs, "MSE": mean_squared_error(y_true, y_pred), "RMSE": np.sqrt(mean_squared_error(y_true, y_pred))} oDataSet.append(oData) oExp.add_data_set(oDataSet, description=" Experimento Artificial MLP 20 realizaçoes.".format()) oExp.save("Objects/EXP02_1_LP_20.gzip".format())
experiment.log_metric("test_accuracy", accuracy_score(y_true, y_pred)) experiment.log_metric("beta", best_b) experiment.log_metric("neurons", best_p) experiment.log_confusion_matrix(matrix=confusion_matrix(y_true, y_pred).tolist(), labels=oDataSet.labelsNames) # model.save('model.h5') # experiment.log_asset("model.h5") model.save_weights('model.weights') experiment.log_asset("model.weights") print(accuracy_score(y_true, y_pred)) print(confusion_matrix(y_true, y_pred)) oData.confusion_matrix = confusion_matrix(y_true, y_pred) oData.model = model oData.params = { "k_fold": K_FOLD, "GRID_RESULT": grid_result, "GRID_VALUES_NEURON": GRID_NEURON, "GRID_VALUES_BETA": GRID_B, "LEARNING RATE": LEARNING_RATE, "EPOCHS": epochs } experiment.log_other("params", oData.params) y_pred = model.predict( oDataSet.attributes[oData.Training_indexes]).argmax(axis=1) y_true = oDataSet.labels[oData.Training_indexes] experiment.log_metric("train_accuracy", accuracy_score(y_true, y_pred)) experiment.end() oDataSet.append(oData)