Skip to content

Instantly share code, notes, and snippets.

View sjmielke's full-sized avatar

Sabrina J. Mielke sjmielke

View GitHub Profile
#!/usr/bin/env python3
# each metric is a list---they must all be of equal length:
# len([bg, cs, da, de, el, es, et, fi, fr, hu, it, lt, lv, nl, pl, pt, ro, sk, sl, sv])
raw_metrics = {
"XMI into English": [102.3396, 96.9558, 99.6932, 96.53, 105.2602, 103.8174, 92.8232, 92.1413, 96.9629, 92.54, 92.0796, 89.171, 94.1704, 86.4946, 91.8679, 102.4575, 106.0929, 99.7949, 100.0662, 96.8784],
"XMI from English": [106.2096, 102.8122, 103.3194, 104.0, 111.0332, 108.0881, 100.1656, 98.0227, 99.7149, 99.1018, 95.3096, 96.0001, 99.3214, 90.3871, 98.2996, 105.2407, 112.4211, 105.7748, 107.9064, 100.1222],
"BLEU into English": [47.4, 42.4, 46.3, 44.0, 50.0, 50.6, 39.3, 38.2, 44.9, 38.4, 40.8, 37.6, 40.3, 38.3, 39.8, 48.3, 50.5, 44.2, 45.3, 43.7],
"BLEU from English": [46.3, 34.7, 45.0, 36.3, 45.5, 50.2, 27.7, 30.5, 45.7, 30.3, 37.9, 31.0, 34.6, 34.9, 30.5, 46.7, 44.2, 39.8, 41.5, 41.3],
"q_MT(T | S) into English": [51.8457, 57.2295, 54.4921, 57.6553, 48.9251, 50.3679, 61.3621, 62.044, 57.2224, 61.6453
from mpmath import mp
mp.dps = 100
import torch
# Take some list of values that shrink to be really small in log space:
torch_lps = torch.log_softmax(-torch.arange(20.0), dim=0)
mpmath_lps = -torch.arange(20.0)
Z = sum([mp.exp(mp.mpf(mpmath_lps[i].item())) for i in range(len(mpmath_lps))])
@sjmielke
sjmielke / nuclearnorm.py
Created March 1, 2018 19:18
Nuclear norm + gradient in PyTorch
import numpy as np
import torch
x = torch.rand(10, 5).double()
x /= torch.norm(x)
def grad(x):
# Alternatively: https://math.stackexchange.com/a/934443
# from scipy.linalg import sqrtm
# return sqrtm(np.linalg.inv(x @ x.transpose())) @ x
@sjmielke
sjmielke / sgld.py
Last active December 15, 2017 19:14
SGLD
import matplotlib.pyplot as plt
import numpy as np
import torch
a = -.1
b = -.3
c = 1.0
d = 1.5
def p(x):
import math
import numpy as np
import sys
"""
Output a 2D gaussian:
░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░
░░░░░░░░▒▒▒▒▒▒▒▒░░░░░░░░░░
@sjmielke
sjmielke / traintagger.py
Created May 3, 2017 12:54
spaCy tagger training desperation
from random import shuffle
from spacy.vocab import Vocab
from spacy.tagger import Tagger
from spacy.tokens import Doc
from spacy.gold import GoldParse
from spacy.en.tag_map import TAG_MAP
def read_postagged(fn):
@sjmielke
sjmielke / opusify.py
Created August 31, 2015 07:44
opusify
#! /usr/bin/env python3
import os
import sys
import shutil
import subprocess
import mutagen.flac
import mutagen.mp3
import mutagen.mp4
import mutagen.oggopus