<jupyter_start><jupyter_text>Computations<jupyter_code># First figure: deep ensemble model

p_ensemble_before = Path('log/ensemble')
p_ensemble_after = Path('log/last_ensemble')

# Load models
net = model.MLP(dropout_rate=0.0)
nets = []
for modelname in [e for e in os.listdir(p_ensemble_before / 'models') if e[-2:] == 'pt']:
    mynet = deepcopy(net)
    mynet.load_state_dict(torch.load(Path(p_ensemble_before / 'models') / modelname))
    nets.append(mynet)

# Inference
out = []
x = torch.linspace(-.5, .5, 200).view(-1, 1)
for net in nets:
    net.eval()  # To keep the dropout
    with torch.no_grad():
        out.append(net(x).view(-1))

res = torch.stack(out, 0)
m_ensemble_before, s_ensemble_before = res.mean(0).numpy(), res.std(0).numpy()

# Load models
net = model.MLP(dropout_rate=0.0)
nets = []
for modelname in [e for e in os.listdir(p_ensemble_after / 'models') if e[-2:] == 'pt']:
    mynet = deepcopy(net)
    mynet.load_state_dict(torch.load(Path(p_ensemble_after / 'models') / modelname))
    nets.append(mynet)

# Inference
out = []
x = torch.linspace(-.5, .5, 200).view(-1, 1)
for net in nets:
    net.eval()  # To keep the dropout
    with torch.no_grad():
        out.append(net(x).view(-1))

res = torch.stack(out, 0)
m_ensemble_after, s_ensemble_after = res.mean(0).numpy(), res.std(0).numpy()
# Computations for DENN
# First figure: deep ensemble model

p_denn_before = Path('log/repulsive')
p_denn_after = Path('log/last_repulsive')

# Load models
net = model.MLP(dropout_rate=0.0)
nets = []
for modelname in [e for e in os.listdir(p_denn_before / 'models') if e[-2:] == 'pt']:
    mynet = deepcopy(net)
    mynet.load_state_dict(torch.load(Path(p_denn_before / 'models') / modelname))
    nets.append(mynet)

# Inference
out = []
x = torch.linspace(-.5, .5, 200).view(-1, 1)
for net in nets:
    net.eval()  # To keep the dropout
    with torch.no_grad():
        out.append(net(x).view(-1))

res = torch.stack(out, 0)
m_denn_before, s_denn_before = res.mean(0).numpy(), res.std(0).numpy()

# Load models
net = model.MLP(dropout_rate=0.0)
nets = []
for modelname in [e for e in os.listdir(p_denn_after / 'models') if e[-2:] == 'pt']:
    mynet = deepcopy(net)
    mynet.load_state_dict(torch.load(Path(p_denn_after / 'models') / modelname))
    nets.append(mynet)

# Inference
out = []
x = torch.linspace(-.5, .5, 200).view(-1, 1)
for net in nets:
    net.eval()  # To keep the dropout
    with torch.no_grad():
        out.append(net(x).view(-1))

res = torch.stack(out, 0)
m_denn_after, s_denn_after = res.mean(0).numpy(), res.std(0).numpy()
fig, axes = plt.subplots(1, 2, figsize=(8, 4), squeeze=False, sharex=True, sharey=True)
ax = axes[0, 0]
ax.plot(x_gt, y_gt, 'k--', label='True signal')
ax.fill_between(x.numpy().reshape(-1), m_ensemble_before - s_ensemble_before, m_ensemble_before + s_ensemble_before, color='b', alpha=.3, label='Uncertainty before black swan')
ax.fill_between(x.numpy().reshape(-1), m_ensemble_after - s_ensemble_after, m_ensemble_after + s_ensemble_after, facecolor='None', edgecolor='red', alpha=.3, label='Uncertainty after black swan', hatch="\\\\\\")
ax.plot(x.numpy().reshape(-1), m_ensemble_before - s_ensemble_before, color='b')
ax.plot(x.numpy().reshape(-1), m_ensemble_before + s_ensemble_before, color='b')
ax.plot(x.numpy().reshape(-1), m_ensemble_after - s_ensemble_after, color='orangered', linestyle='dotted')
ax.plot(x.numpy().reshape(-1), m_ensemble_after + s_ensemble_after, color='orangered', linestyle='dotted')
#ax.plot(x.numpy(), res[0, :].numpy(), c='m', label='Sample function')
ax.scatter(x_train, y_train, marker='+', c='k', s=200, label='Data')
blackswan = ax.scatter(0.25, f(0.25), marker='x', c='red', s=200, linewidth=3, label='Black swan event')
ax.axis([-.55, .55, -.6, 1.15])
fig.legend(prop={'size':15}, loc=(.13, .64), ncol=2)
ax.set_title('Deep ensemble')

ax = axes[0, 1]
ax.plot(x_gt, y_gt, 'k--', label='True signal')
ax.fill_between(x.numpy().reshape(-1), m_denn_before - s_denn_before, m_denn_before + s_denn_before, color='b', alpha=.3, label='Uncertainty before black swan')
ax.fill_between(x.numpy().reshape(-1), m_denn_after - s_denn_after, m_denn_after + s_denn_after, facecolor='None', edgecolor='red', alpha=.3, label='Uncertainty after black swan', hatch="\\\\\\")
ax.plot(x.numpy().reshape(-1), m_denn_before - s_denn_before, color='b')
ax.plot(x.numpy().reshape(-1), m_denn_before + s_denn_before, color='b')
ax.plot(x.numpy().reshape(-1), m_denn_after - s_denn_after, color='orangered', linestyle='dotted')
ax.plot(x.numpy().reshape(-1), m_denn_after + s_denn_after, color='orangered', linestyle='dotted')
#ax.plot(x.numpy(), res[0, :].numpy(), c='m', label='Sample function')
ax.scatter(x_train, y_train, marker='+', c='k', s=200, label='Data')
blackswan = ax.scatter(0.25, f(0.25), marker='x', c='red', s=200, linewidth=3, label='Black swan event')
ax.axis([-.55, .55, -.6, 1.15])
ax.set_title('DENN')

plt.tight_layout()
plt.subplots_adjust(hspace=.1, wspace=.05)<jupyter_output><empty_output><jupyter_text>filename = 'illustration-objective-2'path_figs = Path('img')if not Path.exists(path_figs):    os.makedirs(path_figs)path_savefig = path_figs / '{}.pdf'.format(filename)fig.savefig(path_savefig)<jupyter_code>10**(-.5)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1919134396355353, 'fraction_numerical': 0.02619589977220957, 'mean_word_length': 5.126744186046512, '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': '219754', 'license': 'MIT', 'n_tokens_mistral': 2253, 'n_tokens_neox': 2162, 'n_words': 396, 'path': 'first_figure/generate_figure.ipynb', 'repo_name': 'maxwab/denn-ijcai', 'size': '5268'}