提交 e9719c9e authored 作者: Frederic's avatar Frederic

Add documentation about typed list.

上级 d82eb54a
...@@ -22,6 +22,7 @@ Types and Ops that you can use to build and compile expression graphs. ...@@ -22,6 +22,7 @@ Types and Ops that you can use to build and compile expression graphs.
gof/index gof/index
scan scan
sandbox/index sandbox/index
typed_list
There are also some top-level imports that you might find more convenient: There are also some top-level imports that you might find more convenient:
......
.. _libdoc_typed_list:
===============================
:mod:`typed_list` -- Typed List
===============================
This is a type that represent a list in Theano. All element 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 don't have yet 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)()
s, _ = theano.scan(fn=lambda i, tl: tl[i].sum(),
non_sequences=[a],
sequences=[theano.tensor.arange(2, dtype='int64')])
f = theano.function([a], s)
f([[1, 2, 3], [4, 5]])
#array([ 6., 9.], dtype=float32)
.. automodule:: theano.typed_list.basic
:members:
...@@ -406,6 +406,7 @@ class SparseConstant(gof.Constant, _sparse_py_operators): ...@@ -406,6 +406,7 @@ class SparseConstant(gof.Constant, _sparse_py_operators):
SparseType.Variable = SparseVariable SparseType.Variable = SparseVariable
SparseType.Constant = SparseConstant SparseType.Constant = SparseConstant
# for more dtypes, call SparseType(format, dtype) # for more dtypes, call SparseType(format, dtype)
def matrix(format, name=None, dtype=None): def matrix(format, name=None, dtype=None):
if dtype is None: if dtype is None:
......
...@@ -50,9 +50,7 @@ TypedListType.Variable = TypedListVariable ...@@ -50,9 +50,7 @@ TypedListType.Variable = TypedListVariable
class GetItem(Op): class GetItem(Op):
""" # See doc in instance of this Op after the class definition.
get specified slice of a typed list
"""
def __eq__(self, other): def __eq__(self, other):
return type(self) == type(other) return type(self) == type(other)
...@@ -100,13 +98,16 @@ class GetItem(Op): ...@@ -100,13 +98,16 @@ class GetItem(Op):
return (1,) return (1,)
getitem = GetItem() getitem = GetItem()
"""
Get specified slice of a typed list..
:param x: type type list.
:param index: the index of the value to return from `x`.
"""
class Append(Op):
"""
#append an element at the end of another list
"""
class Append(Op):
# See doc in instance of this Op after the class definition.
def __init__(self, inplace=False): def __init__(self, inplace=False):
self.inplace = inplace self.inplace = inplace
if self.inplace: if self.inplace:
...@@ -159,13 +160,16 @@ class Append(Op): ...@@ -159,13 +160,16 @@ class Append(Op):
return (1,) return (1,)
append = Append() append = Append()
"""
Append an element at the end of another list.
:param x: the base typed list.
:param y: the element to append to `x`.
"""
class Extend(Op):
"""
append all element of a list at the end of another list
"""
class Extend(Op):
# See doc in instance of this Op after the class definition.
def __init__(self, inplace=False): def __init__(self, inplace=False):
self.inplace = inplace self.inplace = inplace
if self.inplace: if self.inplace:
...@@ -222,10 +226,16 @@ class Extend(Op): ...@@ -222,10 +226,16 @@ class Extend(Op):
return (1,) return (1,)
extend = Extend() extend = Extend()
"""
Append all element of a list at the end of another list.
:param x: The typed list to extend.
:param toAppend: The typed list that will be added at the end of `x`.
"""
class Insert(Op):
class Insert(Op):
# See doc in instance of this Op after the class definition.
def __init__(self, inplace=False): def __init__(self, inplace=False):
self.inplace = inplace self.inplace = inplace
if self.inplace: if self.inplace:
...@@ -283,10 +293,17 @@ class Insert(Op): ...@@ -283,10 +293,17 @@ class Insert(Op):
return (1,) return (1,)
insert = Insert() insert = Insert()
"""
Insert an element at an index in a typed list.
:param x: the typed list to modified.
:param index: the index where to put the new element in `x`.
:param toInsert: The new element to insert.
"""
class Remove(Op):
class Remove(Op):
# See doc in instance of this Op after the class definition.
def __init__(self, inplace=False): def __init__(self, inplace=False):
self.inplace = inplace self.inplace = inplace
if self.inplace: if self.inplace:
...@@ -324,10 +341,19 @@ class Remove(Op): ...@@ -324,10 +341,19 @@ class Remove(Op):
return self.__class__.__name__ return self.__class__.__name__
remove = Remove() remove = Remove()
"""
Remove an element from a typed list.
:param x: the typed list to be changed.
:param toRemove: an element to be removed from the typed list.
We remove only the first instance.
It work with ndarray.
"""
class Reverse(Op):
class Reverse(Op):
# See doc in instance of this Op after the class definition.
def __init__(self, inplace=False): def __init__(self, inplace=False):
self.inplace = inplace self.inplace = inplace
if self.inplace: if self.inplace:
...@@ -380,10 +406,15 @@ class Reverse(Op): ...@@ -380,10 +406,15 @@ class Reverse(Op):
return (1,) return (1,)
reverse = Reverse() reverse = Reverse()
"""
Reverse the order of a typed list.
:param x: the typed list to be reversed.
"""
class Index(Op):
class Index(Op):
# See doc in instance of this Op after the class definition.
def __eq__(self, other): def __eq__(self, other):
return type(self) == type(other) return type(self) == type(other)
...@@ -413,7 +444,7 @@ index_ = Index() ...@@ -413,7 +444,7 @@ index_ = Index()
class Count(Op): class Count(Op):
# See doc in instance of this Op after the class definition.
def __eq__(self, other): def __eq__(self, other):
return type(self) == type(other) return type(self) == type(other)
...@@ -441,6 +472,15 @@ class Count(Op): ...@@ -441,6 +472,15 @@ class Count(Op):
return self.__class__.__name__ return self.__class__.__name__
count = Count() count = Count()
"""
Count the number of time an element is in the typed list.
:param x: The typed list to look into.
:param elem: The element we want to count in list.
The element must evaluate to equal.
They don't need to be the same instance. Work with ndrray.
"""
class Length(Op): class Length(Op):
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论