Skip to content

Instantly share code, notes, and snippets.

@benanne
benanne / consider_constant.py
Created February 25, 2014 16:12
Theano consider constant op
import theano
import theano.tensor as T
from theano.tensor.opt import register_canonicalize
class ConsiderConstant(theano.compile.ViewOp):
def grad(self, args, g_outs):
return [T.zeros_like(g_out) for g_out in g_outs]
consider_constant = ConsiderConstant()
register_canonicalize(theano.gof.OpRemove(consider_constant), name='remove_consider_constant')
@benanne
benanne / gist:8752270
Last active April 30, 2017 14:30
buffered threaded generator
import threading
import Queue
import time
def buffered_gen(source_gen, buffer_size=2, sleep_time=1):
"""
Generator that runs a slow source generator in a separate thread.
buffer_size: the maximal number of items to pre-generate (length of the buffer)
"""
buffer = Queue.Queue(maxsize=buffer_size)
@benanne
benanne / gist:3274371
Created August 6, 2012 13:10
mel spectrograms
import numpy as np
def freq2mel(freq):
return 1127.01048 * np.log(1 + freq / 700.0)
def mel2freq(mel):
return (np.exp(mel / 1127.01048) - 1) * 700
def mel_binning_matrix(specgram_window_size, sample_frequency, num_mel_bands):
@benanne
benanne / gist:2300591
Created April 4, 2012 11:52
one hot + maximum mask in theano
import theano
import theano.tensor as T
def one_hot(t, r=None):
"""
given a tensor t of dimension d with integer values from range(r), return a
new tensor of dimension d + 1 with values 0/1, where the last dimension
gives a one-hot representation of the values in t.
@benanne
benanne / gist:ae2a7adaab133c61a059
Created January 28, 2015 13:28
Inception module in Lasagne (without 3x3s1 pooling)
import lasagne as nn
Conv2DLayer = nn.layers.Conv2DDNNLayer
def inception_module(l_in, num_1x1, reduce_3x3, num_3x3, reduce_5x5, num_5x5, gain=1.0, bias=0.1):
"""
inception module (without the 3x3s1 pooling and projection because that's difficult in Theano right now)
"""
shape = l_in.get_output_shape()
out_layers = []
@benanne
benanne / gist:2025406
Created March 12, 2012 23:30
lngamma in theano, windschitl
import numpy as np
import theano
import theano.tensor as T
def log_gamma_windschitl(z):
return 0.5 * (T.log(2*np.pi) - T.log(z) + z * (2 * T.log(z) - 2 + T.log(z * T.sinh(1/z) + 1 / (810*(z**6)))))
@benanne
benanne / gist:3087845
Created July 11, 2012 03:38
torch7 cuda compilation error
[ 96%] Building NVCC (Device) object extra/cuda/lib/THC/CMakeFiles/THC.dir//./THC_generated_THC.cu.o
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/ext/atomicity.h(48): error: identifier "__atomic_fetch_add" is undefined
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/ext/atomicity.h(52): error: identifier "__atomic_fetch_add" is undefined
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/limits(1405): error: identifier "__int128" is undefined
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/limits(1409): error: identifier "__int128" is undefined
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.0/../../../../include/c++/4.7.0/limits(1412): error: identifier "__int128" is undefined
@benanne
benanne / gist:2889661
Created June 7, 2012 16:00
Count number of users in the MSD challenge visible evaluation triplets file
s = set()
with open("kaggle_visible_evaluation_triplets.txt", 'r') as f:
for line in f:
user, _, _ = line.strip().split('\t')
s.add(user)
print "%d users" % len(s)
@benanne
benanne / gist:2343378
Created April 9, 2012 13:23
theano GPU convolution error reproduction
import theano
import theano.tensor as T
import numpy as np
def local_feature_extractor(x, W, b, shape_info=None):
xr = x.dimshuffle(0, 2, 'x', 1) # in: (num_examples, num_input_features, 1, num_timesteps)
Wr = W.dimshuffle(0, 1, 'x', 2) # filters: (num_output_features, num_input_features, 1, width)
# the output of the convolution should be equal in length to the input
len_left = (Wr.shape[3] - 1) / 2
@benanne
benanne / gist:2333153
Created April 8, 2012 00:37
theano GPU convolution error
Error freeing device pointer 0x200800000 (the launch timed out and was terminated).
!!!! error freeing device memory 0x200800000 (self=0xa64f270)
Error freeing device pointer 0x200302400 (the launch timed out and was terminated).
!!!! error freeing dev_structure memory 0x200302400 (self=0xa64f270)
---------------------------------------------------------------------------
MemoryError Traceback (most recent call last)
/usr/lib/python2.7/site-packages/IPython/utils/py3compat.pyc in execfile(fname, *where)
173 else:
174 filename = fname
--> 175 __builtin__.execfile(filename, *where)