Skip to content

Instantly share code, notes, and snippets.

View alextp's full-sized avatar

Alexandre Passos alextp

View GitHub Profile
import numpy as np, scipy, scipy.sparse, numpy.linalg, scipy.optimize
from scipy import weave
def project_l1(lbda, sigma):
"Project positive vector lbda to have l1 norm sigma"
ll = -np.sort(-lbda)
cs = 0.
theta = 0
prevtheta = 0
@alextp
alextp / avisa.py
Created January 31, 2011 00:16
Avisa a pessoa para alongar o pescoço
#!/usr/bin/env python
# coding: utf-8
import pygtk
pygtk.require('2.0')
import gtk
import time
class Avisa(object):
def __init__(self):
open MathNet.Numerics;
open System
let a = Fn.Factorial 10
let elementwiseProduct (a: float array) (b: float array) = [| for i in 0..a.Length-1 -> a.[i]*b.[i] |]
let arraySum (a: float array) (b: float array) = [|for i in 0..a.Length-1 -> a.[i]+b.[i] |]
let scalarProduct (a: float) (b: float array) = [| for i in 0..b.Length-1 -> a*b.[i] |]
let expectation (z: float array) (v: float array array) =
let prod = [| for i in 0..v.Length-1 -> scalarProduct z.[i] v.[i] |]
let gaussLik (a: float array) (b: float array) =
Math.Exp -0.5* (dist a b)
let sampleDiscrete (p: float array) =
let d = new Distributions.ArbitraryDistribution()
d.SetDistributionParameters(0, p)
d.NextInt32()
// Samples the z variable for a single theta
@alextp
alextp / online.py
Created October 22, 2011 00:19
A hacky, old, python implementation of leon bottou's lasvm
# coding: utf-8
"""Online learning."""
import numpy as np
from numpy import sign
import itertools as it
from numpy import array as A, zeros as Z
import math
@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
@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!")