Skip to content

Instantly share code, notes, and snippets.

@mrocklin
mrocklin / pickle-example.py
Created May 21, 2012 18:57
An example that shows how to pickle a Theano Env object. All callables must be removed first. We then demonstrate that this stripped down Env may still be compiled into a callable function.
import theano
import theano.tensor as T
import pickle
import numpy as np
# Create a simple example
x = T.matrix('x')
y = x + x
env = theano.Env([x], [y])
@mrocklin
mrocklin / gist:2871821
Created June 5, 2012 01:10
A quick function to compute the shapes of all variables in a Theano Env
def shape_of_variables(env, input_shapes):
"""
Inputs:
env - the theano.Env in question
input_shapes - a dict mapping input to shape
Outputs:
shapes - a dict mapping variable to shape
WARNING : This modifies the env! Not pure!
@mrocklin
mrocklin / dot.py
Created August 12, 2012 14:49
A small program to look through a Python source directory, pull out the class hierarchy, and generate a .dot file. It also produces a .pdf file using the dot program.
#!/usr/bin/python
import re
import networkx as nx
import os
prog = re.compile("class (?P<subclass>\w+)\((?P<superclasses>[\s\w,]+)\):")
def grep_stream(location):
return os.popen(
@mrocklin
mrocklin / kalman.py
Created September 11, 2012 17:45
The Kalman filter represented in Theano syntax
import theano
import theano.sandbox.linalg as linalg
mu = theano.tensor.matrix('mu')
Sigma = theano.tensor.matrix('Sigma')
H = theano.tensor.matrix('H')
R = theano.tensor.matrix('R')
data = theano.tensor.matrix('data')
dot = theano.tensor.dot
@mrocklin
mrocklin / kalman_mpi.py
Created September 19, 2012 15:49
The computation of the Kalman filter over two computers using MPI-enabled Theano.
import theano
from theano.tensor.io import send, recv, mpi_cmps
import theano.sandbox.linalg as linalg
from theano.gof.sched import sort_schedule_fn
from time import time
dot = theano.tensor.dot
dtype = 'float32'
n = 500
run = False
@mrocklin
mrocklin / time_cudaMemcpyAsync.cu
Created October 1, 2012 18:03
A quick CUDA program to time the effectiveness of using asynchronous CPU-GPU memory transfers.
#include <stdio.h>
#include <sys/time.h>
const int n = 16000000;
// Print number of milliseconds between timevals
void printDuration(timeval a, timeval b, char* message)
{
double elapsedTime = (b.tv_sec - a.tv_sec) * 1000.0;
elapsedTime += (b.tv_usec - a.tv_usec) / 1000.0;
@mrocklin
mrocklin / TR_SY_MV.f90
Created October 24, 2012 14:46
Demonstrates the difference between TR and SY prefixed BLAS calls.
! Answers discussion in the following SO question
! http://stackoverflow.com/questions/4637888/where-can-i-find-blas-example-code-in-fortran/
! Author: Matthew Rocklin
! Disclaimer: This is my first Fortran program. Please do not judge.
implicit none
integer, parameter :: N=2
@mrocklin
mrocklin / cauchy_characteristic
Created December 2, 2012 02:44
An example using SymPy.stats to compute the characteristic function of a general Cauchy random variable. This is run in isympy (hence the pretty printing).
In [1]: from sympy.stats import *
In [2]: x0 = Symbol('x0', real=True, bounded=True)
In [3]: gamma = Symbol('gamma', positive=True)
In [4]: X = Cauchy('X', x0, gamma)
In [5]: density(X)(x)
Out[5]:
@mrocklin
mrocklin / t-characteristic
Created December 2, 2012 02:52
An example using SymPy.stats to compute the characteristic function of a general Cauchy random variable. This is run in isympy (hence the pretty printing)
In [1]: from sympy.stats import *
In [2]: nu = Symbol("nu", positive=True)
In [3]: X = StudentT("x", nu)
In [4]: density(X)(x)
Out[4]:
ν 1
- ─ - ─
@mrocklin
mrocklin / func_plot.py
Created December 26, 2012 19:58 — forked from anonymous/func_plot.py
A quick example of plotting real valued functions. This example highlights the use of functions as first class objects and higher order functions.
from numpy import linspace, sin
from pylab import plot, figure, show
def plotFunction(f):
""" Plot a function on the domain x in (-10, 10) """
xs = linspace(-10, 10, 100)
ys = map(f, xs)
plot(xs, ys)
def derivative(f, dx=.00001):