Skip to content

Instantly share code, notes, and snippets.

View kastnerkyle's full-sized avatar

Kyle Kastner kastnerkyle

View GitHub Profile
@kastnerkyle
kastnerkyle / semantic_search_with_gzip.py
Created July 15, 2023 14:25 — forked from kyo-takano/lexical_search_with_gzip.py
Semantic Search with gzip (gzipによるセマンティック検索)
import gzip
def gzip_search(query: str, candidate_chunks: list[str], top_k: int=1):
"""
文字列ベースで類似したテキストチャンクを推定するアルゴリズム.
`query`, `chunk`, および`query + " " + chunk`をそれぞれgzipで圧縮し、編集距離のようなものをベースに評価する.
Parameters:
query (str): 検索クエリとして使用する文字列.
top_k (int, optional): 返される類似チャンクの上位k個を指定する (default: 1).
@kastnerkyle
kastnerkyle / string_match.py
Last active December 8, 2022 04:41
String matching using fft / fht (hartley transform). For learning, not optimal speed
# Author: Kyle Kastner
# BSD 3-Clause
# Thanks to jakevdp for the nice blog post on FFT
# https://jakevdp.github.io/blog/2013/08/28/understanding-the-fft/
# Summary
# http://www.arazim-project.com/sites/default/files/public/lesson_sums/1fft.pdf
# Details on hartley and many xforms
# https://caxapa.ru/thumbs/455725/algorithms.pdf
# pg 332 http://sep.stanford.edu/data/media/public/oldreports/sep38/38_29.pdf
@kastnerkyle
kastnerkyle / find_noise.py
Created September 12, 2022 00:00 — forked from trygvebw/find_noise.py
A "reverse" version of the k_euler sampler for Stable Diffusion, which finds the noise that will reconstruct the supplied image
import torch
import k_diffusion as K
from PIL import Image
from torch import autocast
from einops import rearrange, repeat
def pil_img_to_latent(model, img, batch_size=1, device='cuda', half=True):
init_image = pil_img_to_torch(img, half=half).to(device)
init_image = repeat(init_image, '1 ... -> b ...', b=batch_size)
import math
import torch
from torch import optim
class AdamWFinetune(optim.Optimizer):
r"""Implements AdamW algorithm with optional weight decay toward the starting value, to
prevent overfitting to the new dataset during fine-tuning.
The original Adam algorithm was proposed in `Adam: A Method for Stochastic Optimization`_.
@kastnerkyle
kastnerkyle / typical_top_k_top_p.py
Last active June 30, 2022 15:11
My own take on a plug and play setup for typical sampling from Meister et. al. "Typical Decoding for Natural Language Generation". Added top k by typicality for now
def typical_top_k_filtering(logits, top_k=0, top_p=0.0, temperature=1.0, min_tokens_to_keep=1, filter_value=-1E12):
""" Filter a distribution of logits using typicality, with optional top-k and/or nucleus (top-p) filtering
Meister et. al. https://arxiv.org/abs/2202.00666
Args:
logits: logits distribution shape (..., vocabulary size)
top_k >0: keep top k tokens with highest prob (top-k filtering).
top_p >0.0: keep the top p tokens which compose cumulative probability mass top_p (nucleus filtering).
min_tokens_to_keep >=1: always keep at least this many tokens through the top_p / nucleus sampling
"""
# https://arxiv.org/abs/2202.00666
@kastnerkyle
kastnerkyle / bwv101.7.C-minor-transposed.json
Last active February 16, 2021 03:45
Quick and dirty example of piano roll plotting from "music JSON"
{
"seconds_per_quarter": 0.5,
"parts_names": [
"Soprano",
"Alto",
"Tenor",
"Bass"
],
"parts_cumulative_times": [
[
import numpy as np
# make a minibatch of time, batch, features
# time length 7
# batch size 2
# feature dimension 4:
# 1:4, 10:14, 20:24, 30:34, etc for first minibatch element
# 5:8, 15:18, etc second minibatch el
n_features = 4
n_timesteps = 7
@kastnerkyle
kastnerkyle / Kiritan singing voice synthesis demo.ipynb
Created May 3, 2020 06:09 — forked from r9y9/Kiritan singing voice synthesis demo.ipynb
Neural_network_based_singing_voice_synthesis_demo_using_kiritan_singing_database_(Japanese)
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@kastnerkyle
kastnerkyle / make_audiobook.py
Created April 26, 2020 20:55 — forked from madebyollin/make_audiobook.py
Converts an epub or text file to audiobook via Google Cloud TTS
#!/usr/bin/env python3
"""
To use:
1. install/set-up the google cloud api and dependencies listed on https://github.com/GoogleCloudPlatform/python-docs-samples/tree/master/texttospeech/cloud-client
2. install pandoc and pypandoc, also tqdm
3. create and download a service_account.json ("Service account key") from https://console.cloud.google.com/apis/credentials
4. run GOOGLE_APPLICATION_CREDENTIALS=service_account.json python make_audiobook.py book_name.epub
"""
import re
import sys
@kastnerkyle
kastnerkyle / make_audiobook.py
Created April 26, 2020 20:55 — forked from madebyollin/make_audiobook.py
Converts an epub or text file to audiobook via Google Cloud TTS
#!/usr/bin/env python3
"""
To use:
1. install/set-up the google cloud api and dependencies listed on https://github.com/GoogleCloudPlatform/python-docs-samples/tree/master/texttospeech/cloud-client
2. install pandoc and pypandoc, also tqdm
3. create and download a service_account.json ("Service account key") from https://console.cloud.google.com/apis/credentials
4. run GOOGLE_APPLICATION_CREDENTIALS=service_account.json python make_audiobook.py book_name.epub
"""
import re
import sys