def plot():
    from hypergrad.omniglot import show_all_alphabets
    import matplotlib.pyplot as plt
    import matplotlib as mpl
    from spectral_clustering.spclust import find_blockifying_perm
    with open('results.pkl') as f:
        results, grad_transform_dict = pickle.load(f)

    fig = plt.figure(0)
    fig.set_size_inches((6,4))
    ax = fig.add_subplot(111)
    ax.set_title('Performance vs hyper step')
    for k, v in results.iteritems():
        ax.plot(line_search_dists, v, 'o-', label=k)
    ax.set_xlabel('Step size')
    ax.set_ylabel('Negative log prob')
    ax.legend(loc=1, frameon=False)
    plt.savefig('performance.png')

    # Plotting learned transforms
    step_size = 0.2
    T_baseline = make_transform(N_scripts, script_corr_init).as_dict()
    print grad_transform_dict[0]
    T_learned  = {i : T_baseline[i] + step_size * grad_transform_dict[i] for i in T_baseline.keys()}

    covar_baseline = dictmap(T_to_covar, T_baseline)
    covar_learned  = dictmap(T_to_covar, T_learned)

    covar_baseline = dictmap(covar_to_corr, covar_baseline)
    covar_learned  = dictmap(covar_to_corr, covar_learned)

    covar_baseline = dictmap(np.abs, covar_baseline)
    covar_learned  = dictmap(np.abs, covar_learned)

    # Blockify:
    npr.seed(3)
    perm = find_blockifying_perm(covar_learned[0], 1, 3)
    show_all_alphabets(perm)
    print perm
    def permute_array(A):
        return A[np.ix_(perm, perm)]

    covar_baseline = dictmap(permute_array, covar_baseline)
    covar_learned  = dictmap(permute_array, covar_learned)

    all_baseline = np.concatenate([covar_baseline[i] for i in range(N_layers)], axis=0)
    all_learned  = np.concatenate([covar_learned[i]  for i in range(N_layers)], axis=0)
    all_img = np.concatenate((all_baseline, all_learned), axis=1)
    all_img = np.minimum(np.maximum(all_img, 0.0), 0.2)
    fig = plt.figure(0)
    fig.clf()
    fig.set_size_inches((6,8))
    ax = fig.add_subplot(111)
    ax.imshow(all_img, cmap = mpl.cm.binary)
    ax.set_title('Original / learned')
    ax.set_ylabel('Layer number')
    ax.set_xticks([])
    ax.set_yticks([])
    plt.savefig('learned_covar_permuted.png')
Example #2
0
def plot():
    from hypergrad.omniglot import show_all_alphabets
    import matplotlib.pyplot as plt
    import matplotlib as mpl
    from spectral_clustering.spclust import find_blockifying_perm
    with open('results.pkl') as f:
        results, grad_transform_dict = pickle.load(f)

    fig = plt.figure(0)
    fig.set_size_inches((6,4))
    ax = fig.add_subplot(111)
    ax.set_title('Performance vs hyper step')
    for k, v in results.iteritems():
        ax.plot(line_search_dists, v, 'o-', label=k)
    ax.set_xlabel('Step size')
    ax.set_ylabel('Negative log prob')
    ax.legend(loc=1, frameon=False)
    plt.savefig('performance.png')

    # Plotting learned transforms
    step_size = 0.2
    T_baseline = make_transform(N_scripts, script_corr_init).as_dict()
    print grad_transform_dict[0]
    T_learned  = {i : T_baseline[i] + step_size * grad_transform_dict[i] for i in T_baseline.keys()}

    covar_baseline = dictmap(T_to_covar, T_baseline)
    covar_learned  = dictmap(T_to_covar, T_learned)

    covar_baseline = dictmap(covar_to_corr, covar_baseline)
    covar_learned  = dictmap(covar_to_corr, covar_learned)

    covar_baseline = dictmap(np.abs, covar_baseline)
    covar_learned  = dictmap(np.abs, covar_learned)

    # Blockify:
    npr.seed(3)
    perm = find_blockifying_perm(covar_learned[0], 1, 3)
    show_all_alphabets(perm)
    print perm
    def permute_array(A):
        return A[np.ix_(perm, perm)]

    covar_baseline = dictmap(permute_array, covar_baseline)
    covar_learned  = dictmap(permute_array, covar_learned)

    all_baseline = np.concatenate([covar_baseline[i] for i in range(N_layers)], axis=0)
    all_learned  = np.concatenate([covar_learned[i]  for i in range(N_layers)], axis=0)
    all_img = np.concatenate((all_baseline, all_learned), axis=1)
    all_img = np.minimum(np.maximum(all_img, 0.0), 0.2)
    fig = plt.figure(0)
    fig.clf()
    fig.set_size_inches((6,8))
    ax = fig.add_subplot(111)
    ax.imshow(all_img, cmap = mpl.cm.binary)
    ax.set_title('Original / learned')
    ax.set_ylabel('Layer number')
    ax.set_xticks([])
    ax.set_yticks([])
    plt.savefig('learned_covar_permuted.png')
