Skip to content
Snippets Groups Projects
Verified Commit a8837bee authored by Björn Ludwig's avatar Björn Ludwig
Browse files

refactor(plots notebook): introduce printing of pdf paths

parent 1054a66b
No related branches found
No related tags found
No related merge requests found
Pipeline #31947 passed
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
# Visualizations of activation functions # Visualizations of activation functions
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
from pathlib import Path from pathlib import Path
import plotly.graph_objects as go import plotly.graph_objects as go
import plotly.io as pio import plotly.io as pio
import torch import torch
from plotly.graph_objs import Figure from plotly.graph_objs import Figure
from plotly.io import write_image from plotly.io import write_image
from torch import sigmoid from torch import sigmoid
from torch.nn.functional import softplus from torch.nn.functional import softplus
from pytorch_gum_uncertainty_propagation.functionals import quadlu from pytorch_gum_uncertainty_propagation.functionals import quadlu
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Plot with Plotly ## Plot with Plotly
### Preparations ### Preparations
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
def export_plotly_to_pdf(fig: Figure, filename: str): def export_plotly_to_pdf(fig: Figure, filename: str):
"""Export figure to pdf at the right location right away""" """Export figure to pdf at the right location right away"""
PATH_TO_IMAGES = Path( PATH_TO_IMAGES = Path(
"/home/bjorn/code/GUM-compliant_neural_network_" "/home/bjorn/code/GUM-compliant_neural_network_"
"robustness_verification/src/thesis/images" "robustness_verification/src/thesis/images"
) )
write_image(fig, PATH_TO_IMAGES.joinpath(filename)) write_image(fig, PATH_TO_IMAGES.joinpath(filename))
return filename return filename
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
#### Set up shared Plotly layout parameters #### Set up shared Plotly layout parameters
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
pio.templates.default = "plotly" pio.templates.default = "plotly"
color_blind_seque = ["#2e2b83", "#7e3e69", "#a56753", "#b7a03c", "#abe600"] color_blind_seque = ["#2e2b83", "#7e3e69", "#a56753", "#b7a03c", "#abe600"]
common_font = dict(font=dict(family="Serif", size=15)) common_font = dict(font=dict(family="Serif", size=15))
common_tlr_margin_for_layout = dict( common_tlr_margin_for_layout = dict(
margin_t=5, margin_t=5,
margin_l=63, margin_l=63,
margin_r=63, margin_r=63,
) )
common_tlr_margin_for_layout = dict(
margin_t=5,
margin_l=63,
margin_r=63,
)
common_bottom_axis_title_margins = dict( common_bottom_axis_title_margins = dict(
margin_b=52, margin_b=52,
) )
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
shared_layout = dict( shared_layout = dict(
**common_font, **common_font,
xaxis_title="$x$", xaxis_title="$x$",
yaxis_title="$y$", yaxis_title="$y$",
height=400, height=400,
width=400, width=400,
autosize=False, autosize=False,
showlegend=True, showlegend=True,
) )
shared_ratio = dict( shared_ratio = dict(
scaleanchor="x", scaleanchor="x",
scaleratio=1, scaleratio=1,
) )
shared_title_params = dict( shared_title_params = dict(
y=0.9, y=0.9,
x=0.5, x=0.5,
xanchor="center", xanchor="center",
yanchor="top", yanchor="top",
) )
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Graph of QuadLU ## Graph of QuadLU
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
alphas = [1 / 8 * 2**exponent for exponent in range(3, -1, -1)] alphas = [1 / 8 * 2**exponent for exponent in range(3, -1, -1)]
x_quadlu_tensor = torch.linspace(-2.0, 1.0, 100) x_quadlu_tensor = torch.linspace(-2.0, 1.0, 100)
alphas_and_ys = {} alphas_and_ys = {}
for alpha in alphas: for alpha in alphas:
alphas_and_ys[alpha] = quadlu(x_quadlu_tensor, alpha).detach().numpy() alphas_and_ys[alpha] = quadlu(x_quadlu_tensor, alpha).detach().numpy()
x_quadlu = x_quadlu_tensor.detach().numpy() x_quadlu = x_quadlu_tensor.detach().numpy()
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
fig_quadlu = go.Figure() fig_quadlu = go.Figure()
for idx, (alpha, y_quadlu) in enumerate(alphas_and_ys.items()): for idx, (alpha, y_quadlu) in enumerate(alphas_and_ys.items()):
fig_quadlu.add_trace( fig_quadlu.add_trace(
go.Scatter( go.Scatter(
x=x_quadlu, x=x_quadlu,
y=y_quadlu, y=y_quadlu,
mode="lines", mode="lines",
name=f"$\operatorname{{QuadLU}}_{{\\frac{{1}}{{{int(1 / alpha)}}}}}$", name=f"$\operatorname{{QuadLU}}_{{\\frac{{1}}{{{int(1 / alpha)}}}}}$",
marker_color=color_blind_seque[-idx - 2], marker_color=color_blind_seque[-idx - 2],
) )
) )
x_alphas = [-alpha, -alpha, alpha, alpha, -alpha] x_alphas = [-alpha, -alpha, alpha, alpha, -alpha]
y_alphas = [-0.15, -0.05, -0.05, -0.15, -0.15] y_alphas = [-0.15, -0.05, -0.05, -0.15, -0.15]
fig_quadlu.add_trace( fig_quadlu.add_trace(
go.Scatter( go.Scatter(
x=x_alphas, x=x_alphas,
y=y_alphas, y=y_alphas,
mode="none", mode="none",
fill="toself", fill="toself",
name=f"$[-\\frac{{1}}{{{int(1 / alpha)}}}, \\frac{{1}}{{{int(1 / alpha)}}}]$", name=f"$[-\\frac{{1}}{{{int(1 / alpha)}}}, \\frac{{1}}{{{int(1 / alpha)}}}]$",
fillcolor=color_blind_seque[-idx - 2], fillcolor=color_blind_seque[-idx - 2],
) )
) )
fig_quadlu.update_layout( fig_quadlu.update_layout(
legend=dict(yanchor="auto", y=0.97, xanchor="auto", x=0.1, **common_font), legend=dict(yanchor="auto", y=0.97, xanchor="auto", x=0.1, **common_font),
**common_bottom_axis_title_margins, **common_bottom_axis_title_margins,
**common_tlr_margin_for_layout, **common_tlr_margin_for_layout,
**shared_layout, **shared_layout,
) )
fig_quadlu.update_xaxes( fig_quadlu.update_xaxes(
dict(range=[x_quadlu.min(), x_quadlu.max()]), constrain="domain" dict(range=[x_quadlu.min(), x_quadlu.max()]), constrain="domain"
) )
fig_quadlu.update_yaxes(**shared_ratio) fig_quadlu.update_yaxes(**shared_ratio)
fig_quadlu.show() fig_quadlu.show()
``` ```
%% Output %% Output
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
quadlus_filename = "quadlus_graphs.pdf" quadlus_filename = "quadlus_graphs.pdf"
path_to_pdf = export_plotly_to_pdf(fig_quadlu, quadlus_filename) path_to_pdf = export_plotly_to_pdf(fig_quadlu, quadlus_filename)
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Graph of softplus ## Graph of softplus
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
betas = [1 / 8 * 2**exponent for exponent in range(5)] betas = [1 / 8 * 2**exponent for exponent in range(5)]
x_softplus_tensor = torch.linspace(-3.0, 7, 100) x_softplus_tensor = torch.linspace(-3.0, 7, 100)
betas_and_ys = {} betas_and_ys = {}
for beta in betas: for beta in betas:
betas_and_ys[beta] = softplus(x_softplus_tensor, beta).detach().numpy() betas_and_ys[beta] = softplus(x_softplus_tensor, beta).detach().numpy()
x_softplus = x_softplus_tensor.detach().numpy() x_softplus = x_softplus_tensor.detach().numpy()
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
fig_softplus = go.Figure() fig_softplus = go.Figure()
for idx, (beta, y_softplus) in enumerate(betas_and_ys.items()): for idx, (beta, y_softplus) in enumerate(betas_and_ys.items()):
fig_softplus.add_trace( fig_softplus.add_trace(
go.Scatter( go.Scatter(
x=x_softplus, x=x_softplus,
y=y_softplus, y=y_softplus,
mode="lines", mode="lines",
name=f"$\operatorname{{softplus}}_{{\\frac{{1}}{int(1 / beta)}}}$" name=f"$\operatorname{{softplus}}_{{\\frac{{1}}{int(1 / beta)}}}$"
if beta < 1 if beta < 1
else f"$\operatorname{{softplus}}_{{{int(beta)}}}$", else f"$\operatorname{{softplus}}_{{{int(beta)}}}$",
marker_color=color_blind_seque[-idx - 1], marker_color=color_blind_seque[-idx - 1],
) )
) )
fig_softplus.update_layout( fig_softplus.update_layout(
legend=dict(yanchor="auto", y=1, xanchor="auto", x=0.0, **common_font), legend=dict(yanchor="auto", y=1, xanchor="auto", x=0.0, **common_font),
**common_bottom_axis_title_margins, **common_bottom_axis_title_margins,
**common_tlr_margin_for_layout, **common_tlr_margin_for_layout,
**shared_layout, **shared_layout,
) )
fig_softplus.update_xaxes( fig_softplus.update_xaxes(
dict(range=[x_softplus.min(), x_softplus.max()], constrain="domain") dict(range=[x_softplus.min(), x_softplus.max()], constrain="domain")
) )
fig_softplus.update_yaxes(**shared_ratio) fig_softplus.update_yaxes(**shared_ratio)
fig_softplus.show() fig_softplus.show()
``` ```
%% Output %% Output
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
softpluss_filename = "softpluss_graphs.pdf" softpluss_filename = "softpluss_graphs.pdf"
path_to_pdf = export_plotly_to_pdf(fig_softplus, softpluss_filename) path_to_pdf = export_plotly_to_pdf(fig_softplus, softpluss_filename)
path_to_pdf
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Graph of sigmoid ## Graph of sigmoid
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
x_sigmoid_tensor = torch.linspace(-4.0, 4.0, 100) x_sigmoid_tensor = torch.linspace(-4.0, 4.0, 100)
y_sigmoid = sigmoid(x_sigmoid_tensor).detach().numpy() y_sigmoid = sigmoid(x_sigmoid_tensor).detach().numpy()
x_sigmoid = x_sigmoid_tensor.detach().numpy() x_sigmoid = x_sigmoid_tensor.detach().numpy()
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
fig_sigmoid = go.Figure() fig_sigmoid = go.Figure()
fig_sigmoid.add_trace( fig_sigmoid.add_trace(
go.Scatter( go.Scatter(
x=x_sigmoid, x=x_sigmoid,
y=y_sigmoid, y=y_sigmoid,
mode="lines", mode="lines",
name=f"$\\sigma$", name=f"$\\sigma$",
marker_color=color_blind_seque[1], marker_color=color_blind_seque[1],
) )
) )
fig_sigmoid.update_layout( fig_sigmoid.update_layout(
legend=dict(yanchor="auto", y=1, xanchor="auto", x=0.0, **common_font), legend=dict(yanchor="auto", y=1, xanchor="auto", x=0.0, **common_font),
**common_bottom_axis_title_margins, **common_bottom_axis_title_margins,
**common_tlr_margin_for_layout, **common_tlr_margin_for_layout,
**shared_layout, **shared_layout,
) )
fig_sigmoid.update_xaxes(range=[x_sigmoid.min(), x_sigmoid.max()], constrain="domain") fig_sigmoid.update_xaxes(range=[x_sigmoid.min(), x_sigmoid.max()], constrain="domain")
fig_sigmoid.update_yaxes(range=[-0.5, 1.5]) fig_sigmoid.update_yaxes(range=[-0.5, 1.5])
fig_sigmoid.show() fig_sigmoid.show()
``` ```
%% Output %% Output
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
sigmoid_filename = "sigmoid_graphs.pdf" sigmoid_filename = "sigmoid_graphs.pdf"
path_to_pdf = export_plotly_to_pdf(fig_sigmoid, sigmoid_filename) path_to_pdf = export_plotly_to_pdf(fig_sigmoid, sigmoid_filename)
path_to_pdf
``` ```
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment