Skip to content

Instantly share code, notes, and snippets.

View harpone's full-sized avatar

Heikki Arponen harpone

View GitHub Profile
@harpone
harpone / Cython
Last active August 29, 2015 14:07
Stochastic differential equations: Python+Numpy vs. Cython. FIGHT!!
# Same with Cython:
import numpy as np
cimport cython
cimport numpy as np
from libc.stdint cimport uint32_t, int32_t
from libc.math cimport sqrt
from libc.math cimport fabs
from libc.math cimport pow
@harpone
harpone / gist:401b91d48652d7138844
Last active August 29, 2015 14:07
Pure Python
def simulation(L = 0, N = 100000, dt = 1E-3, init = .1):
"""Simulate a stochastic differential equation.
"""
#Set up some parameters:
f1 = .1
g1 = .01
g2 = .1
dW = np.random.randn(N)*np.sqrt(dt)
import numpy as np
cimport cython
cimport numpy as np
from libc.stdint cimport uint32_t, int32_t
from libc.math cimport sqrt
from libc.math cimport fabs
from libc.math cimport pow
import tensorflow as tf
from tensorflow.python.framework import ops
import numpy as np
# Define custom py_func which takes also a grad op as argument:
def py_func(func, inp, Tout, stateful=True, name=None, grad=None):
# Need to generate a unique name to avoid duplicates:
rnd_name = 'PyFuncGrad' + str(np.random.randint(0, 1E+8))
class GCSDataset(Dataset):
"""Generic PyTorch dataset for GCS. Streams data from GCS and (optionally) caches to local disk.
"""
def __init__(self,
bucketname=None,
path_list=None, # TODO: list bucket/path contents if None
target_list=None,
transform=None,
@harpone
harpone / lamb.py
Created September 24, 2020 12:25
Lamb optimizer that doesn't work on TPUs
class Lamb(Optimizer):
r"""Implements Lamb algorithm.
It has been proposed in `Large Batch Optimization for Deep Learning: Training BERT in 76 minutes`_.
Arguments:
params (iterable): iterable of parameters to optimize or dicts defining
parameter groups
lr (float, optional): learning rate (default: 1e-3)
betas (Tuple[float, float], optional): coefficients used for computing
running averages of gradient and its square (default: (0.9, 0.999))
eps (float, optional): term added to the denominator to improve
@harpone
harpone / kl_divergence.py
Last active January 12, 2024 22:09
Differentiable k-nearest neighbor (Kozachenko-Leonenko) based estimates of KL-divergence and entropy
"""
MIT License
knn, kl_div, entropy Copyright (c) 2017 Heikki Arponen
"""
import torch
def knn(x, y, k=3, last_only=False, discard_nearest=True):
@harpone
harpone / profile_openimages_dataload.py
Last active December 3, 2020 13:22
Testing/profiling webdataset data loading speed issue
from itertools import islice
from munch import Munch
import sys, os
from torch.utils.data import DataLoader
from torchvision import transforms
import time
import webdataset as wds
sys.path.append(os.getcwd())
@harpone
harpone / torchxla_webdataset_example.py
Created January 15, 2021 12:05
Test Webdataset with torch-xla multiprocessing distributed setting
from itertools import islice
import os
import torch
from torch.utils.data import DataLoader
from torchvision import transforms
import numpy as np
import torch_xla.distributed.parallel_loader as pl
import torch_xla.core.xla_model as xm
import torch_xla.distributed.xla_multiprocessing as xmp
@harpone
harpone / gist:677eea8f28289ff51cf2bc69ec63bf10
Created January 10, 2022 14:36
STM_longcovid_translation.txt
Long-lasting COVID-19 -
Consensus statement of the expert group appointed by STM on 31 December 2021
VN / 20672/2021
DRAFT 7.1.2022