Skip to content

Instantly share code, notes, and snippets.

View thomasahle's full-sized avatar
♟️

Thomas Dybdahl Ahle thomasahle

♟️
View GitHub Profile
@thomasahle
thomasahle / rvq.py
Created October 21, 2022 20:53
Various methods of finding semi-sparse K-means clusterings
import numpy as np
from sklearn.cluster import KMeans
from sklearn.metrics.pairwise import euclidean_distances
import collections
def kl_means(X, k:int, l:int, policy:str):
n, d = X.shape
km = KMeans(k).fit(X)
centers = km.cluster_centers_ / l
labels = np.stack([km.labels_] * l).T
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import sklearn.metrics
def upper(xs, ys, convex=True):
i = np.argsort(xs)
ys = ys[i]
xs = xs[i]
import numpy as np
def quartic(y, n1, n2, ip):
"""A solution to the equation
n1 x2 + n2 (1 - x2) + 2 ip sqrt(x2 (1 - x2)) == y
"""
assert n2 <= y <= n1
d = np.sign(ip) * (ip**4 + ip**2 * (n1 - y) * (y - n2)) ** 0.5
x2 = (2 * ip**2 + (n1 - n2) * (y - n2) - 2 * d) / (4 * ip**2 + (n1 - n2) ** 2)
import tqdm
import torch
import torch.nn as nn
import torch.optim as optim
import argparse
import torchdata.datapipes as dp
from torch.utils.data import DataLoader
from torch.nn import functional as F
import pytorch_lightning as pl
import random
@thomasahle
thomasahle / ndiv.tex
Created November 12, 2023 18:34
Latex code for "does not divide" symbols
\usepackage{graphicx}
\newcommand{\shortslash}{\raisebox{0.2ex}{\scalebox{0.65}{/}}}
\newcommand{\notdivides}{\!\mathrel{\backslash\kern-0.4em\shortslash}\!}
\newcommand{\notdividesTim}{\!\!\mathrel{\rotatebox[origin=c]{20}{$\nmid$}}\!\!}
\def\notdividesHeinrich{\mathpalette\notdiv\relax}
\let\divides=\backslash
\def\notdiv#1#2{\setbox0=\hbox{$#1\divides$}%
\vcenter{\hbox to\wd0{\hss$\scriptscriptstyle/\hss$}}\kern-\wd0
@thomasahle
thomasahle / nonlocals.py
Created November 18, 2023 21:36
Test of nonlocals functions
def nonlocals():
import inspect
stack = inspect.stack()
if len(stack) < 3: return {}
f = stack[2][0]
res = {}
while f.f_back:
res.update({k:v for k,v in f.f_locals.items() if k not in res})
f = f.f_back
return res
@thomasahle
thomasahle / scale_and_square.py
Created November 21, 2023 19:13
Implementation of Highams Algorithm 10.20 (scaling and squaring algorithm). This algorithm evaluates the matrix exponential X = e^A of A ∈ C^{n×n} using the scaling and squaring method.
import numpy as np
import scipy.linalg as sla
theta_m = {3: 1.5e-2, 5: 5.4e-1, 7: 9.5e-1, 9: 2.1e0, 13: 5.4e0}
pade_coefficients = {
3: [120, 60, 12, 1],
5: [30240, 15120, 3360, 420, 30, 1],
7: [17297280, 8648640, 1995840, 277200, 25200, 1512, 56, 1],
9: [17643225600, 8821612800, 2075673600, 302702400, 30270240, 2162160, 110880, 3960, 90, 1],
@thomasahle
thomasahle / twelve.py
Created November 28, 2023 23:47
Probability of winning "game of twelve" with optimal play
import itertools
dp = [0] * 2**12
dp[0] = 1
for state in range(1, 2**12):
for d1, d2 in itertools.product(range(6), repeat=2):
o = 0
s = d1 + d2 + 1
if s < 12 and state & (1 << s):
o = max(o, dp[state & ~(1 << s)])
@thomasahle
thomasahle / soft_topk_bce.py
Created December 6, 2023 20:58
Soft TopK with BCE loss
import torch
from torch.autograd import Function
import torch.nn.functional as F
@torch.no_grad()
def _find_ts(xs, ks, binary_iter=16, newton_iter=1):
n = xs.shape[-1]
assert torch.all((0 < ks) & (ks < n)), "We don't support k=0 or k=n"
# Lo should be small enough that all sigmoids are in the 0 area.
################################################################################
# NFA Implementation using greenery
################################################################################
import greenery
from greenery import rxelems as rx
from collections import defaultdict
class State:
def __init__(self, is_accept=False):