Skip to content

Instantly share code, notes, and snippets.

View tansey's full-sized avatar

Wesley Tansey tansey

View GitHub Profile
@tansey
tansey / chol_unpack.py
Created February 10, 2017 21:48
Unpack a lower triangular Cholesky matrix from a neural network output in Tensorflow
import tensorflow as tf
def unpack_cholesky(q, ndims):
# Build the lower-triangular Cholesky from the flat buffer (assumes q shape is [batchsize, cholsize])
chol_diag = tf.nn.softplus(q[:,:ndims])
chol_offdiag = q[:,ndims:]
chol_rows = []
chol_start = 0
chol_end = 1
for i in xrange(ndims):
@tansey
tansey / wtfset.cpp
Created November 9, 2016 02:57
terrible STL set iterator behavior
#include <set>
#include <iostream>
class A
{
std::set<int> s;
public:
A() { s.insert(0); s.insert(1); }
std::set<int> getSet() { return s; }
@tansey
tansey / admm_convergence.py
Created July 31, 2015 22:57
ADMM convergence checker in CVXPY
'''
Implementation of the ADMM convergence rate SDP from Nishihara et al.,
ICML 2015, equation 11.
Code by Wesley Tansey and Sanmi Koyejo
7/31/2015
'''
import cvxpy as cvx
import numpy as np
@tansey
tansey / gist:5b59d73c58587973844d
Created September 24, 2014 15:55
Python-style string formatting in Julia with floating point support
function format(s, args...)
# Python-style string formatting with floating point support
# Note that this is 1-based to be more Julian
result = deepcopy(s)
for (i, x) in enumerate(args)
q = Regex("{$i(:\.([0-9])+f)?}")
next = result
for m in eachmatch(q, result)
val = x
if m.captures[2] != nothing
@tansey
tansey / gist:9753126
Created March 25, 2014 00:54
Numpy Array vs. Numpy Masked Array -- Madness
import numpy as np
import numpy.ma as ma
# Create a 3x3 array in regular numpy
a = np.arange(9).reshape((3,3))
# Get the middle row
b = a[1]
# Change the middle value in the middle row
@tansey
tansey / plot_with_bands.py
Last active January 3, 2016 17:38
Plot means with confidence (stderr or other metric) bands in a pretty format using matplotlib.
def plot_with_bands(graph_title, means, bands, series, xvals=None, xlabel=None, ylabel=None, subtitle=None, filename='results.pdf'):
colors = ['blue','red','green', 'black', 'yellow', 'orange', 'purple', 'brown'] # max 8 lines
print 'means: {0} bands: {1}'.format(means.shape, bands.shape)
assert(means.shape == bands.shape)
assert(xvals is None or xvals.shape[0] == means.shape[1])
assert(means.shape[1] <= len(colors))
if xvals is None:
xvals = np.arange(means.shape[0])
ax = plt.subplot(111)
plt.ticklabel_format(axis='y', style='plain', useOffset=False)
@tansey
tansey / gist:6977878
Created October 14, 2013 15:55
2 parameter gibbs sampler with pretty plots
import random
import math
import matplotlib.pyplot as plt
import numpy as np
def acf(x, length=35):
return np.array([1] + [np.corrcoef(x[:-i], x[i:])[0,1] for i in range(1,length)])
# The number of samples to observe
n = 100
class A(object):
def __init__(self, myvar = {}):
self.myvar = myvar
a1 = A()
a1.myvar['foo'] = 1
a2 = A()
a2.myvar['bar'] = 2
class A(object):
def __init__(self, myvar = {}):
self.myvar = myvar
a1 = A()
a1.myvar['foo'] = 1
a2 = A()
a2.myvar['bar'] = 2
@tansey
tansey / gist:2410545
Created April 18, 2012 02:00
Training a NEAT network with backprop in SharpNEAT
double[] inputs = ...
double[] desiredOutputs = ...
// Get the neural network
var network = ((FastCyclicNetwork)blackbox;
// Perform backpropagation through time
network.Train(inputs, desiredOutputs);