Skip to content

Instantly share code, notes, and snippets.

@benanne
Created April 9, 2012 13:23
Show Gist options
  • Select an option

  • Save benanne/2343378 to your computer and use it in GitHub Desktop.

Select an option

Save benanne/2343378 to your computer and use it in GitHub Desktop.
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
len_right = Wr.shape[3] - 1 - len_left
zeros_buffer = T.zeros((xr.shape[0], xr.shape[1], xr.shape[2], xr.shape[3] + Wr.shape[3] - 1))
xr_padded = T.set_subtensor(zeros_buffer[:, :, :, len_left:-len_right], xr)
if shape_info is None:
image_shape = None
filter_shape = None
else:
image_shape = (shape_info['num_examples'], shape_info['num_input_features'], 1, shape_info['num_timesteps'] + shape_info['width'] - 1)
filter_shape = (shape_info['num_output_features'], shape_info['num_input_features'], 1, shape_info['width'])
outr = T.nnet.conv.conv2d(xr_padded, Wr, border_mode='valid', image_shape=image_shape, filter_shape=filter_shape)
out = outr[:,:,0,:].dimshuffle(0, 2, 1)
return T.nnet.sigmoid(out + b.dimshuffle('x', 'x', 0))
# the following always fails:
num_examples = 100
num_input_features = 513
num_timesteps = 1280
width = 8
num_output_features = 200
# this works:
"""
num_examples = 100
num_input_features = 513
num_timesteps = 500
width = 8
num_output_features = 10
"""
shape_info = {
'num_examples': num_examples,
'num_input_features': num_input_features,
'num_timesteps': num_timesteps,
'width': width,
'num_output_features': num_output_features,
}
x = T.tensor3('x')
W = theano.shared(value=np.random.normal(0, 1, (num_output_features, num_input_features, width)).astype(theano.config.floatX), name='W')
b = theano.shared(value=np.random.normal(0, 1, (num_output_features,)).astype(theano.config.floatX), name='b')
feats = local_feature_extractor(x, W, b, shape_info)
print "compiling"
f = theano.function([x], feats)
print "evaluating"
v = np.random.normal(0, 1, (num_examples, num_timesteps, num_input_features)).astype(theano.config.floatX)
out = f(v) # BOOM
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment