for m in metrics: accumulated_metrics[m] = [] name = f"MLP{i:05}_d-{dataset_idx}_b-{batch_size}_h-{n_hidden_nodes}_eta-{eta}".replace(".", ",") i += 1 print(name) best = {"best_acc": 0, "best_loss": np.inf} for _ in range(loops): print(".", end="") net = MLP(train[0], train[1], n_hidden_nodes, momentum=0) train_losses, valid_losses, train_accuracies, valid_accuracies, pocket_epoch = net.train( train[0], train[1], valid[0], valid[1], eta, epochs, early_stop_count=early_stop, shuffle=shuffle, batch_size=batch_size ) cm_train = net.confmat(train[0], train[1]) cm_valid = net.confmat(valid[0], valid[1]) print("cm_train", cm_train) print("cm_valid", cm_valid) print("pocket epoch", pocket_epoch) train_loss, valid_loss = train_losses[pocket_epoch], valid_losses[pocket_epoch] train_acc, train_sens, train_spec, _, _ = two_class_conf_mat_metrics( net.confmat(train[0], train[1])) valid_acc, valid_sens, valid_spec, _, _ = two_class_conf_mat_metrics( net.confmat(valid[0], valid[1])) if debug_best_and_plot: if valid_acc > best["best_acc"] or ( valid_acc == best["best_acc"] and valid_loss < best["best_loss"]): print("BEST!")
import numpy as np from model.mlp import MLP if __name__ == '__main__': np.random.seed(72) anddata = np.array([[0, 0, 0], [0, 1, 0], [1, 0, 0], [1, 1, 1]]) xordata = np.array([[0, 0, 0], [0, 1, 1], [1, 0, 1], [1, 1, 0]]) p = MLP(anddata[:, 0:2], anddata[:, 2:3], 2) p.train_for_niterations(anddata[:, 0:2], anddata[:, 2:3], 0.25, 1001) p.confmat(anddata[:, 0:2], anddata[:, 2:3]) q = MLP(xordata[:, 0:2], xordata[:, 2:3], 2) q.train_for_niterations(xordata[:, 0:2], xordata[:, 2:3], 0.25, 5001) q.confmat(xordata[:, 0:2], xordata[:, 2:3])
valid = standardize_dataset(valid, mean, std) test = standardize_dataset(test, mean, std) # for i in [1, 2, 5, 10, 20, 100]: for i in [20]: print(f"x-----> {i} hidden nodes <-----x") net = MLP(train[0], number_to_one_hoot(train[1]), i, outtype="softmax") # net.train(train[0], number_to_one_hoot(train[1]), 0.1, 1000) net.earlystopping_primitive(train[0], number_to_one_hoot(train[1]), valid[0], number_to_one_hoot(valid[1]), eta=0.1, niteration=100, early_stop_count=2) net.confmat(train[0], number_to_one_hoot(train[1])) net.confmat(test[0], number_to_one_hoot(test[1])) print() print() """ Confusion matrix: [[4827 0 15 6 13 16 23 5 15 21] [ 1 5541 22 12 7 8 9 12 37 8] [ 10 25 4700 60 15 5 13 38 24 18] [ 8 24 41 4780 2 81 2 18 51 31] [ 6 6 40 2 4659 25 13 34 12 71] [ 23 19 18 90 4 4260 46 7 40 27] [ 19 3 26 10 46 31 4826 5 19 6] [ 5 13 39 44 8 8 2 4965 8 92] [ 30 33 51 68 10 52 17 11 4614 42] [ 3 14 16 29 95 20 0 80 22 4672]]
print(np.abs(inputs).max(0)) # Targets to one hoot targets = number_to_one_hoot(targets, 3) train = (inputs[::2], targets[::2]) valid = (inputs[1::4], targets[1::4]) test = (inputs[3::4], targets[3::4]) accs = [] for i in range(10): net = MLP(train[0], train[1], 10) net.earlystopping_primitive(train[0], train[1], valid[0], valid[1], 0.1, 10, 2) # net.train(train[0], train[1], 0.1, 100000) print("Train") net.confmat(train[0], train[1]) print("Valid") net.confmat(valid[0], valid[1]) print("Test") acc = conf_mat_acc(net.confmat(test[0], test[1])) accs.append(acc) print(f"Mean accuracy: {np.mean(np.array(accs)) * 100:2.4f}") """ 5. Number of Instances: 150 (50 in each of three classes) 6. Number of Attributes: 4 numeric, predictive attributes and the class 7. Attribute Information: 1. sepal length in cm 2. sepal width in cm 3. petal length in cm