Skip to content

Instantly share code, notes, and snippets.

View selflein's full-sized avatar

Sven selflein

View GitHub Profile
@selflein
selflein / simple_diffusion_two_moons.ipynb
Created November 18, 2022 16:10
Diffusion model on a toy dataset
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@selflein
selflein / normalizing_flow_compact_support.ipynb
Created January 10, 2022 23:42
Learning distributions on compact support with Normalizing Flows
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@selflein
selflein / format_df.py
Last active August 3, 2021 08:58
[Format best values in pandas DataFrame] #Python #Pandas
import pandas as pd
def format_best(to_bold, numeric_df=None, order="max", format_str="\\bfseries{{{}}}"):
"""Format the row with the best value within each column of a pandas DataFrame in a particular way.
Args:
to_bold: pandas.DataFrame to operate on in-place.
numeric_df: Optional pandas.DataFrame from which the best rows should be infered.
order: Define what is the row that should be formatted. Supported: `max`, `min`, `second_highest`, `second_lowest`. If it is a list then it is interpreted in a per column way, otherwise applies to all columns.
@selflein
selflein / brier_decomposition.py
Created December 6, 2020 14:15
[Three-component Brier decomposition] Decomposition of Brier score into three components uncertainty, reliability and resolution #numpy #python #calibration
def brier_decomposition(labels, probs):
"""Compute the decompositon of the Brier score into its three components
uncertainty (UNC), reliability (REL) and resolution (RES).
Brier score is given by `BS = REL - RES + UNC`. The decomposition requires partioning
into discrete events. Partioning into probability classes `M_k` is done for `p_k > p_i`
for all `i!=k`. This induces a error when compared to the Brier score.
For more information on the partioning see
Murphy, A. H. (1973). A New Vector Partition of the Probability Score, Journal of Applied Meteorology and Climatology, 12(4)
@selflein
selflein / seaborn_setting.py
Last active September 6, 2021 18:52
[Seaborn settings for paper plots] #matplotlib #publication
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
sns.set_style("ticks")
sns.set_context("paper", font_scale=1.0)
params = {
'text.usetex' : True,
'font.size' : 11,
@selflein
selflein / grad_norm_logger.py
Created April 15, 2020 19:56
[Logger for gradient norms in PyTorch with Tensorboard] #pytorch
class GradNormLogger:
def __init__(self):
self.grad_norms = defaultdict(list)
def update(self, model: torch.nn.Module, norm_type: float = 2.):
total_norm = 0
for name, p in model.named_parameters():
if p.requires_grad:
try:
@selflein
selflein / example.py
Last active February 1, 2020 13:00
Save image with same resolution without border etc. using matplotlib #python #matplotlib
dpi = 80
# What size does the figure need to be in inches to fit the image?
figsize = width / float(dpi), height / float(dpi)
# Create a figure of the right size with one axes that takes up the full figure
fig = plt.figure(figsize=figsize)
ax = fig.add_axes([0, 0, 1, 1])
ax.set_aspect('equal', adjustable='box')