def calculate_mode_connectivity(w1, w2, eval_loader, config):
    net = load_model('{}/{}.pth'.format(config['exp_dir'], 'init')).to(DEVICE)
    loss_history, acc_history, ts = [], [], []
    for t in np.arange(0.0, 1.01, 0.025):
        ts.append(t)
        net = assign_weights(net, w1 + t*(w2-w1)).to(DEVICE)
        metrics = eval_single_epoch(net, eval_loader)
        loss_history.append(metrics['loss'])
        acc_history.append(metrics['accuracy'])
    return loss_history, acc_history, ts
Exemple #2
0
def plot_loss_plane(w, eval_loader, path, w_labels, config):
    u = w[2] - w[0]
    dx = np.linalg.norm(u)
    u /= dx

    v = w[1] - w[0]
    v -= np.dot(u, v) * u
    dy = np.linalg.norm(v)
    v /= dy

    m = load_task_model_by_policy(0, 'init', config['exp_dir'])
    m.eval()
    coords = np.stack(get_xy(p, w[0], u, v) for p in w)
    # print("coords", coords)

    G = 15
    margin = 0.2
    alphas = np.linspace(0.0 - margin, 1.0 + margin, G)
    betas = np.linspace(0.0 - margin, 1.0 + margin, G)
    tr_loss = np.zeros((G, G))
    grid = np.zeros((G, G, 2))

    for i, alpha in enumerate(alphas):
        for j, beta in enumerate(betas):
            p = w[0] + alpha * dx * u + beta * dy * v
            m = assign_weights(m, p).to(DEVICE)
            err = eval_single_epoch(m, eval_loader)['loss']
            c = get_xy(p, w[0], u, v)
            #print(c)
            grid[i, j] = [alpha * dx, beta * dy]
            tr_loss[i, j] = err

    contour = {'grid': grid, 'values': tr_loss, 'coords': coords}
    save_np_arrays(contour, path=path)
    plot_contour(grid,
                 tr_loss,
                 coords,
                 log_alpha=-5.0,
                 N=7,
                 path=path,
                 w_labels=w_labels,
                 dataset='mnist')  #config['dataset'])
    return contour