Skip to content

Instantly share code, notes, and snippets.

View RF5's full-sized avatar

Matthew Baas RF5

View GitHub Profile
@RF5
RF5 / kmeans.py
Created October 4, 2021 09:45
Pure kmeans in pytorch
""" By Matthew Baas (rf5.github.io) """
import torch
import torch.nn.functional as F
def kmeans_pp_init(X, k, dist_func, tol=1e-9):
"""
`X` is (d, N) , `k` is int;
uses kmeanspp init from https://theory.stanford.edu/~sergei/papers/kMeansPP-soda.pdf
"""
@RF5
RF5 / flac2mp3.py
Created June 25, 2019 19:01
Python script to rapidly convert folders of flac audio files to mp3, preserving metadata and cover art
# pip install fastprogress
# pip install pydub
from pydub import AudioSegment
from pydub.utils import mediainfo
from fastprogress import progress_bar
import os
import glob
from pathlib import Path
from concurrent.futures.process import ProcessPoolExecutor
@RF5
RF5 / semiramis.py
Created April 23, 2018 20:27
Python script to generate VHDL, currently for a DE0 FPGA board.
class DE0_Generator(object):
def __init__(self, base_hz=50e6):
# base clock of 50MHz for DE0 board
self.vhdl = ""
self.processes = []
self.signals = {}
self.with_statements = []
self.base_hz = base_hz
@RF5
RF5 / random_search.py
Created January 26, 2018 16:06
Class for optimizing a machine learning model's hyperparameters with random search
from math import sin, cos, exp, sqrt, pi
import random
def holder_table(di):
x0 = di['x0']
x1 = di['x1']
return -abs(sin(x0)*cos(x1)*exp(abs(1-sqrt(x0*x0+x1*x1)/pi)))
class RandomOptimizer(object):
@RF5
RF5 / attention_util.py
Created January 15, 2018 12:38
Tensorflow script to get Luong-style attention context vector from an rnn output sequence and a previous hidden state
"""
The TL;DR:
What is this? A function to get the global Luong-style context vector for an attention mechanism in tensorflow.
Use case? tensorflow's attention api's are quite restrictive to seq2seq models and aren't very flexible or
require training helpers and sequence feeders that must be used to use their attention api properly.
So, this just gives you the context vector given an rnn output sequence and a previous rnn state (traditionally the decoder's last state)
Note: Scoring is done between the previous hidden state and the encoder rnn's outputs and not the encoder's hidden states,
although for many RNNCells the output is the hidden state