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 / 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.