提交 77c55988 authored 作者: James Bergstra's avatar James Bergstra

cleaning Apply docstrings

上级 aac0c7bb
...@@ -8,19 +8,26 @@ import utils ...@@ -8,19 +8,26 @@ import utils
class Apply(utils.object2): class Apply(utils.object2):
""" """
Represents the application of an Op on input Results, producing output Represents the application of an Op on input Results, producing output
Results. These should be instantiated by an Op's make_node function. Results.
This class should be instantiated by an Op's make_node function.
""" """
#__slots__ = ['op', 'inputs', 'outputs']
def __init__(self, op, inputs, outputs): def __init__(self, op, inputs, outputs):
""" """
Sets self.op, self.inputs, self.outputs to the respective parameter Initialize self.{op, inputs, outputs}
in the arguments list.
@param op: initialize self.op
@param inputs: initialize self.inputs
@param outputs: initialize self.outputs
@type op: Op instance
@type inputs: list of Result instances
@type outputs: list of Result instances
The owner field of each output in the outputs list will be set to @note: The owner field of each output in the outputs list will be set to self.
self.
Note: it is illegal for an output element to have an owner that is @note: If an output element has an owner that is neither None nor self, then a
not None, unless it already points to self. ValueError exception will be raised.
""" """
self.op = op self.op = op
self.inputs = [] self.inputs = []
...@@ -44,12 +51,17 @@ class Apply(utils.object2): ...@@ -44,12 +51,17 @@ class Apply(utils.object2):
self.outputs.append(output) self.outputs.append(output)
else: else:
raise TypeError("The 'outputs' argument to Apply must contain Result instances with no owner, not %s" % output) raise TypeError("The 'outputs' argument to Apply must contain Result instances with no owner, not %s" % output)
def default_output(self): def default_output(self):
"""Returns the default output for this node.
@rtype: Result instance
@return: an element of self.outputs, typically self.outputs[0].
@note: may raise AttributeError self.op.default_output is out of range, or if there are
multiple outputs and self.op.default_output does not exist.
""" """
Returns the default output for this node. If there is only one
output, it will be returned. Else, it will consult the value of
node.op.default_output to decide which output to return.
"""
do = getattr(self.op, 'default_output', None) do = getattr(self.op, 'default_output', None)
if do is None: if do is None:
if len(self.outputs) == 1: if len(self.outputs) == 1:
...@@ -59,32 +71,42 @@ class Apply(utils.object2): ...@@ -59,32 +71,42 @@ class Apply(utils.object2):
elif do < 0 or do >= len(self.outputs): elif do < 0 or do >= len(self.outputs):
raise AttributeError("%s.default_output is out of range." % self.op) raise AttributeError("%s.default_output is out of range." % self.op)
return self.outputs[do] return self.outputs[do]
out = property(default_output, out = property(default_output,
doc = "same as self.default_output()") doc = "alias for self.default_output()")
def __str__(self): def __str__(self):
return op_as_string(self.inputs, self) return op_as_string(self.inputs, self)
def __repr__(self): def __repr__(self):
return str(self) return str(self)
def __asapply__(self): def __asapply__(self):
return self return self
def clone(self): def clone(self):
# cp = copy(self) """Duplicate this Apply instance with inputs = self.inputs.
# cp.outputs = [output.clone() for output in self.outputs]
# for output in cp.outputs: @return: a new Apply instance (or subclass instance) with new outputs.
# output.owner = cp
# return cp @note: tags are copied from self to the returned instance.
"""
cp = self.__class__(self.op, self.inputs, [output.clone() for output in self.outputs]) cp = self.__class__(self.op, self.inputs, [output.clone() for output in self.outputs])
cp.tag = copy(self.tag) cp.tag = copy(self.tag)
return cp return cp
def clone_with_new_inputs(self, inputs, strict = True): def clone_with_new_inputs(self, inputs, strict = True):
""" """Duplicate this Apply instance in a new graph.
Returns an Apply node with the same op but different inputs. Unless
strict is False, the type fields of all the inputs must be @param inputs: list of Result instances to use as inputs.
equal to the current ones.
@type strict: Bool
@param strict: If True, the type fields of all the inputs must be equal to the current
ones, and returned outputs are guaranteed to have the same types as self.outputs.
If False, then there's no guarantee that the clone's outputs will have the same types
as self.outputs, and cloning may not even be possible (it depends on the Op).
If strict is True, the outputs of the clone will have the same type as @returns: an Apply instance with the same op but different outputs.
the outputs of self. Else, it depends on the types of the new inputs
and the behavior of the op wrt that.
""" """
# if check_type: # if check_type:
# for curr, new in zip(self.inputs, inputs): # for curr, new in zip(self.inputs, inputs):
...@@ -108,6 +130,7 @@ class Apply(utils.object2): ...@@ -108,6 +130,7 @@ class Apply(utils.object2):
new_node.inputs = inputs new_node.inputs = inputs
return new_node return new_node
#convenience properties
nin = property(lambda self: len(self.inputs), doc = 'same as len(self.inputs)') nin = property(lambda self: len(self.inputs), doc = 'same as len(self.inputs)')
nout = property(lambda self: len(self.outputs), doc = 'same as len(self.outputs)') nout = property(lambda self: len(self.outputs), doc = 'same as len(self.outputs)')
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论