Skip to content

Instantly share code, notes, and snippets.

View alextp's full-sized avatar

Alexandre Passos alextp

View GitHub Profile
@alextp
alextp / arow.py
Created January 7, 2012 23:38
arow
# a short implementation of arow in theano
def arow(params, loss, lbda1, lbda2):
sigma = [theano.shared(value=np.ones(p.value.shape)) for p in params]
gl = [T.grad(cost=loss, wrt=p) for p in params]
ups = {}
for i in xrange(len(params)):
ups[params[i]] = params[i] - lbda1*gl[i]/sigma[i]
ups[sigma[i]] = sigma[i] + lbda2*gl[i]*gl[i]
return ups
@alextp
alextp / SparseCoding.scala
Created January 26, 2012 15:56
Scala sparse coding implementation
import java.util.Random
import collection.mutable.ArrayBuffer
/**
* Created by IntelliJ IDEA.
* User: apassos
* Date: 1/26/12
* Time: 9:43 AM
* To change this template use File | Settings | File Templates.
@alextp
alextp / ClassifierPos.scala
Created January 22, 2013 01:33
jinho features style
def addFeature(v: SparseIndexedTensor1, f: String) { v.update(ClassifierPosFeatureDomain.index(f), 1.0) }
def addLemma(v: SparseIndexedTensor1, w: WordData, f: String, prefix: String) {
if (w.ambiguityClasses.contains(f)) addFeature(v, prefix+f)
}
def getAffinity(sent: SentenceData, w: WordData, pos: Int) {
val f = sent.get(sent.lemmas, pos)
if (w.ambiguityClasses.contains(f)) w.ambiguityClasses(f) else ""
}
def getLemmaFeature(sent: SentenceData, w: WordData, pos: Int, dif: Int) = {
val prefix = "W"+(dif)+"="
class StringMapCubbie[T](val m: mutable.Map[String,T]) extends Cubbie {
setMap(new mutable.Map[String, Any] {
override def update(key: String, value: Any): Unit = {
value match {
case v: T => m(key) = v
case _ => throw new InvalidClassException(value.getClass.getName + " is not of the proper type.")
}
}
def += (kv: (String, Any)): this.type = { update(kv._1, kv._2); this }
def -= (key: String): this.type = sys.error("Can't remove slots from cubbie map!")
serialize(modelFile+"-iter-"+trainer.iteration)
val other = ClassifierPos.load(modelFile+"-iter-"+trainer.iteration)
val od = other.ClassifierPosFeatureDomain.dimensionDomain
val s0 = testDocs.head.sentences.head
val sd0 = new SentenceData(s0)
val sd1 = new other.SentenceData(s0)
sd0.lemmas.zipWithIndex.foreach(l => assert(sd1.lemmas(l._2) == l._1))
val f0 = new SparseBinaryTensor1(ClassifierPosFeatureDomain.dimensionDomain.size)
val f1 = new SparseBinaryTensor1(other.ClassifierPosFeatureDomain.dimensionDomain.size)
assert(ClassifierPosFeatureDomain.dimensionDomain.size == other.ClassifierPosFeatureDomain.dimensionDomain.size)
class StringMapCubbie[T](val m: mutable.Map[String,T]) extends Cubbie {
var akeys : Seq[String] = null
var avalues: Seq[T] = null
setMap(new mutable.Map[String, Any] {
override def update(key: String, value: Any): Unit = {
if (key == "keys") {
akeys = value.asInstanceOf[Traversable[String]].toSeq
} else if (key == "values") {
assert(keys != null)
avalues = value.asInstanceOf[Traversable[T]].toSeq
@alextp
alextp / bayesopt.py
Created November 29, 2011 01:18
A trivial implementation of Bayesian optimization
# coding: utf-8
# An attempt at implementing Bayesian optimization according to
# Brochu, Cora, and de Freitas' tutorial
# http://haikufactory.com/files/bayopt.pdf
from sklearn import gaussian_process
import numpy as np
import scipy.optimize, scipy.stats as st
import argparse
import numpy as np
import tensorflow as tf
from tensorflow.python.framework.errors import FailedPreconditionError
"""Code for data dependent initialization in Weight Normalization paper:
https://arxiv.org/abs/1602.07868
"""
@alextp
alextp / fast_svd.py
Created November 4, 2010 13:12
Gunnar Martinsson's fast svd
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]
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