Skip to content

Instantly share code, notes, and snippets.

@bartvm
Last active May 13, 2016 17:30
Show Gist options
  • Save bartvm/9d782bccd324d5bdb632a2a85db5af72 to your computer and use it in GitHub Desktop.
Save bartvm/9d782bccd324d5bdb632a2a85db5af72 to your computer and use it in GitHub Desktop.
import theano
from theano import tensor
import numpy as np
def main():
x = tensor.vector('x')
y = tensor.alloc(np.float32(0), *x.shape)
def step(x_elem, t, prev_y):
new_y = tensor.set_subtensor(prev_y[t], x_elem)
return t + 1, new_y
(_, ys), _ = theano.scan(fn=step, outputs_info=[tensor.constant(0), y], sequences=[x])
copy_of_x = ys[-1]
x_val = np.random.rand(10).astype(np.float32)
f = theano.function([x], copy_of_x)
np.testing.assert_allclose(f(x_val), x_val)
np.testing.assert_allclose(f(x_val), x_val)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment