Skip to content

Instantly share code, notes, and snippets.

View gabrieldernbach's full-sized avatar

gabrieldernbach gabrieldernbach

View GitHub Profile
@gabrieldernbach
gabrieldernbach / profiler.py
Created December 14, 2020 14:52
Profiler
import cProfile
import pstats
import io
def profile(file_path=None, breakpoint_after_call=False):
"""decorator for runtime profiling of functions or class methods
prints the profiling statistics sorted by
cumulative time,
total time,
@gabrieldernbach
gabrieldernbach / continous_sync.sh
Last active May 6, 2021 15:57
sync local folder with remote
fswatch ~/repo/ -o | xargs -I {} rsync -avzhe ssh ~/repo/ server:/home/user/repo/ --delete --exclude "venv/*"
import numpy as np
from scipy.stats import chi2_contingency
# example data taken from
# https://en.wikipedia.org/wiki/Chi-squared_test#Example_chi-squared_test_for_categorical_data
X = np.array([
[90, 60, 104, 95],
[30, 50, 51, 20],
[30, 40, 45, 35],
])
@gabrieldernbach
gabrieldernbach / ml_code_golf.py
Last active June 6, 2022 17:40
Solving mnist, fast and short
from torchvision.datasets import MNIST
import numpy as np
def data(train):
mnist = MNIST(root='.', download=True, train=train)
X = mnist.data.numpy().reshape(-1, 784) / 255
y = mnist.targets.numpy()
return X, y
@gabrieldernbach
gabrieldernbach / kernel-k-means.py
Last active June 6, 2022 17:39
fast non linear clustering on millions of datapoints
import numpy as np
import matplotlib.pyplot as plt
from sklearn.kernel_approximation import Nystroem
from sklearn.cluster import MiniBatchKMeans
# dot in the middle
X = np.random.randn(100, 2)
# circle around
Y = X / np.sqrt((X**2).mean(1, keepdims=True)) * 8
Y = Y + np.random.randn(100, 2)
import numpy
import torch
import torch.nn as nn
import torch.nn.functional as F
import matplotlib.pyplot as plt
from torchvision.datasets import MNIST
from torch.utils.data import DataLoader
import torchvision.transforms as T
from einops import rearrange
@gabrieldernbach
gabrieldernbach / Knap_sack.py
Last active June 6, 2022 17:36
A fast solver for the knap-sack problem. This is a branch and bound best-first-search. The bound is derived from the continuous relaxation of the problem, which can be identified as a linear-program and is solved efficiently in 1D by sorting.
import functools
import queue
from random import randint
from random import seed
from random import uniform
from typing import NamedTuple
class Item(NamedTuple):
id: int
@gabrieldernbach
gabrieldernbach / functional.py
Created June 6, 2022 17:51
python method chaining without monads
import functools
from joblib import Parallel, delayed
def compose2(f, g):
return lambda x: g(f(x))
def compose(*fs):
return functools.reduce(compose2, fs)
def pipe(x, *fs):
import torch.nn as nn
class Residual(nn.Module):
def __init__(self, dim):
super().__init__()
self.layer = nn.Sequential(
nn.Conv2d(dim, dim, 7, 1, 3, groups=dim),
nn.BachNorm2d(dim),
nn.Conv2d(dim, dim*4, 1),
nn.ReLU(),
FROM ubuntu:20.04
ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get update; apt-get upgrade -y
RUN apt-get install -y emacs vim r-base r-base-dev libcurl4-openssl-dev
ARG DOWNLOAD_STATIC_LIBV8=1
RUN R -e 'install.packages("rstan")'