Skip to content

Instantly share code, notes, and snippets.

@hans
Created March 21, 2015 16:24
Show Gist options
  • Save hans/c698e61b6715f4d51e5e to your computer and use it in GitHub Desktop.
Save hans/c698e61b6715f4d51e5e to your computer and use it in GitHub Desktop.
Example using theano.scan to output a variable-length sequence. (Useful for e.g. generator RNNs.)
import numpy as np
import theano
import theano.tensor as T
x = T.bscalar('x')
def scan_f(x):
# Second argument "until" establishes a stop condition
return x + 1, theano.scan_module.until(T.eq(x, 5))
values, _ = theano.scan(scan_f, outputs_info=x, n_steps=1024)
f = theano.function([x], values)
print f(0)
# $ python scan_variable.py
# [1 2 3 4 5 6]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment