def plot_evolvement():
    import sklearn.datasets
    import torch
    import sys
    sys.path.append('../')
    from models import csnn_learnable_r

    epochs = [0, 5, 10, 50, 100, 500]
    outputDir = '../results/evolvement/'

    # load models
    models = []
    for epoch in epochs:
        model = csnn_learnable_r(2, 64, bias=False)
        PATH = outputDir + 'csnn_epoch{}.pth'.format(epoch)
        model.load_state_dict(torch.load(PATH))
        models.append(model)
    print('load models successfully')

    # load confidences
    confidences = []
    x_lin = None
    y_lin = None
    for epoch in epochs:
        dir = outputDir + 'moons_confidence_alpha1_epoch{}.npz'.format(epoch)
        f = np.load(dir)
        confidences.append(f['c'])
        if x_lin is None:
            x_lin = f['a']
            y_lin = f['b']
    print('load confidences successfully')

    # Moons
    noise = 0.1
    # sklearn has no random seed, it depends on numpy to get random numbers
    np.random.seed(0)
    x_train, y_train = sklearn.datasets.make_moons(n_samples=1500, noise=noise)
    x_train0 = x_train
    mask = y_train.astype(np.bool)
    mean = np.mean(x_train, axis=0)
    std = np.std(x_train, axis=0)
    print('mean, std', mean, std)
    x_train = (x_train - mean) / std / np.sqrt(2)

    # support circles
    fig, axs = plt.subplots(2, 3, figsize=(10, 6.67), dpi=300)
    alpha = 1
    for i in range(len(epochs)):
        row = i // 3
        col = i % 3
        w = models[i].fc1.weight.data.numpy()
        r = models[i].r.detach().numpy()
        center = w / alpha
        radius2 = r * r + np.sum(w * w, axis=1) * (1 / alpha / alpha - 1)
        radius = np.sqrt(radius2)
        # cycle = plt.rcParams['axes.prop_cycle'].by_key()['color']
        N = w.shape[0]
        color = 1. / N * np.arange(0, N)
        for j in range(w.shape[0]):
            circle = plt.Circle(center[j],
                                radius[j],
                                color=cm.jet(color[j]),
                                fill=False)
            axs[row, col].add_artist(circle)
            # axs[0].axis('equal')

        axs[row, col].scatter(x_train[mask, 0], x_train[mask, 1], s=1, c='r')
        axs[row, col].scatter(x_train[~mask, 0], x_train[~mask, 1], s=1)
        axs[row, col].set(xlim=(-2.4, 2.4), ylim=(-2.4, 2.4))
        axs[row, col].set_aspect('equal')
        if row == 0: axs[row, col].set_xticklabels([])
        if col != 0: axs[row, col].set_yticklabels([])
        axs[row, col].set_title('epoch={}'.format(epochs[i]))
        plt.savefig(outputDir + 'evolvement_circles.png', dpi=300)

    # confidence
    fig, axs = plt.subplots(2, 3, figsize=(10, 5), dpi=300)
    for i in range(len(epochs)):
        row = i // 3
        col = i % 3
        level = np.linspace(0.5, 1., 21)
        contour = axs[row, col].contourf(x_lin,
                                         y_lin,
                                         confidences[i],
                                         cmap=plt.get_cmap('inferno'),
                                         levels=level)  # , extend='both')
        fig.colorbar(contour, ax=axs[row, col], format='%.2f')
        axs[row, col].scatter(x_train[mask, 0], x_train[mask, 1], s=1, c='r')
        axs[row, col].scatter(x_train[~mask, 0], x_train[~mask, 1], s=1)
        axs[row, col].set(xlim=(-2.4, 2.4), ylim=(-2.4, 2.4))
        axs[row, col].set_aspect('equal')
        if row == 0: axs[row, col].set_xticklabels([])
        if col != 0: axs[row, col].set_yticklabels([])
        axs[row, col].set_title('epoch={}'.format(epochs[i]))
        plt.savefig(outputDir + 'evolvement_conf.png', dpi=300)
losses = []
accs_validate = []
losses_validate = []

