This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class OnlineLearner(object): | |
def __init__(self, **kwargs): | |
self.last_misses = 0. | |
self.iratio = 0. | |
self.it = 1. | |
self.l = kwargs["l"] | |
self.max_ratio = -np.inf | |
self.threshold = 500. | |
def hinge_loss(self, vector, cls, weight): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def collapsed_likelihood(tprior, gmean, gvar, tcount): | |
p0 = sum(np.log(gamma_pdf(tp, gmean, gvar)) for tp in tprior) | |
p0 += tcount.shape[0]*(gammaln(sum(tprior))-sum(gammaln(tp) for tp in tprior)) | |
for d in xrange(tcount.shape[0]): | |
for i,t in enumerate(tcount[d]): | |
p0 += gammaln(t + tprior[i]) | |
p0 -= gammaln(sum(tcount[d]) + sum(tprior)) | |
return p0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def single_collapsed_likelihood(tprior, gmean, gvar, tcount): | |
"Assumes tprior is a scalar" | |
p0 = np.log(gamma_pdf(tprior, gmean, gvar)) | |
p0 += tcount.shape[0]*(gammaln(tprior*tcount.shape[1])-gammaln(tprior)*tcount.shape[1]) | |
for d in xrange(tcount.shape[0]): | |
for i,t in enumerate(tcount[d]): | |
p0 += gammaln(t + tprior) | |
p0 -= gammaln(sum(tcount[d]) + tprior*tcount.shape[1]) | |
return p0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def hash_collapsed_likelihood(tprior, gmean, gvar, tcount, s1): | |
"Assumes a single hyperparameter" | |
p0 = np.log(gamma_pdf(tprior, gmean, gvar)) | |
s0 = len(tcount) | |
p0 += s0*(gammaln(tprior*s1)-sum(gammaln(tprior) for i in xrange(s1))) | |
for d in xrange(s0): | |
for i,t in tcount[d].items(): | |
p0 += gammaln(t + tprior) | |
p0 += (s1-len(tcount[d]))*gammaln(tprior) | |
p0 -= gammaln(sum(tcount[d]) + s1*tprior) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def slice_sample(likelihood, x0): | |
old_lik = likelihood(x0) | |
old_alpha = x0 | |
lnt = old_lik - np.random.exponential(1) | |
w = old_alpha/32. | |
L = max(0, old_alpha - w*random.random()) | |
R = L + w | |
K = 4 | |
while K > 0 and (lnt < likelihood(L) or lnt < likelihood(R)): | |
V = random.random() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import random | |
import math | |
from scipy.special import gamma,gammaln | |
from scipy import weave | |
import sys | |
import collections | |
from libbayes import discrete, gamma_pdf, slice_sample, exp_pdf | |
from libbayes import single_collapsed_likelihood as scl | |
from toputils import get_words |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
def symdirichlet(alpha, n): | |
v = np.zeros(n)+alpha | |
return np.random.dirichlet(v) | |
import re | |
wre = re.compile(r"(\w)+") | |
def get_words(text, stop=True): | |
"A simple tokenizer" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
def symdirichlet(alpha, n): | |
v = np.zeros(n)+alpha | |
return np.random.dirichlet(v) | |
def exp_digamma(x): | |
if x < 0.1: | |
return x/100 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
import theano | |
import theano.tensor as T | |
import theano.sparse | |
import numpy as np | |
class Arow(object): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np, numpy.linalg as linalg | |
def fast_svd(M, k): | |
p = k+5 | |
Y = np.dot(M, np.random.normal(size=(M.shape[1],p))) | |
Q,r = linalg.qr(Y) | |
B = np.dot(Q.T,M) | |
Uhat, s, v = linalg.svd(B, full_matrices=False) | |
U = np.dot(Q, Uhat) | |
return U.T[:k].T, s[:k], v[:k] |
OlderNewer