This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import importlib.util | |
import inspect | |
import json | |
from pathlib import Path | |
from typing import Optional, Union | |
import zipfile | |
import torch | |
from torch import nn |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
def json_dumps_compact(data, indent: int | str, **kwargs) -> str: | |
""" | |
Dump given JSON document to a string, flattening lists (recursively) but respecting the indent for other objects. | |
:param data: JSON object to be dumped | |
:param indent: indent level for JSON object members (None is not supported) | |
:param kwargs: other arguments passed to :func:`json.dumps` ("separators" is not supported) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Demonstrate how a resource can be shared for exclusive access among the workers of a PyTorch dataloader, by distributing | |
a corresponding lock. | |
At first glance, it might seem that it defies the purpose of using multiple workers by sharing a lock among them. There | |
are some use cases though; for example, (1) the access to the shared resource (and thus the locking) is short and | |
followed by further processing within the worker or (2) there are multiple shared resources and one simply has to ensure | |
that no concurrent access to the same resource happens (in the latter case, multiple locks would need to be shared in | |
the demonstrated way). |