Skip to content

Instantly share code, notes, and snippets.

@jlowin
Created October 11, 2012 19:08
Show Gist options
  • Save jlowin/3874776 to your computer and use it in GitHub Desktop.
Save jlowin/3874776 to your computer and use it in GitHub Desktop.
Scan bug?
import numpy as np
import theano
import theano.tensor as tt
# this is the input
inp = np.arange(10).reshape(-1,1)
print 'this is the input: {}'.format(inp)
# this is the expected output
exp_out = np.zeros((10,1)); exp_out[4:] = inp[:-4]
print 'this is the expected output: {}'.format(exp_out)
def onestep(x, x_tm4):
"""
This function has two inputs and two outputs. x represents the sequence at
time t. That value is returned as the first output. The first output is
then recycled with a lag of 4 to create the second input, x_tm4. The second
input is passed through to become the second output. Getting the second
output is the objective of this exercise."""
return x, x_tm4
seq = tt.matrix()
initial_value = theano.shared(np.zeros((4,1)))
outputs_info = [{'initial' : initial_value, 'taps' : [-4]}, None]
results, updates = theano.scan(fn = onestep,
sequences = seq,
outputs_info = outputs_info)
f1 = theano.function([seq], results)
print 'Expected: the input and its lag. Returned: the input and its lag.'
print f1(inp)
f2 = theano.function([seq], results[0])
print 'Expected: just the input. Returned: just the input.'
print f2(inp)
f3 = theano.function([seq], results[1])
print 'Expected: just the lag. Returned: just the input!'
print f3(inp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment