Skip to content

Instantly share code, notes, and snippets.


Modar M. Alfadly xmodar

View GitHub Profile
xmodar /
Last active February 10, 2023 17:49
Seamless running stats for (native python, numpy.ndarray, torch.tensor). Also see:
"""Seamless running stats for (native python, numpy.ndarray, torch.tensor)."""
from collections import namedtuple
class MeanMeter:
"""Estimate the mean for a stream of values."""
def __init__(self):
"""Initialize the meter."""
xmodar /
Last active November 18, 2022 12:16
torch.cov and torch.corrcoef equivalent to np.cov and np.corrcoef, respectively with gradient support.
def cov(tensor, rowvar=True, bias=False):
"""Estimate a covariance matrix (np.cov)"""
tensor = tensor if rowvar else tensor.transpose(-1, -2)
tensor = tensor - tensor.mean(dim=-1, keepdim=True)
factor = 1 / (tensor.shape[-1] - int(not bool(bias)))
return factor * tensor @ tensor.transpose(-1, -2).conj()
xmodar /
Last active August 8, 2022 18:54
Frechet's distance entirely in PyTorch with data batches streaming support.
"""Frechet's distance between two multi-variate Gaussians"""
import torch
import torch.nn as nn
class FrechetDistance:
"""Frechet's distance between two multi-variate Gaussians
def __init__(self, double=True, num_iterations=20, eps=1e-12):
View lambertw.ts
* Lambert W-function when k = 0
* {@link}
* {@link}
export function lambertW(x: number, log = false): number {
if (log) return lambertWLog(x); // x is actually log(x)
if (x >= 0) return lambertWLog(Math.log(x)); // handles [0, Infinity]
const xE = x * Math.E;
if (isNaN(x) || xE < -1) return NaN; // handles NaN and [-Infinity, -1 / Math.E)
xmodar / python.ts
Last active March 21, 2022 01:53
JavaScript utilities to mimic Python
View python.ts
/** {@link} */
export const operator = {
lt: <T>(a: T, b: T) => a < b,
le: <T>(a: T, b: T) => a <= b,
eq: <T>(a: T, b: T) => a === b,
ne: <T>(a: T, b: T) => a !== b,
ge: <T>(a: T, b: T) => a >= b,
gt: <T>(a: T, b: T) => a > b,
not: <T>(a: T) => !a,
abs: (a: number) => Math.abs(a),
"""Resnet + SVM"""
import torch
from torch import nn
import torchvision.transforms as T
from torchvision import models
class SVM(nn.Module):
"""Multi-Class SVM with Gaussian Kernel (Radial Basis Function)
xmodar /
Last active November 20, 2021 03:44
A more flexible context manager than `torch.random.fork_rng()` to preserve the state of the random number generator in PyTorch for the desired devices.
import torch
class RNG():
"""Preserve the state of the random number generators of torch
Inspired by torch.random.fork_rng().
Seeding random number generators (RNGs):
xmodar /
Last active November 17, 2021 15:38
"""InvTorch: Core Invertible Utilities"""
import itertools
import collections
import torch
from torch import nn
import torch.utils.checkpoint
__all__ = ['invertible_checkpoint', 'InvertibleModule']
"""Invertible BatchNorm"""
import torch
from torch import nn
class NonZero(nn.Module):
"""Parameterization to force the values to be nonzero"""
def __init__(self, eps=1e-5, preserve_sign=True):
self.eps, self.preserve_sign = eps, preserve_sign
xmodar /
Last active October 25, 2021 17:47
import torch
from torch import nn
class Deconv(nn.Module):
"""Inverse conv"""
def __init__(self, conv, output_padding=0):
dim = len(conv.padding)
if isinstance(output_padding, int):