# pre_train
for run in range(runs):
    np.random.seed(seeds[run])
    torch.manual_seed(seeds[run])
    dl_train = torch.utils.data.DataLoader(ds_train,
                                           batch_size=batchSize,
                                           shuffle=True,
                                           drop_last=False)
    dl_test = torch.utils.data.DataLoader(ds_test,
                                          batch_size=x_validate.shape[0],
                                          shuffle=False)
    model = csnn_learnable_r(2, features, bias=BIAS)
    if learnable_r:
        model.set_lambda(LAMBDA)
        model.set_miu(MIU)
    optimizer = torch.optim.Adam(model.parameters(),
                                 lr=learningRate,
                                 weight_decay=l2Penalty)
    accuracy, loss, accuracy_validate, loss_validate = pre_train(model,
                                                                 optimizer,
                                                                 dl_train,
                                                                 dl_test,
                                                                 x_train,
                                                                 y_train,
                                                                 x_validate,
                                                                 y_validate,
                                                                 run,
def plot_radius_penalty_effect():
    import sklearn.datasets
    import torch
    import sys
    sys.path.append('../')
    from models import csnn_learnable_r

    # lambdas = [0.00, 0.01, 0.02, 0.04, 0.16, 0.64]
    lambdas = [0.00, 0.02, 0.04, 0.64]
    outputDir = '../results/radius_penalty_impact/'

    # load models
    models = []
    for l in lambdas:
        model = csnn_learnable_r(2, 64, bias=False)
        PATH = outputDir + 'lambda_{:.2f}/csnn_run0_epoch500.pth'.format(l)
        model.load_state_dict(torch.load(PATH))
        models.append(model)
    print('load models successfully')

    # load rs
    rs = []
    for l in lambdas:
        f = np.load(outputDir +
                    'lambda_{:.2f}/mean_std_accs_aucs_net4.npz'.format(l))
        rs.append(f['l'])
    print('load radius successfully')

    # load confidences
    confidences = []
    x_lin = None
    y_lin = None
    for l in lambdas:
        f = np.load(outputDir +
                    'lambda_{:.2f}/moons_confidence_alpha1.npz'.format(l))
        confidences.append(f['c'])
        if x_lin is None:
            x_lin = f['a']
            y_lin = f['b']
    print('load confidences successfully')

    # Moons
    noise = 0.1
    # sklearn has no random seed, it depends on numpy to get random numbers
    np.random.seed(0)
    x_train, y_train = sklearn.datasets.make_moons(n_samples=1500, noise=noise)
    x_train0 = x_train
    mask = y_train.astype(np.bool)
    mean = np.mean(x_train, axis=0)
    std = np.std(x_train, axis=0)
    print('mean, std', mean, std)
    x_train = (x_train - mean) / std / np.sqrt(2)

    fig, axs = plt.subplots(len(lambdas), 3, figsize=(10, 10.6), dpi=300)
    alpha = 1
    for i in range(len(lambdas)):
        # support circles
        w = models[i].fc1.weight.data.numpy()
        r = models[i].r.detach().numpy()
        center = w / alpha
        radius2 = r * r + np.sum(w * w, axis=1) * (1 / alpha / alpha - 1)
        radius = np.sqrt(radius2)
        N = w.shape[0]
        col = 1. / N * np.arange(0, N)
        for j in range(w.shape[0]):
            circle = plt.Circle(center[j],
                                radius[j],
                                color=cm.jet(col[j]),
                                fill=False)
            axs[i, 0].add_artist(circle)

        axs[i, 0].scatter(x_train[mask, 0], x_train[mask, 1], s=1, c='r')
        axs[i, 0].scatter(x_train[~mask, 0], x_train[~mask, 1], s=1)
        axs[i, 0].set(xlim=(-2.4, 2.4), ylim=(-2.4, 2.4))
        axs[i, 0].set_aspect('equal')
        if i != len(lambdas) - 1: axs[i, 0].set_xticklabels([])

        # confidence
        level = np.linspace(0.5, 1., 21)
        contour = axs[i, 1].contourf(x_lin,
                                     y_lin,
                                     confidences[i],
                                     cmap=plt.get_cmap('inferno'),
                                     levels=level)  # , extend='both')
        fig.colorbar(contour, ax=axs[i, 1], format='%.2f')
        axs[i, 1].scatter(x_train[mask, 0], x_train[mask, 1], s=1, c='r')
        axs[i, 1].scatter(x_train[~mask, 0], x_train[~mask, 1], s=1)
        axs[i, 1].set(xlim=(-2.4, 2.4), ylim=(-2.4, 2.4))
        axs[i, 1].set_aspect('equal')
        axs[i, 1].set_yticklabels([])
        if i != len(lambdas) - 1: axs[i, 1].set_xticklabels([])

        # radius
        epochs = np.arange(101) * 5
        axs[i, 2].plot(epochs, rs[i][:, 0], lw=2, label='$||r||_\infty$')
        axs[i, 2].plot(epochs,
                       rs[i][:, 1] / 8.,
                       lw=2,
                       label='$||r||_2/\sqrt{n_{hidden}}$')
        axs[i, 2].fill_between(epochs,
                               rs[i][:, 1] / 8. - rs[i][:, 2],
                               rs[i][:, 1] / 8. + rs[i][:, 2],
                               facecolor='orange',
                               alpha=0.2)
        axs[i, 2].axis([0, 500, 0., 1.8])
        axs[i, 2].set_ylabel('r norm')
        if i == len(lambdas) - 1: axs[i, 2].set_xlabel('epochs')
        axs[i, 2].legend()
        axs[i, 2].yaxis.tick_right()
        axs[i, 2].yaxis.set_label_position("right")
        if i != len(lambdas) - 1: axs[i, 2].set_xticklabels([])

    plt.savefig(outputDir + 'evolvement.png', dpi=300)
Exemplo n.º 4
0
outputDir = '/home/hh/data/ngsim/combined_dataset/'
seed = 0

np.random.seed(seed)
torch.manual_seed(seed)
for LAMBDA in LAMBDAS:
    print('lambda {:.2f}'.format(LAMBDA))
    for run in range(runs):
        dl_train = torch.utils.data.DataLoader(ds_train,
                                               batch_size=batchSize,
                                               shuffle=True,
                                               drop_last=False)
        dl_test = torch.utils.data.DataLoader(ds_test,
                                              batch_size=x_validate.shape[0],
                                              shuffle=False)
        model = csnn_learnable_r(inputs, hiddenUnits, bias=BIAS)
        # model = csnn_learnable_r_3layers(inputs, hiddenUnits, bias=BIAS)
        # model = Net2(inputs, hiddenUnits, bias=BIAS)
        if learnable_r:
            model.set_lambda(LAMBDA)
            model.set_miu(MIU)
        optimizer = torch.optim.Adam(model.parameters(),
                                     lr=learningRate,
                                     weight_decay=l2Penalty)
        pre_train(model,
                  optimizer,
                  dl_train,
                  dl_test,
                  x_train,
                  y_train,
                  x_validate,