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. Ifspeed is important, it is probably better to pad to a densetensor.

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

  1. >>> import theano.typed_list
  2. >>> tl = theano.typed_list.TypedListType(theano.tensor.fvector)()
  3. >>> v = theano.tensor.fvector()
  4. >>> o = theano.typed_list.append(tl, v)
  5. >>> f = theano.function([tl, v], o)
  6. >>> f([[1, 2, 3], [4, 5]], [2])
  7. [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 ofTypedList, so you can only use it as non_sequences (not in sequences oras outputs):

  1. >>> import theano.typed_list
  2. >>> a = theano.typed_list.TypedListType(theano.tensor.fvector)()
  3. >>> l = theano.typed_list.length(a)
  4. >>> s, _ = theano.scan(fn=lambda i, tl: tl[i].sum(),
  5. ... non_sequences=[a],
  6. ... sequences=[theano.tensor.arange(l, dtype='int64')])
  7. >>> f = theano.function([a], s)
  8. >>> f([[1, 2, 3], [4, 5]])
  9. array([ 6., 9.], dtype=float32)
  • class theano.typedlist.basic.TypedListConstant(_type, data, name=None)[source]
  • Subclass to add the typed list operators to the basic Variable class.
  • class theano.typedlist.basic.TypedListVariable(_type, owner=None, index=None, name=None)[source]
  • Subclass to add the typed list operators to the basic Variable class.
  • theano.typedlist.basic.append = _[source]
  • Append an element at the end of another list.

Parameters:

  • x – The base typed list.
  • y – The element to append to x.

  • theano.typedlist.basic.count = _[source]
  • 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.

Notes

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

  • theano.typedlist.basic.extend = _[source]
  • 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.typedlist.basic.getitem = _[source]
  • Get specified slice of a typed list.

Parameters:

  • x – Typed list.
  • index – The index of the value to return from x.

  • theano.typedlist.basic.insert = _[source]
  • 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.typedlist.basic.length = _[source]
  • Returns the size of a list.

Parameters:x – Typed list.

  • theano.typedlist.basic.make_list = _[source]
  • Build a Python list from those Theano variable.

Parameters:a (tuple/list of Theano variable) –

Notes

All Theano variables must have the same type.

  • theano.typedlist.basic.remove = _[source]
  • 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.

Notes

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

  • theano.typedlist.basic.reverse = _[source]
  • Reverse the order of a typed list.

Parameters:x – The typed list to be reversed.