Skip to content

Instantly share code, notes, and snippets.

View cdfox's full-sized avatar

Christopher Fox cdfox

View GitHub Profile
@cdfox
cdfox / reproduce_roblox_distilbert.py
Last active August 9, 2020 22:37
Reproducing Roblox DistilBERT Medium Post
# Reproducing Roblox DistilBERT Medium Post
# https://blog.roblox.com/2020/05/scaled-bert-serve-1-billion-daily-requests-cpus/
#
# 1. Launch C5 12xlarge with Deep Learning AMI (Ubuntu 18.04) Version 32.0 (ami-0dc2264cd927ca9eb)
# 2. pip install transformers[torch]
# 3. python reproduce_roblox_distilbert.py
import timeit
from transformers import DistilBertTokenizerFast, \
DistilBertForSequenceClassification
class foo:
def __eq__(self, o):
return False
t = (foo(),)
t2 = (t[0],)
t == t2 # True
t[0] == t2[0] # False
t[0] is t2[0] # True
package main
import (
"fmt"
"log"
"net/http"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
@cdfox
cdfox / numba_lda.py
Last active August 29, 2015 14:12
Gibbs sampling for latent Dirichlet allocation using Numba.
import re
import sys
import numpy as np
import numba
file_name = sys.argv[1] # one document per line
num_topics = int(sys.argv[2])
num_iterations = int(sys.argv[3])
alpha = float(sys.argv[4])
alpha0 = float(sys.argv[5])
import math
def sqrt(x, high, low, e):
mid = (high + low) / 2.0
square = mid * mid
if (square >= x and square - x <= e) or (square < x and x - square <= e):
return mid
elif square > x:
return sqrt(x, high, mid, e)
else:
@cdfox
cdfox / gist:6857081
Created October 6, 2013 18:00
Counting the possible orderings of distinct elements [a,b,c,d,e,f] given that a < d, b < e, c < b, c < f, d < f, d < e.
from itertools import permutations
perms = permutations(range(6))
def check(p):
i1 = p.index(0) < p.index(3)
i2 = p.index(1) < p.index(4)
i3 = p.index(2) < p.index(1)
i4 = p.index(2) < p.index(5)
i5 = p.index(3) < p.index(4)
@cdfox
cdfox / preprocess.go
Last active December 15, 2015 21:29
Preprocess a set of documents, one on each line of the input file, for LDA inference.
// For each line of the input file, remove nonalphanumeric characters,
// lowercase all letters, remove stopwords, and write the result to the output
// file. I used the answer here as a template for reading/writing files:
// http://stackoverflow.com/questions/1821811/how-to-read-write-from-to-file/9739903#9739903
package main
import (
"bufio"
"fmt"
sentences = [s for rev in reviews for s in rev.split(".")]
@cdfox
cdfox / gist:2830517
Created May 29, 2012 20:30
hello world
print 'hello world'
@cdfox
cdfox / gist:2560930
Created April 30, 2012 18:39
Using the Requests library
import requests
import codecs
url = 'http://www.instapaper.com/m'
params = {'u': 'http://techcrunch.com/2012/04/30/bn-8-k-microsoft-paying-180m-advance-on-nook-for-windows-8-125m-for-content-tech-acquisition/'}
r = requests.get(url, params=params)
# r.text holds the response from the server. It appears to be unicode in this case.
f = codecs.open("foo.html", mode="w", encoding="UTF-8")
f.write(r.text)