1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
| def train_bi_nn_model(dataset, H_list, device, learning_rate=1e-2, iteration=5000): x_train, y_train, x_val, y_val, x_test, y_test = unzip_dataset(dataset)
N = x_train.shape[0] D_in = x_train.shape[1] D_out = 1 loss_fn = torch.nn.BCELoss() res_table = np.zeros((len(H_list),8)) res_table_ind = 0 for H in H_list: model = torch.nn.Sequential( torch.nn.Linear(D_in, H), torch.nn.ReLU(), torch.nn.Linear(H, D_out), torch.nn.Sigmoid() ).to(device) loss_history = [] accuracy_val = [] correct = 0.0 best_accuracy_val = 0.0 best_iteration = 0.0 best_model = None optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate) t0 = time.time() for t in range(iteration): y_train_pred = model(x_train).squeeze(dim=-1) loss = loss_fn(y_train_pred, y_train) loss_history.append(loss.item()) y_val_pred = model(x_val).squeeze(dim=-1) y_val_pred[y_val_pred >= 0.5] = 1.0 y_val_pred[y_val_pred < 0.5] = 0.0 correct = (y_val_pred == y_val).sum().item() accuracy = correct / y_val.shape[0] accuracy_val.append(accuracy) if accuracy > best_accuracy_val: best_accuracy_val = accuracy best_iteration = t best_model = model if t % 1000 == 0: print("iteration: %s/%s" % (t, iteration)) optimizer.zero_grad() loss.backward() optimizer.step() t1 = time.time() print("training time = %s(s)" % (t1 - t0)) y_test_pred = best_model(x_test).squeeze(dim=-1)
yt = y_test.cpu().detach().numpy() yp = y_test_pred.cpu().detach().numpy() auc = metrics.roc_auc_score(yt, yp) yp[yp >= 0.5] = 1.0 yp[yp < 0.5] = 0.0 accuracy_test, precision, recall, f1 = confusion_mat_evaluate(yt, yp) res_table[res_table_ind] += np.array([H, best_accuracy_val, accuracy_test, auc, precision, recall, f1, t1-t0]) res_table_ind += 1 print("The best model: iteration = %s" % (best_iteration)) print("On validation dataset: accuracy = %s" % (best_accuracy_val)) print("On testing dataset: accuracy = %s, auc = %s, precision = %s, recall = %s, f1 = %s" \ % (accuracy_test, auc, precision, recall, f1))
fig = plt.figure(figsize=(15,4)) ax1 = plt.subplot(1,2,1) ax2 = plt.subplot(1,2,2) plt.sca(ax1) plt.title("H = %s" % H) plt.xlabel("iteration") plt.ylabel("loss") plt.plot(range(iteration), loss_history) plt.sca(ax2) plt.title("H = %s" % H) plt.xlabel("iteration") plt.ylabel("accuracy_val") plt.plot(range(iteration), accuracy_val) plt.show()
return res_table
|