Example #3
0
def plot():
    from hypergrad.omniglot import show_all_alphabets
    import matplotlib.pyplot as plt
    import matplotlib as mpl
    #from spectral_clustering.spclust import find_blockifying_perm
    with open('results.pkl') as f:
        results, all_transforms = pickle.load(f)
    fig = plt.figure(0)
    fig.set_size_inches((6,4))
    ax = fig.add_subplot(111)
    ax.set_title('Performance vs hyper step')
    for k, v in results.iteritems():
        ax.plot(v, 'o-', label=k)
    ax.set_xlabel('Hyper iteration')
    ax.set_ylabel('Negative log prob')
    ax.legend(loc=1, frameon=False)
    plt.savefig('performance.png')

    # Plotting learned transforms
    T_baseline = make_transform(N_scripts, script_corr_init).as_dict()
    print all_transforms[-1][0]
    T_learned  = {i : all_transforms[-1][i] for i in T_baseline.keys()}

    covar_baseline = dictmap(T_to_covar, T_baseline)
    covar_learned  = dictmap(T_to_covar, T_learned)

    # covar_baseline = dictmap(covar_to_corr, covar_baseline)
    # covar_learned  = dictmap(covar_to_corr, covar_learned)

    covar_baseline = dictmap(np.abs, covar_baseline)
    covar_learned  = dictmap(np.abs, covar_learned)

    # # Blockify:
    # npr.seed(1)
    # perm = find_blockifying_perm(covar_learned[0], 10, 3)

    # Sort by magnitude
    perm = np.argsort(np.sum(covar_learned[0], axis=0))

    def permute_array(A):
        return A[np.ix_(perm, perm)]
    show_all_alphabets(perm)
    covar_baseline = dictmap(permute_array, covar_baseline)
    covar_learned  = dictmap(permute_array, covar_learned)

    all_baseline = np.concatenate([covar_baseline[i] for i in range(N_layers)], axis=0)
    all_learned  = np.concatenate([covar_learned[i]  for i in range(N_layers)], axis=0)
    all_img = np.concatenate((all_baseline, all_learned), axis=1)
    all_img = np.minimum(np.maximum(all_img, 0.0), 1)
    fig = plt.figure(0)
    fig.clf()
    fig.set_size_inches((6,8))
    ax = fig.add_subplot(111)
    ax.imshow(all_img, cmap = mpl.cm.binary)
    ax.set_title('Original / learned')
    ax.set_ylabel('Layer number')
    ax.set_xticks([])
    ax.set_yticks([])
    plt.savefig('learned_covar_permuted.png')


    # Plot various covariance matrices.
    def boxplot( matrix, cmap, name):
        fig = plt.figure(0)
        fig.clf()
        fig.set_size_inches((3,3))
        ax = fig.add_subplot(111)
        ax.matshow(matrix, cmap=cmap)
        plt.xticks(np.array([]))
        plt.yticks(np.array([]))
        plt.savefig(name, bbox_inches='tight')

    boxplot(covar_learned[0],                  mpl.cm.binary, 'covar_learned_toplayer.pdf')
    boxplot(np.eye(covar_learned[0].shape[0]), mpl.cm.binary, 'covar_eye.pdf')
    boxplot(np.ones(covar_learned[0].shape),   mpl.cm.hot,    'covar_full.pdf')
