Previous topic

sandbox.rng_mrg – MRG random number generator

Next topic

Optimizations

This Page

typed_list – Typed List

Note

This has been added in release 0.7.

Note

This works, but is not well integrated with the rest of Theano. If speed is important, it is probably better to pad to a dense tensor.

This is a type that represents a list in Theano. All elements must have the same Theano type. Here is an example:

import theano.typed_list

tl = theano.typed_list.TypedListType(theano.tensor.fvector)()
v = theano.tensor.fvector()
o = theano.typed_list.append(tl, v)
f = theano.function([tl, v], o)
print f([[1, 2, 3], [4, 5]], [2])
#[array([ 1.,  2.,  3.], dtype=float32), array([ 4.,  5.], dtype=float32), array([ 2.], dtype=float32)]

A second example with Scan. Scan doesn’t yet have direct support of TypedList, so you can only use it as non_sequences (not in sequences or as outputs):

import theano.typed_list

a = theano.typed_list.TypedListType(theano.tensor.fvector)()
l = theano.typed_list.length(a)
s, _ = theano.scan(fn=lambda i, tl: tl[i].sum(),
                   non_sequences=[a],
                   sequences=[theano.tensor.arange(l, dtype='int64')])

f = theano.function([a], s)
f([[1, 2, 3], [4, 5]])
#array([ 6.,  9.], dtype=float32)
class theano.typed_list.basic.TypedListVariable(type, owner=None, index=None, name=None)

Subclass to add the typed list operators to the basic Variable class.

theano.typed_list.basic.append = <theano.typed_list.basic.Append object at 0x7f62e51241d0>

Append an element at the end of another list.

Parameters:
  • x – the base typed list.
  • y – the element to append to x.
theano.typed_list.basic.count = <theano.typed_list.basic.Count object at 0x7f62e51245d0>

Count the number of times an element is in the typed list.

Parameters:
  • x – The typed list to look into.
  • elem – The element we want to count in list. The elements are compared with equals.
Note:

Python implementation of count doesn’t work when we want to count an ndarray from a list. This implementation works in that case.

theano.typed_list.basic.extend = <theano.typed_list.basic.Extend object at 0x7f62e5124290>

Append all elements of a list at the end of another list.

Parameters:
  • x – The typed list to extend.
  • toAppend – The typed list that will be added at the end of x.
theano.typed_list.basic.getitem = <theano.typed_list.basic.GetItem object at 0x7f62e5124110>

Get specified slice of a typed list.

Parameters:
  • x – typed list.
  • index – the index of the value to return from x.
theano.typed_list.basic.insert = <theano.typed_list.basic.Insert object at 0x7f62e5124350>

Insert an element at an index in a typed list.

Parameters:
  • x – the typed list to modify.
  • index – the index where to put the new element in x.
  • toInsert – The new element to insert.
theano.typed_list.basic.length = <theano.typed_list.basic.Length object at 0x7f62e5124650>

Returns the size of a list.

Parameters:x – typed list.
theano.typed_list.basic.make_list = <theano.typed_list.basic.MakeList object at 0x7f62e51246d0>

Build a Python list from those Theano variable.

Parameters:a – tuple/list of Theano variable
Note:All Theano variable must have the same type.
theano.typed_list.basic.remove = <theano.typed_list.basic.Remove object at 0x7f62e5124410>

Remove an element from a typed list.

Parameters:
  • x – the typed list to be changed.
  • toRemove – an element to be removed from the typed list. We only remove the first instance.
Note:

Python implementation of remove doesn’t work when we want to remove an ndarray from a list. This implementation works in that case.

theano.typed_list.basic.reverse = <theano.typed_list.basic.Reverse object at 0x7f62e51244d0>

Reverse the order of a typed list.

Parameters:x – the typed list to be reversed.