提交 abe8581a authored 作者: Mathieu Germain's avatar Mathieu Germain

Use np.cumprod instead of np.diag in OpDecoratorTests

because as_op does not support operation that returns a view.
上级 f4268206
...@@ -302,10 +302,10 @@ You can try it as follows: ...@@ -302,10 +302,10 @@ You can try it as follows:
.. testoutput:: example .. testoutput:: example
:hide: :hide:
:options: +ELLIPSIS :options: +ELLIPSIS
... ...
... ...
.. code-block:: none .. code-block:: none
[[ 0.02443785 0.67833979 0.91954769 0.95444365] [[ 0.02443785 0.67833979 0.91954769 0.95444365]
...@@ -657,6 +657,8 @@ signature: ...@@ -657,6 +657,8 @@ signature:
It converts the Python function to a callable object that takes as It converts the Python function to a callable object that takes as
inputs Theano variables that were declared. inputs Theano variables that were declared.
.. note::
The python function wrapped by the `as_op` decorator needs to return a new data allocation, no views or in place modification of the input.
as_op Example as_op Example
------------- -------------
......
...@@ -6,7 +6,6 @@ import numpy as np ...@@ -6,7 +6,6 @@ import numpy as np
from theano.tests import unittest_tools as utt from theano.tests import unittest_tools as utt
from theano import function from theano import function
import theano import theano
from theano import tensor
from theano.tensor import dmatrix, dvector from theano.tensor import dmatrix, dvector
from numpy import allclose from numpy import allclose
from theano.compile import as_op from theano.compile import as_op
...@@ -21,16 +20,17 @@ def mul(a, b): ...@@ -21,16 +20,17 @@ def mul(a, b):
class OpDecoratorTests(utt.InferShapeTester): class OpDecoratorTests(utt.InferShapeTester):
def test_1arg(self): def test_1arg(self):
x = dmatrix('x') x = dmatrix('x')
@as_op(dmatrix, dvector) @as_op(dmatrix, dvector)
def diag(x): def cumprod(x):
return np.diag(x) return np.cumprod(x)
fn = function([x], diag(x)) fn = function([x], cumprod(x))
r = fn([[1.5, 5], [2, 2]]) r = fn([[1.5, 5], [2, 2]])
r0 = np.array([1.5, 2]) r0 = np.array([1.5, 7.5, 15., 30.])
assert allclose(r, r0), (r, r0) assert allclose(r, r0), (r, r0)
...@@ -41,12 +41,12 @@ class OpDecoratorTests(utt.InferShapeTester): ...@@ -41,12 +41,12 @@ class OpDecoratorTests(utt.InferShapeTester):
y.tag.test_value = [0, 0] y.tag.test_value = [0, 0]
@as_op([dmatrix, dvector], dvector) @as_op([dmatrix, dvector], dvector)
def diag_mult(x, y): def cumprod_plus(x, y):
return np.diag(x) * y return np.cumprod(x) + y
fn = function([x, y], diag_mult(x, y)) fn = function([x, y], cumprod_plus(x, y))
r = fn([[1.5, 5], [2, 2]], [1, 100]) r = fn([[1.5, 5], [2, 2]], [1, 100, 2, 200])
r0 = np.array([1.5, 200]) r0 = np.array([2.5, 107.5, 17., 230.])
assert allclose(r, r0), (r, r0) assert allclose(r, r0), (r, r0)
...@@ -61,12 +61,12 @@ class OpDecoratorTests(utt.InferShapeTester): ...@@ -61,12 +61,12 @@ class OpDecoratorTests(utt.InferShapeTester):
return [y] return [y]
@as_op([dmatrix, dvector], dvector, infer_shape) @as_op([dmatrix, dvector], dvector, infer_shape)
def diag_mult(x, y): def cumprod_plus(x, y):
return np.diag(x) * y return np.cumprod(x) + y
self._compile_and_check([x, y], [diag_mult(x, y)], self._compile_and_check([x, y], [cumprod_plus(x, y)],
[[[1.5, 5], [2, 2]], [1, 100]], [[[1.5, 5], [2, 2]], [1, 100, 2, 200]],
diag_mult.__class__, warn=False) cumprod_plus.__class__, warn=False)
def test_pickle(self): def test_pickle(self):
x = dmatrix('x') x = dmatrix('x')
...@@ -81,5 +81,4 @@ class OpDecoratorTests(utt.InferShapeTester): ...@@ -81,5 +81,4 @@ class OpDecoratorTests(utt.InferShapeTester):
def test_shape_i_hash(): def test_shape_i_hash():
assert isinstance(theano.tensor.opt.Shape_i(np.int64(1)).__hash__(), assert isinstance(theano.tensor.opt.Shape_i(np.int64(1)).__hash__(), int)
int)
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论