Skip to content

Instantly share code, notes, and snippets.

View spezold's full-sized avatar

Simon Pezold spezold

  • Basel (CH)
View GitHub Profile
@spezold
spezold / mean_std_torch.py
Created May 17, 2024 14:38
Calculate stable mean and standard deviation over a potentially large sample of values, using Chan et al.'s version of Welford's online algorithm (same as "mean_std.py", but in PyTorch)
"""
Calculate sample mean and std. over all desired axes of given samples, using Chan et al.'s version of Welford's online
algorithm; cf. https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Parallel_algorithm (20240515) and
equations (2.1a), (2.1b) in the referenced paper [1]_; or likewise, equations (1.5a), (1.5b) in [2]_.
Mind the typo in [1]_, (2.1a): T_{1, m+n} on the right side of the equation should be T_{m+1, m+n} (cf. [2]_, (1.5a)).
References
----------
.. [1] T. F. Chan, G. H. Golub, and R. J. LeVeque, “Updating Formulae and a Pairwise Algorithm for Computing Sample
Variances,” in COMPSTAT 1982 5th Symposium held at Toulouse 1982, Heidelberg, 1982, pp. 30–41,
doi: 10.1007/978-3-642-51461-6_3.
@spezold
spezold / mean_std.py
Last active May 17, 2024 14:36
Calculate stable mean and standard deviation over a potentially large sample of values, using Chan et al.'s version of Welford's online algorithm
"""
Calculate sample mean and std. over all desired axes of given samples, using Chan et al.'s version of Welford's online
algorithm; cf. https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Parallel_algorithm (20240515) and
equations (2.1a), (2.1b) in the referenced paper [1]_; or likewise, equations (1.5a), (1.5b) in [2]_.
Mind the typo in [1]_, (2.1a): T_{1, m+n} on the right side of the equation should be T_{m+1, m+n} (cf. [2]_, (1.5a)).
References
----------
.. [1] T. F. Chan, G. H. Golub, and R. J. LeVeque, “Updating Formulae and a Pairwise Algorithm for Computing Sample
@spezold
spezold / my_modulefinder.py
Last active April 14, 2022 09:00
Find all modules that are imported by the given project, list the code files (*.py, *.ipynb) that use them, and try to distinguish between STL and non-STL modules.
"""
CAUTION: Make sure that
1. this file is placed in the root directory of the project of interest
(or otherwise, adjust `BASE_DIR` accordingly);
2. the file is run in the same Python environment (conda environment, poetry environment, ...)
as the project of interest (so activate the corresponding environment first, if necessary).
"""
from collections import defaultdict
from importlib.util import find_spec
@spezold
spezold / rolling_window_inside_and_outside.py
Last active August 19, 2021 19:20
Return both the values inside and outside of a rolling window over a 1D PyTorch tensor as a 2D tensor
from typing import Tuple
import torch
from torch import Tensor
# The straightforward solution
def rolling_window_inside_and_outside(t: Tensor, size: int, stride: int=1) -> Tuple[Tensor, Tensor]:
"""
Given a 1D tensor, provide both the values inside the rolling window and outside the rolling window for each window
position with the given window size and stride.
@spezold
spezold / save_and_load_model.py
Last active June 16, 2021 11:42
**Update: have a look at torch.package instead** (https://pytorch.org/docs/1.9.0/package.html) -- Original description: Save and load a PyTorch model (both code and weights): minimum working example, based on the inner workings of TorchServe.
import importlib.util
import inspect
import json
from pathlib import Path
from typing import Optional, Union
import zipfile
import torch
from torch import nn
@spezold
spezold / torch_percentile.py
Last active December 23, 2021 15:33
*Update (2020-10-28)*: with the introduction of torch.quantile (https://pytorch.org/docs/stable/generated/torch.quantile.html) in PyTorch 1.7 this gist has become largely obsolete – Calculate percentile of a PyTorch tensor's values, similar to numpy.percentile
from typing import Union
import torch
import numpy as np
def percentile(t: torch.tensor, q: float) -> Union[int, float]:
"""
Return the ``q``-th percentile of the flattened input tensor's data.