Example #4
0
def plot():
    from hypergrad.omniglot import show_all_alphabets
    import matplotlib.pyplot as plt
    import matplotlib as mpl
    #from spectral_clustering.spclust import find_blockifying_perm
    with open('results.pkl') as f:
        results, all_transforms = pickle.load(f)
    fig = plt.figure(0)
    fig.set_size_inches((6, 4))
    ax = fig.add_subplot(111)
    ax.set_title('Performance vs hyper step')
    for k, v in results.iteritems():
        ax.plot(v, 'o-', label=k)
    ax.set_xlabel('Hyper iteration')
    ax.set_ylabel('Negative log prob')
    ax.legend(loc=1, frameon=False)
    plt.savefig('performance.png')

    # Plotting learned transforms
    T_baseline = make_transform(N_scripts, script_corr_init).as_dict()
    print all_transforms[-1][0]
    T_learned = {i: all_transforms[-1][i] for i in T_baseline.keys()}

    covar_baseline = dictmap(T_to_covar, T_baseline)
    covar_learned = dictmap(T_to_covar, T_learned)

    # covar_baseline = dictmap(covar_to_corr, covar_baseline)
    # covar_learned  = dictmap(covar_to_corr, covar_learned)

    covar_baseline = dictmap(np.abs, covar_baseline)
    covar_learned = dictmap(np.abs, covar_learned)

    # # Blockify:
    # npr.seed(1)
    # perm = find_blockifying_perm(covar_learned[0], 10, 3)

    # Sort by magnitude
    perm = np.argsort(np.sum(covar_learned[0], axis=0))

    def permute_array(A):
        return A[np.ix_(perm, perm)]

    show_all_alphabets(perm)
    covar_baseline = dictmap(permute_array, covar_baseline)
    covar_learned = dictmap(permute_array, covar_learned)

    all_baseline = np.concatenate([covar_baseline[i] for i in range(N_layers)],
                                  axis=0)
    all_learned = np.concatenate([covar_learned[i] for i in range(N_layers)],
                                 axis=0)
    all_img = np.concatenate((all_baseline, all_learned), axis=1)
    all_img = np.minimum(np.maximum(all_img, 0.0), 1)
    fig = plt.figure(0)
    fig.clf()
    fig.set_size_inches((6, 8))
    ax = fig.add_subplot(111)
    ax.imshow(all_img, cmap=mpl.cm.binary)
    ax.set_title('Original / learned')
    ax.set_ylabel('Layer number')
    ax.set_xticks([])
    ax.set_yticks([])
    plt.savefig('learned_covar_permuted.png')

    # Plot various covariance matrices.
    def boxplot(matrix, cmap, name):
        fig = plt.figure(0)
        fig.clf()
        fig.set_size_inches((3, 3))
        ax = fig.add_subplot(111)
        ax.matshow(matrix, cmap=cmap)
        plt.xticks(np.array([]))
        plt.yticks(np.array([]))
        plt.savefig(name, bbox_inches='tight')

    boxplot(covar_learned[0], mpl.cm.binary, 'covar_learned_toplayer.pdf')
    boxplot(np.eye(covar_learned[0].shape[0]), mpl.cm.binary, 'covar_eye.pdf')
    boxplot(np.ones(covar_learned[0].shape), mpl.cm.hot, 'covar_full.pdf')