<jupyter_start><jupyter_text>Git commit to reproduce: b4ab2633be4a5048efa579dc3c47c5ff019e446e<jupyter_code># %env CUDA_VISIBLE_DEVICES=
import torch
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()

import pandas as pd
import sys
sys.path.append('source/')

%matplotlib inline
%load_ext autoreload
%autoreload 2
from sklearn.model_selection import ParameterGrid

def foo(script, params, gpus=[], bs=1, changer={}):
    scheduler = []
    n_gpu = len(gpus)
    
    base = 'python ' + script
    for k, _ in params.items():
        base += ' --{} {{{}}}'.format(k, changer.get(k, k))
    if len(gpus) > 0:
        base += ' --gpu {gpu}'

    grid = ParameterGrid(params)
    for i, p in enumerate(grid):
        p['gpu'] = gpus[i % n_gpu]
        run = ''
        run = base.format(**p)
        run += '\n' if (i + 1) % (n_gpu * bs) == 0 else ' &\n'
        scheduler.append(run)

    return scheduler

def get_logs(df, script):
    logs = pd.DataFrame()
    for _id, row in df.iterrows():
        tmp = pd.read_csv(row['root']/'{}-logs.csv'.format(script))
        for k, v in row.items():
            try:
                tmp[k] = v
            except:
                pass
        logs = logs.append(tmp, ignore_index=True)
    return logs
params = {
    'num-examples': [10, 20, 30, 40, 100, 500, 4000],
    'seed': np.arange(4),
    'data-split-seed': [0],
    'vae': ['./vae/z-dim=2,hidden-dim=32/0/'],
    'prior': ['dwp', 'sn'],
    'lr': [0.01, 0.001, 0.0001],
#     'z-dim': [2],
#     'kernel-dim': [16],
    'hid-dim': [20, 40, 100, 500],
    'kl-weight': [1.],
    'name': ['dwp-sn-init-full-grid'],
    'data': ['shapes'],
    'model': ['shapes'],
    'epochs': [200],
    'init': ['vae', 'xavier'],
}

tmp = foo('train-bnn.py', params, np.arange(1, 8), bs=6, changer={'data-split-seed': 'seed'})

with open('./sh-scripts/dwp-sn-init-full-grid.sh', 'w') as f:
    f.write(''.join(tmp))
params = {
    'num-examples': [10, 20, 30, 40, 100, 500, 4000],
    'seed': np.arange(4),
    'data-split-seed': [0],
    'vae': ['./vae/z-dim=2,hidden-dim=32/0/'],
    'lr': [0.01, 0.001, 0.0001],
    'hid-dim': [20, 40, 100, 500],
    'net-cfg': ['vanilla'],
    'name': ['vanilla-init-full-grid'],
    'data': ['shapes'],
    'model': ['shapes'],
    'epochs': [100],
    'init': ['vae', 'xavier'],
}

gpus = [0, 1, 2, 4, 6] # <--- DEFINE GPUs
tmp = foo('train-net.py', params, gpus, bs=6, changer={'data-split-seed': 'seed'})
print('%d exps' % len(tmp))
with open('./sh-scripts/{}.sh'.format(params['name'][0]), 'w') as f:
    f.write(''.join(tmp))
import exman
index = exman.Index('./exman-train-bnn.py/')
df = index.info()

logs = get_logs(df[df['name'] == 'dwp-sn-init-full-grid'], 'train-bnn.py')
logs = logs[logs['t'] == 200]

logs['prior+init'] = logs['prior'] + ' (' + logs['init'] + ' init)'
import exman
index = exman.Index('./exman/')
df = index.info()


vanilla_logs = get_logs(df[df['name'] == 'vanilla-init-full-grid'], 'train-net.py')
vanilla_logs = vanilla_logs[vanilla_logs['t'] == 100]

vanilla_logs['prior+init'] = 'vanilla (' + vanilla_logs['init'] + ' init)'
vanilla_logs['ens[10]_test_acc'] = vanilla_logs['test_acc']
vanilla_logs['num_examples'] = vanilla_logs['num_examples'].astype(int)
tmp = logs.append(vanilla_logs)
tmp.to_csv('SHAPES_Results_DWP+SN.csv')

# max over different lr
tmp = tmp.groupby(['hid_dim', 'num_examples', 'seed', 'prior+init']).agg({'ens[10]_test_acc': 'max'}).reset_index()
sns.catplot(x='hid_dim', y='ens[10]_test_acc',
            data=tmp,
            col='num_examples', hue='prior+init', kind='point')
plt.savefig('./plots/shapes-main-res', dpi=200)


tmp = logs.append(vanilla_logs)
# max over different lr
tmp = tmp.groupby(['hid_dim', 'num_examples', 'seed', 'prior+init']).agg({'ens[10]_test_acc': 'max'}).reset_index()
sns.catplot(col='hid_dim', y='ens[10]_test_acc',
            data=tmp,
            x='num_examples', hue='prior+init', kind='point')
