Skip to content

Instantly share code, notes, and snippets.

# coding: utf-8
#
# @author Jonathan Raiman
# @date 9th October 2014
#
# Messing around with Stanford's GloVe words
# Download them [here](http://www-nlp.stanford.edu/projects/glove/)
import gzip, numpy as np, io
@JonathanRaiman
JonathanRaiman / Theano Gaussian Kernel
Created November 2, 2014 01:33
A Gaussian Kernel in Theano
import theano, theano.tensor as T, numpy as np
def _outer_substract(x, y):
x = x.dimshuffle(0, 1, 'x')
x = T.addbroadcast(x, 2)
return (x - y.T).T
def _gaussian_kernel(x, y, beta = 0.1):
K = _outer_substract(x,y)
return T.exp( -beta * K.norm(L=2,axis=1))
x = T.matrix()
@JonathanRaiman
JonathanRaiman / Cython Outer Product
Created November 4, 2014 03:55
Outer product in Cython
%%cython
import cython
import numpy as np
cimport numpy as np
from libc.math cimport exp
from libc.string cimport memset
from cpython cimport PyCapsule_GetPointer # PyCObject_AsVoidPtr
@JonathanRaiman
JonathanRaiman / Cython Vector Outer Product
Created November 4, 2014 05:44
Take the outer product for all pairs of vectors in two matrices (np.outer(A, B, axis = 1) essentially)
%%cython
import cython
from cython cimport view
import numpy as np
cimport numpy as np
from cpython cimport PyCapsule_GetPointer # PyCObject_AsVoidPtr
from scipy.linalg.blas import fblas
REAL = np.float32
@JonathanRaiman
JonathanRaiman / Cython & BLAS gemm
Last active August 29, 2015 14:08
Cython & BLAS gemm
# How to get gemm to work in Cython
# 1. suppose your data is Fortran contiguous (really ?)
# then blas supports this out of the box:
%%cython
cimport numpy as np
import numpy as np
from cpython cimport PyCapsule_GetPointer # PyCObject_AsVoidPtr
@JonathanRaiman
JonathanRaiman / Arrays of Cython Structs
Created December 16, 2014 06:10
Little trivial example of memory allocation and deallocation in cython with arrays, points, structs, and more...
from libc.stdlib cimport malloc, free, realloc
cdef struct Chair:
int size
cdef class Bench:
cdef Chair* chairs
cdef readonly int num_chairs
property chairs:
@JonathanRaiman
JonathanRaiman / sqlite + pickle
Created December 23, 2014 01:13
Python pickling of objects into sqlite db
import sqlite3
import pickle
protocol = 0
sqlite3.register_converter("pickle", pickle.loads)
sqlite3.register_adapter(list, pickle.dumps)
sqlite3.register_adapter(set, pickle.dumps)
@JonathanRaiman
JonathanRaiman / Lattice_Learning.py
Last active August 29, 2015 14:13
Lattice Learning
import matplotlib.pyplot as plt
import theano, theano.tensor as T, numpy as np
import theano_lstm
import os
from collections import Counter
to_softmax = lambda x: T.nnet.softmax(x)[0] if x.ndim == 1 else T.nnet.softmax(x.T).T
class LatticeModel(object):
def __init__(self,
word_size,
@JonathanRaiman
JonathanRaiman / install_python3.sh
Last active August 29, 2015 14:16
Dot Installation of Python on OS X
#!/bin/bash
# stop script on error and print it
set -e
# inform me of undefined variables
set -u
# handle cascading failures well
set -o pipefail
# install homebrew
@JonathanRaiman
JonathanRaiman / mshadow_eigen.h
Last active August 29, 2015 14:24
Mshadow using Eigen's dot product engine
#ifndef DALI_MATH_MSHADOW_EIGEN_DOT_H
#define DALI_MATH_MSHADOW_EIGEN_DOT_H
#if MSHADOW_USE_EIGEN_DOT
#include <Eigen/Eigen>
#include <mshadow/tensor.h>
#include <cblas.h>
// Eigen Backend for Dot-Product in Mshadow
// Causes Adagrad to be slower.