Skip to content

Instantly share code, notes, and snippets.

@proger
proger / abv.py
Last active April 26, 2024 07:53
# prompt: https://twitter.com/francoisfleuret/status/1783479122418716805
import os
os.environ['TORCH_LOGS'] = 'output_code' # shows all the bmms
import torch
torch.set_float32_matmul_precision('high')
N, T, D, U, C = 3, 128, 5, 32, 32 # batch, time, heads, head_dim, dim
S = T
A = torch.randn(N, T, D, U) / U**0.5
@francois-rozet
francois-rozet / flow_matching.py
Last active September 4, 2024 07:44
Flow Matching in 100 LOC
#!/usr/bin/env python
import math
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
from sklearn.datasets import make_moons
from torch import Tensor
from tqdm import tqdm
@crowsonkb
crowsonkb / spherical_avg.py
Created April 26, 2021 22:51
Spherical weighted average
import geoopt
def spherical_avg(p, w=None, tol=1e-6):
sphere = geoopt.Sphere()
if w is None:
w = p.new_ones([p.shape[0]])
assert p.ndim == 2 and w.ndim == 1 and len(p) == len(w)
w = w / w.sum()
p = sphere.projx(p)
@crowsonkb
crowsonkb / mdmm_2.py
Created February 1, 2021 00:47
Modified Differential Multiplier Method
import abc
import torch
from torch import nn, optim
class Constraint(nn.Module, metaclass=abc.ABCMeta):
def __init__(self, fn, damping):
super().__init__()
self.fn = fn
# git clone https://github.com/NVlabs/stylegan2
import os
import numpy as np
from scipy.interpolate import interp1d
from scipy.io import wavfile
import matplotlib.pyplot as plt
import PIL.Image
import moviepy.editor
import dnnlib
@hiromu
hiromu / main.py
Created December 24, 2017 18:14
Speaker Identification using GMM on MFCC
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import glob
import librosa
import numpy as np
import os
import sklearn.mixture
import sys
#!/usr/bin/env python
'''Crop an image to just the portions containing text.
Usage:
./crop_morphology.py path/to/image.jpg
This will place the cropped image in path/to/image.crop.png.
For details on the methodology, see
@victor-shepardson
victor-shepardson / pytorch-glumpy.py
Last active March 25, 2024 19:47
using pycuda and glumpy to draw pytorch GPU tensors to the screen without copying to host memory
from contextlib import contextmanager
import numpy as np
import torch
from torch import Tensor, ByteTensor
import torch.nn.functional as F
from torch.autograd import Variable
import pycuda.driver
from pycuda.gl import graphics_map_flags
from glumpy import app, gloo, gl