plt.savefig('./plots/shapes-main-res-1', dpi=200)<jupyter_output>/Vol0/user/hse/toretto.gtr/miniconda3/lib/python3.6/site-packages/pandas/core/frame.py:6211: FutureWarning: Sorting because non-concatenation axis is not aligned. A future version
of pandas will change to not sort by default.

To accept the future behavior, pass 'sort=False'.

To retain the current behavior and silence the warning, pass 'sort=True'.

  sort=sort)
/Vol0/user/hse/toretto.gtr/miniconda3/lib/python3.6/site-packages/scipy/stats/stats.py:1713: FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated; use `arr[tuple(seq)]` instead of `arr[seq]`. In the future this will be interpreted as an array index, `arr[np.array(seq)]`, which will result either in an error or a different result.
  return np.add.reduce(sorted[indexer] * weights, axis=axis) / sumval<jupyter_text>------<jupyter_code>def plot_grid(imgs, ncol, nrow, w=2):
    h = imgs.shape[-1]
    tmp = imgs[np.newaxis].reshape((nrow, ncol, h, h))
    tmp = tmp.transpose((0, 2, 1, 3))
    tmp = tmp.reshape((nrow * h, ncol * h))

    plt.figure(figsize=(ncol * w + 2, nrow * w))
    plt.imshow(tmp, cmap='jet', aspect='auto')
    plt.xticks(np.arange(-0.5, ncol * h, h), np.arange(ncol))
    plt.yticks(np.arange(-0.5, nrow * h, h), np.arange(nrow) * ncol)
    plt.colorbar()
import yaml

base = df.query('hid_dim == 40 and num_examples == 10 and name == "dwp-it2-grid" and kl_weight == 1 and lr == 0.01')['root'].values[0]

with open(base/'params.yaml') as f:
    args = yaml.load(f)

from models.vae import Decoder16x16, Encoder16x16, VAE
from models.shapes import ShapeModel

d = torch.device('cuda')
net = ShapeModel(args['net_cfg'], device=d, hid_dim=args['hid_dim'])
net.load_state_dict(torch.load('exman-train-bnn.py/' + args['root'] + '/model.torch'))

m, v = map(utils.tonp, net.net[0].q_params())

m = m.reshape((-1, 16, 16))
v = v.reshape((-1, 16, 16))

plot_grid(m, 8, 5)
plt.title(r'Filters Mean for FFG with DWP prior');
import yaml

base = df.query('hid_dim == 40 and num_examples == 10 and name == "sn-grid" and kl_weight == 1 and lr == 0.01')['root'].values[0]
args = base/'params.yaml'
with open(args) as f:
    args = yaml.load(f)

from models.vae import Decoder16x16, Encoder16x16, VAE
from models.shapes import ShapeModel

d = torch.device('cuda')
net = ShapeModel(args['net_cfg'], device=d, hid_dim=args['hid_dim'])
net.load_state_dict(torch.load('exman-train-bnn.py/' + args['root'] + '/model.torch'))

m, v = map(utils.tonp, net.net[0].q_params())

m = m.reshape((-1, 16, 16))
v = v.reshape((-1, 16, 16))

plot_grid(m, 8, 5)
plt.title(r'Filters Mean for FFG with Standart Normal prior');
logs.query('num_examples == 20 and lr == 0.01 and hid_dim == 40 and prior == "dwp"')['root'].values[0]
ls -l exman-train-bnn.py/runs/004877-2018-08-14-21-59-58
base = logs.query('num_examples == 20 and lr == 0.01 and hid_dim == 40 and prior == "dwp"')['root'].values[0]
args = base/'params.yaml'
with open(args) as f:
    args = yaml.load(f)

from models.vae import Decoder16x16, Encoder16x16, VAE
from models.shapes import ShapeModel

d = torch.device('cuda')
net = ShapeModel(args['net_cfg'], device=d, hid_dim=args['hid_dim'])
net.load_state_dict(torch.load('exman-train-bnn.py/' + args['root'] + '/model.torch'))

m, v = map(utils.tonp, net.net[0].q_params())

m = m.reshape((-1, 16, 16))
v = v.reshape((-1, 16, 16))

plot_grid(m, 8, 5)
plt.title(r'Filters Mean for FFG with Standart Normal prior');

plot_grid(np.sqrt(v), 8, 5)
plt.title(r'Filters Vars for FFG with Standart Normal prior');<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1998416050686378, 'fraction_numerical': 0.04091869060190074, 'mean_word_length': 3.8883870967741934, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '659619', 'license': 'MIT', 'n_tokens_mistral': 3231, 'n_tokens_neox': 2970, 'n_words': 662, 'path': 'experiments/SHAPES_Results_DWP+SN.ipynb', 'repo_name': 'bayesgroup/deep-weight-prior', 'size': '7576'}