提交 e8ecd0fc authored 作者: abergeron's avatar abergeron

Merge pull request #3296 from harlouci/numpydoc_typedList_scalar

Numpydoc typed list scalar
差异被折叠。
...@@ -87,14 +87,18 @@ erfc = Erfc(upgrade_to_float_no_complex, name='erfc') ...@@ -87,14 +87,18 @@ erfc = Erfc(upgrade_to_float_no_complex, name='erfc')
class Erfcx(UnaryScalarOp): class Erfcx(UnaryScalarOp):
""" """
Implements the scaled complementary error function exp(x**2)*erfc(x) in a numerically stable way for large x. This Implements the scaled complementary error function exp(x**2)*erfc(x) in a
is useful for calculating things like log(erfc(x)) = log(erfcx(x)) - x ** 2 without causing underflow. Should only numerically stable way for large x. This is useful for calculating things
be used if x is known to be large and positive, as using erfcx(x) for large negative x may instead introduce like log(erfc(x)) = log(erfcx(x)) - x ** 2 without causing underflow.
overflow problems. Should only be used if x is known to be large and positive, as using
erfcx(x) for large negative x may instead introduce overflow problems.
Note: This op can still be executed on GPU, despite not having c_code. When
Notes
-----
This op can still be executed on GPU, despite not having c_code. When
running on GPU, sandbox.cuda.opt.local_gpu_elemwise_[0,1] replaces this op running on GPU, sandbox.cuda.opt.local_gpu_elemwise_[0,1] replaces this op
with sandbox.cuda.elemwise.ErfcxGPU. with sandbox.cuda.elemwise.ErfcxGPU.
""" """
def impl(self, x): def impl(self, x):
if imported_scipy_special: if imported_scipy_special:
...@@ -124,7 +128,9 @@ class Erfinv(UnaryScalarOp): ...@@ -124,7 +128,9 @@ class Erfinv(UnaryScalarOp):
""" """
Implements the inverse error function. Implements the inverse error function.
Note: This op can still be executed on GPU, despite not having c_code. When Notes
-----
This op can still be executed on GPU, despite not having c_code. When
running on GPU, sandbox.cuda.opt.local_gpu_elemwise_[0,1] replaces this op running on GPU, sandbox.cuda.opt.local_gpu_elemwise_[0,1] replaces this op
with sandbox.cuda.elemwise.ErfinvGPU. with sandbox.cuda.elemwise.ErfinvGPU.
...@@ -237,6 +243,7 @@ gamma = Gamma(upgrade_to_float, name='gamma') ...@@ -237,6 +243,7 @@ gamma = Gamma(upgrade_to_float, name='gamma')
class GammaLn(UnaryScalarOp): class GammaLn(UnaryScalarOp):
""" """
Log gamma function. Log gamma function.
""" """
@staticmethod @staticmethod
def st_impl(x): def st_impl(x):
...@@ -280,6 +287,7 @@ gammaln = GammaLn(upgrade_to_float, name='gammaln') ...@@ -280,6 +287,7 @@ gammaln = GammaLn(upgrade_to_float, name='gammaln')
class Psi(UnaryScalarOp): class Psi(UnaryScalarOp):
""" """
Derivative of log gamma function. Derivative of log gamma function.
""" """
@staticmethod @staticmethod
def st_impl(x): def st_impl(x):
...@@ -360,13 +368,13 @@ psi = Psi(upgrade_to_float, name='psi') ...@@ -360,13 +368,13 @@ psi = Psi(upgrade_to_float, name='psi')
class Chi2SF(BinaryScalarOp): class Chi2SF(BinaryScalarOp):
""" """
Compute (1 - chi2_cdf(x)) Compute (1 - chi2_cdf(x)) ie. chi2 pvalue (chi2 'survival function').
ie. chi2 pvalue (chi2 'survival function')
C code is provided in the Theano_lgpl repository. C code is provided in the Theano_lgpl repository.
This make it faster. This make it faster.
https://github.com/Theano/Theano_lgpl.git https://github.com/Theano/Theano_lgpl.git
""" """
@staticmethod @staticmethod
......
...@@ -28,8 +28,11 @@ def theano_dtype(expr): ...@@ -28,8 +28,11 @@ def theano_dtype(expr):
class SymPyCCode(ScalarOp): class SymPyCCode(ScalarOp):
""" An Operator that wraps SymPy's C code generation """
An Operator that wraps SymPy's C code generation.
Examples
--------
>>> from sympy.abc import x, y # SymPy Variables >>> from sympy.abc import x, y # SymPy Variables
>>> from theano.scalar.basic_sympy import SymPyCCode >>> from theano.scalar.basic_sympy import SymPyCCode
>>> op = SymPyCCode([x, y], x + y) >>> op = SymPyCCode([x, y], x + y)
...@@ -42,6 +45,7 @@ class SymPyCCode(ScalarOp): ...@@ -42,6 +45,7 @@ class SymPyCCode(ScalarOp):
>>> f = theano.function([xt, yt], zt) >>> f = theano.function([xt, yt], zt)
>>> f(1.0, 2.0) >>> f(1.0, 2.0)
3.0 3.0
""" """
def __init__(self, inputs, expr, name=None): def __init__(self, inputs, expr, name=None):
......
"""A shared variable container for true scalars - for internal use. """
A shared variable container for true scalars - for internal use.
Why does this file exist? Why does this file exist?
------------------------- -------------------------
...@@ -37,9 +38,12 @@ class ScalarSharedVariable(_scalar_py_operators, SharedVariable): ...@@ -37,9 +38,12 @@ class ScalarSharedVariable(_scalar_py_operators, SharedVariable):
def shared(value, name=None, strict=False, allow_downcast=None): def shared(value, name=None, strict=False, allow_downcast=None):
"""SharedVariable constructor for scalar values. Default: int64 or float64. """
SharedVariable constructor for scalar values. Default: int64 or float64.
:note: We implement this using 0-d tensors for now. Notes
-----
We implement this using 0-d tensors for now.
""" """
if not isinstance(value, (numpy.number, float, int, complex)): if not isinstance(value, (numpy.number, float, int, complex)):
......
...@@ -48,6 +48,7 @@ class _typed_list_py_operators: ...@@ -48,6 +48,7 @@ class _typed_list_py_operators:
class TypedListVariable(_typed_list_py_operators, Variable): class TypedListVariable(_typed_list_py_operators, Variable):
""" """
Subclass to add the typed list operators to the basic `Variable` class. Subclass to add the typed list operators to the basic `Variable` class.
""" """
TypedListType.Variable = TypedListVariable TypedListType.Variable = TypedListVariable
...@@ -104,8 +105,13 @@ getitem = GetItem() ...@@ -104,8 +105,13 @@ getitem = GetItem()
""" """
Get specified slice of a typed list. Get specified slice of a typed list.
:param x: typed list. Parameters
:param index: the index of the value to return from `x`. ----------
x
Typed list.
index
The index of the value to return from `x`.
""" """
...@@ -174,8 +180,13 @@ append = Append() ...@@ -174,8 +180,13 @@ append = Append()
""" """
Append an element at the end of another list. Append an element at the end of another list.
:param x: the base typed list. Parameters
:param y: the element to append to `x`. ----------
x
The base typed list.
y
The element to append to `x`.
""" """
...@@ -250,8 +261,13 @@ extend = Extend() ...@@ -250,8 +261,13 @@ extend = Extend()
""" """
Append all elements of a list at the end of another list. Append all elements of a list at the end of another list.
:param x: The typed list to extend. Parameters
:param toAppend: The typed list that will be added at the end of `x`. ----------
x
The typed list to extend.
toAppend
The typed list that will be added at the end of `x`.
""" """
...@@ -325,9 +341,15 @@ insert = Insert() ...@@ -325,9 +341,15 @@ insert = Insert()
""" """
Insert an element at an index in a typed list. Insert an element at an index in a typed list.
:param x: the typed list to modify. Parameters
:param index: the index where to put the new element in `x`. ----------
:param toInsert: The new element to insert. x
The typed list to modify.
index
The index where to put the new element in `x`.
toInsert
The new element to insert.
""" """
...@@ -356,9 +378,9 @@ class Remove(Op): ...@@ -356,9 +378,9 @@ class Remove(Op):
out[0] = x out[0] = x
""" """
inelegant workaround for ValueError: The truth value of an Inelegant workaround for ValueError: The truth value of an
array with more than one element is ambiguous. Use a.any() or a.all() array with more than one element is ambiguous. Use a.any() or a.all()
being thrown when trying to remove a matrix from a matrices list being thrown when trying to remove a matrix from a matrices list.
""" """
for y in range(out[0].__len__()): for y in range(out[0].__len__()):
if node.inputs[0].ttype.values_eq(out[0][y], toRemove): if node.inputs[0].ttype.values_eq(out[0][y], toRemove):
...@@ -371,13 +393,18 @@ class Remove(Op): ...@@ -371,13 +393,18 @@ class Remove(Op):
remove = Remove() remove = Remove()
"""Remove an element from a typed list. """Remove an element from a typed list.
:param x: the typed list to be changed. Parameters
:param toRemove: an element to be removed from the typed list. ----------
x
The typed list to be changed.
toRemove
An element to be removed from the typed list.
We only remove the first instance. We only remove the first instance.
:note: Python implementation of remove doesn't work when we want to Notes
remove an ndarray from a list. This implementation works in that -----
case. Python implementation of remove doesn't work when we want to remove an ndarray
from a list. This implementation works in that case.
""" """
...@@ -437,7 +464,11 @@ reverse = Reverse() ...@@ -437,7 +464,11 @@ reverse = Reverse()
""" """
Reverse the order of a typed list. Reverse the order of a typed list.
:param x: the typed list to be reversed. Parameters
----------
x
The typed list to be reversed.
""" """
...@@ -452,7 +483,7 @@ class Index(Op): ...@@ -452,7 +483,7 @@ class Index(Op):
def perform(self, node, inputs, outputs): def perform(self, node, inputs, outputs):
""" """
inelegant workaround for ValueError: The truth value of an Inelegant workaround for ValueError: The truth value of an
array with more than one element is ambiguous. Use a.any() or a.all() array with more than one element is ambiguous. Use a.any() or a.all()
being thrown when trying to remove a matrix from a matrices list being thrown when trying to remove a matrix from a matrices list
""" """
...@@ -480,7 +511,7 @@ class Count(Op): ...@@ -480,7 +511,7 @@ class Count(Op):
def perform(self, node, inputs, outputs): def perform(self, node, inputs, outputs):
""" """
inelegant workaround for ValueError: The truth value of an Inelegant workaround for ValueError: The truth value of an
array with more than one element is ambiguous. Use a.any() or a.all() array with more than one element is ambiguous. Use a.any() or a.all()
being thrown when trying to remove a matrix from a matrices list being thrown when trying to remove a matrix from a matrices list
""" """
...@@ -499,13 +530,18 @@ count = Count() ...@@ -499,13 +530,18 @@ count = Count()
""" """
Count the number of times an element is in the typed list. Count the number of times an element is in the typed list.
:param x: The typed list to look into. Parameters
:param elem: The element we want to count in list. ----------
x
The typed list to look into.
elem
The element we want to count in list.
The elements are compared with equals. The elements are compared with equals.
:note: Python implementation of count doesn't work when we want to Notes
count an ndarray from a list. This implementation works in that -----
case. Python implementation of count doesn't work when we want to count an ndarray
from a list. This implementation works in that case.
""" """
...@@ -543,7 +579,11 @@ length = Length() ...@@ -543,7 +579,11 @@ length = Length()
""" """
Returns the size of a list. Returns the size of a list.
:param x: typed list. Parameters
----------
x
Typed list.
""" """
...@@ -573,7 +613,12 @@ make_list = MakeList() ...@@ -573,7 +613,12 @@ make_list = MakeList()
""" """
Build a Python list from those Theano variable. Build a Python list from those Theano variable.
:param a: tuple/list of Theano variable Parameters
----------
a : tuple/list of Theano variable
Notes
-----
All Theano variables must have the same type.
:note: All Theano variable must have the same type.
""" """
...@@ -2,16 +2,20 @@ from theano import gof ...@@ -2,16 +2,20 @@ from theano import gof
class TypedListType(gof.Type): class TypedListType(gof.Type):
"""
Parameters
----------
ttype
Type of theano variable this list will contains, can be another list.
depth
Optionnal parameters, any value above 0 will create a nested list of
this depth. (0-based)
"""
def __init__(self, ttype, depth=0): def __init__(self, ttype, depth=0):
"""
:Parameters:
-'ttype' : Type of theano variable this list
will contains, can be another list.
-'depth' : Optionnal parameters, any value
above 0 will create a nested list of this
depth. (0-based)
"""
if depth < 0: if depth < 0:
raise ValueError('Please specify a depth superior or' raise ValueError('Please specify a depth superior or'
'equal to 0') 'equal to 0')
...@@ -25,10 +29,16 @@ class TypedListType(gof.Type): ...@@ -25,10 +29,16 @@ class TypedListType(gof.Type):
def filter(self, x, strict=False, allow_downcast=None): def filter(self, x, strict=False, allow_downcast=None):
""" """
:Parameters:
-'x' : value to filter Parameters
-'strict' : if true, only native python list will be accepted ----------
-'allow_downcast' : does not have any utility at the moment x
Value to filter.
strict
If true, only native python list will be accepted.
allow_downcast
Does not have any utility at the moment.
""" """
if strict: if strict:
if not isinstance(x, list): if not isinstance(x, list):
...@@ -45,9 +55,9 @@ class TypedListType(gof.Type): ...@@ -45,9 +55,9 @@ class TypedListType(gof.Type):
def __eq__(self, other): def __eq__(self, other):
""" """
two list are equals if they contains the same type. Two lists are equal if they contain the same type.
"""
"""
return type(self) == type(other) and self.ttype == other.ttype return type(self) == type(other) and self.ttype == other.ttype
def __hash__(self): def __hash__(self):
...@@ -58,8 +68,8 @@ class TypedListType(gof.Type): ...@@ -58,8 +68,8 @@ class TypedListType(gof.Type):
def get_depth(self): def get_depth(self):
""" """
utilitary function to get the 0 based Utilitary function to get the 0 based level of the list.
level of the list
""" """
if isinstance(self.ttype, TypedListType): if isinstance(self.ttype, TypedListType):
return self.ttype.get_depth() + 1 return self.ttype.get_depth() + 1
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论