Exemple #1
0
    alphas = []
    mmcs = []
    nzs = []
    aucs = []

    bestValidationAcc = 0.
    for epoch in range(epochs):
        # alpha = min(1, max(0, (epoch**0.1-1.5)/0.6))
        # alpha = epoch/epochs
        alpha = maxAlpha * epoch / epochs
        # r2 = min(0.01, 0.01 - epoch * 0.06 / 5000)
        # r2 = 1.
        for i, batch in enumerate(dl_train):
            loss, x, y, y_pred, z = step(model, optimizer, batch, alpha, r2)

        accuracy, loss = eval_step(model, x_train, y_train, alpha, r2)
        #if epoch % 10 == 0:
        #    print('train: epoch {}, accuracy {:.3f}, loss {:.3f}'.format(epoch, accuracy, loss))
        testacc, testloss = eval_step(model, x_validate, y_validate, alpha, r2)
        if testacc > bestValidationAcc:
            # include both learnable param and registered buffer
            # PATH = outputDir+'/csnn_2_csnn_layers_run{}_r2{:.1f}_maxAlpha{:.1f}_affine_false.pth'.format(run, r2, maxAlpha)
            # torch.save({'net':model, 'alpha':alpha, 'r2':r2}, PATH)
            bestValidationAcc = testacc

        if epoch % 5 == 0:
            #print('validation: epoch {}, fc1 shape {}, loss {:.3f}'.format(epoch, model.fc1.weight.data.shape[0], loss))
            losses.append(loss)
            losses_validate.append(testloss)
            accuracies.append(accuracy)
            accuracies_validate.append(testacc)
    bestValidationAcc = 0.
    np.set_printoptions(precision=4)
    for epoch in range(epochs):
        # alpha = maxAlpha * epoch/epochs
        alpha = maxAlpha
        for i, batch in enumerate(dl_train):
            if learnable_r:
                loss_ce, loss_penalty, loss_l2, x, y, y_pred, z = step(
                    model, optimizer, batch, alpha, r2, learnable_r=True)
            else:
                loss, x, y, y_pred, z = step(model, optimizer, batch, alpha,
                                             r2)

        if learnable_r:
            accuracy, loss_ce, loss_penalty, loss_l2 = eval_step(
                model, x_train, y_train, alpha, r2, learnable_r=True)
            testacc, testloss_ce, testloss_penalty, testloss_l2 = eval_step(
                model, x_validate, y_validate, alpha, r2, learnable_r=True)
        else:
            accuracy, loss = eval_step(model, x_train, y_train, alpha, r2)
            testacc, testloss = eval_step(model, x_validate, y_validate, alpha,
                                          r2)
        if testacc > bestValidationAcc:
            bestValidationAcc = testacc

        if epoch % 5 == 0:
            if learnable_r:
                losses.append(loss_ce + loss_penalty)
                losses_validate.append(testloss_ce + testloss_penalty)
            else:
                losses.append(loss)
Exemple #3
0
        nzs = []
        aucs = []

        bestValidationAcc = 0.
        for epoch in range(epochs):
            # alpha = min(1, max(0, (epoch**0.1-1.5)/0.6))
            # alpha = epoch/epochs
            alpha = maxAlpha * epoch / epochs
            # alpha = 0.5
            # r2 = min(0.01, 0.01 - epoch * 0.06 / 5000)
            # r2 = 1.
            for i, batch in enumerate(dl_train):
                # loss_ce, loss_r, loss_w, x, y, y_pred, z = step(model, optimizer, batch, alpha, r2, learnable_r)
                step(model, optimizer, batch, alpha, r2, learnable_r)
            if learnable_r:
                accuracy, loss_ce, loss_r, loss_w = eval_step(
                    model, x_train, y_train, alpha, r2, learnable_r)
                testacc, testloss_ce, testloss_r, testloss_w = eval_step(
                    model, x_validate, y_validate, alpha, r2, learnable_r)
            else:
                accuracy, loss = eval_step(model, x_train, y_train, alpha, r2,
                                           learnable_r)
                testacc, testloss = eval_step(model, x_validate, y_validate,
                                              alpha, r2, learnable_r)
            #if epoch % 10 == 0:
            #    print('train: epoch {}, accuracy {:.3f}, loss {:.3f}'.format(epoch, accuracy, loss))
            if testacc > bestValidationAcc:
                # include both learnable param and registered buffer
                # PATH = outputDir+'/csnn_2_csnn_layers_run{}_r2{:.1f}_maxAlpha{:.1f}_affine_false.pth'.format(run, r2, maxAlpha)
                # torch.save({'net':model, 'alpha':alpha, 'r2':r2}, PATH)
                bestValidationAcc = testacc