提交 19f4a625 authored 作者: Ramana.S's avatar Ramana.S

Made the final changes

上级 f380577a
...@@ -67,8 +67,8 @@ possibilities you may encounter or need. For that refer to ...@@ -67,8 +67,8 @@ possibilities you may encounter or need. For that refer to
#itypes and otypes attributes are #itypes and otypes attributes are
#compulsory if make_node method is not defined. #compulsory if make_node method is not defined.
#They're the type of input and output respectively #They're the type of input and output respectively
itypes = [] itypes = None
otypes = [] otypes = None
#Compulsory if itypes and otypes are not defined #Compulsory if itypes and otypes are not defined
def make_node(self, *inputs): def make_node(self, *inputs):
...@@ -265,7 +265,10 @@ Op Example ...@@ -265,7 +265,10 @@ Op Example
import theano import theano
class DoubleOp(theano.Op): #Using make_node
class DoubleOp1(theano.Op):
__props__ = () __props__ = ()
def make_node(self, x): def make_node(self, x):
...@@ -297,11 +300,10 @@ Op Example ...@@ -297,11 +300,10 @@ Op Example
return eval_points return eval_points
return self.grad(inputs, eval_points) return self.grad(inputs, eval_points)
.. testcode:: example (using itypes and otypes) #Using itypes and otypes
import theano
class DoubleOp(theano.Op): class DoubleOp2(theano.Op):
__props__ = () __props__ = ()
itypes = [theano.tensor.dmatrix] itypes = [theano.tensor.dmatrix]
...@@ -329,10 +331,41 @@ Op Example ...@@ -329,10 +331,41 @@ Op Example
You can try it as follows: You can try it as follows:
.. testcode:: example .. testcode:: example(Using make_node)
x = theano.tensor.matrix()
f = theano.function([x], DoubleOp1()(x))
import numpy
inp = numpy.random.rand(5, 4)
out = f(inp)
assert numpy.allclose(inp * 2, out)
print(inp)
print(out)
.. testoutput:: example
:hide:
:options: +ELLIPSIS
...
...
.. code-block:: none
[[ 0.08257206 0.34308357 0.5288043 0.06582951]
[ 0.65977826 0.10040307 0.5402353 0.55472296]
[ 0.82358552 0.29502171 0.97387481 0.0080757 ]
[ 0.77327215 0.65401857 0.76562992 0.94145702]
[ 0.8452076 0.30500101 0.88430501 0.95818655]]
[[ 0.16514411 0.68616713 1.0576086 0.13165902]
[ 1.31955651 0.20080613 1.08047061 1.10944593]
[ 1.64717104 0.59004341 1.94774962 0.0161514 ]
[ 1.5465443 1.30803715 1.53125983 1.88291403]
[ 1.6904152 0.61000201 1.76861002 1.9163731 ]]
.. testcode:: example (Using itypes and otypes)
x = theano.tensor.matrix() x = theano.tensor.matrix()
f = theano.function([x], DoubleOp()(x)) f = theano.function([x], DoubleOp2()(x))
import numpy import numpy
inp = numpy.random.rand(5, 4) inp = numpy.random.rand(5, 4)
out = f(inp) out = f(inp)
...@@ -340,6 +373,7 @@ You can try it as follows: ...@@ -340,6 +373,7 @@ You can try it as follows:
print(inp) print(inp)
print(out) print(out)
.. testoutput:: example .. testoutput:: example
:hide: :hide:
:options: +ELLIPSIS :options: +ELLIPSIS
......
...@@ -521,12 +521,6 @@ class PureOp(object): ...@@ -521,12 +521,6 @@ class PureOp(object):
Required: return an Apply instance representing the Required: return an Apply instance representing the
application of this Op to the provided inputs. application of this Op to the provided inputs.
All subclasses should over-ride this function.
Raises
------
MethodNotDefined : the subclass does not override this method.
""" """
raise utils.MethodNotDefined( raise utils.MethodNotDefined(
"make_node", type(self), self.__class__.__name__) "make_node", type(self), self.__class__.__name__)
...@@ -964,10 +958,12 @@ class Op(utils.object2, PureOp, CLinkerOp): ...@@ -964,10 +958,12 @@ class Op(utils.object2, PureOp, CLinkerOp):
def make_node(self, *inputs): def make_node(self, *inputs):
if not hasattr(self, 'itypes'): if not hasattr(self, 'itypes'):
raise NotImplementedError("itypes not defined") raise NotImplementedError("You can either define itypes and otypes,\
or implement make_node")
if not hasattr(self, 'otypes'): if not hasattr(self, 'otypes'):
raise NotImplementedError("otypes not defined") raise NotImplementedError("You can either define itypes and otypes,\
or implement make_node")
if len(inputs) != len(self.itypes): if len(inputs) != len(self.itypes):
raise ValueError("We expected %d inputs but got %d." % raise ValueError("We expected %d inputs but got %d." %
...@@ -1408,9 +1404,8 @@ class COp(Op): ...@@ -1408,9 +1404,8 @@ class COp(Op):
if check_input: if check_input:
# Extract the various properties of the input and output variables # Extract the various properties of the input and output variables
variables = node.inputs + node.outputs variables = node.inputs + node.outputs
variable_names = (["INPUT_%i" % variable_names = (["INPUT_%i" % i for i in range(len(node.inputs))] +
i for i in range(len(node.inputs))] + ["OUTPUT_%i" % ["OUTPUT_%i" % i for i in range(len(node.inputs))])
i for i in range(len(node.inputs))])
# Generate dtype macros # Generate dtype macros
for i, v in enumerate(variables): for i, v in enumerate(variables):
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论