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

Merge pull request #8 from nouiz/abergeron-op_decorator

Add Doc and modif tests
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
shared shared
function function
io io
ops
mode mode
module module
debugmode debugmode
......
==================================================
:mod:`ops` -- Some Common Ops and extra Ops stuff
==================================================
.. automodule:: theano.compile.ops
:members:
"""This file contain auxiliary Ops, used during the compilation phase.""" """This file contain auxiliary Ops, used during the compilation phase
and Ops building class (:class:`FromFunctionOp`) and decorator
(:func:`as_op`) that help make new Ops more rapidly.
"""
import copy import copy
import warnings import warnings
......
""" """
Tests for the Op decorator Tests for the Op decorator
""" """
import numpy as np
import unittest
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 import tensor
from theano.tensor import dmatrix, dvector from theano.tensor import dmatrix, dvector
import numpy as np
from numpy import allclose from numpy import allclose
from theano.compile import as_op from theano.compile import as_op
class OpDecoratorTests(unittest.TestCase):
class OpDecoratorTests(utt.InferShapeTester):
def test_1arg(self): def test_1arg(self):
x = dmatrix('x') x = dmatrix('x')
...@@ -21,44 +21,41 @@ class OpDecoratorTests(unittest.TestCase): ...@@ -21,44 +21,41 @@ class OpDecoratorTests(unittest.TestCase):
return np.diag(x) return np.diag(x)
fn = function([x], diag(x)) fn = function([x], diag(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, 2])
assert allclose(r, r0), (r, r0) assert allclose(r, r0), (r, r0)
def test_2arg(self): def test_2arg(self):
x = dmatrix('x') x = dmatrix('x')
x.tag.test_value=np.zeros((2,2)) x.tag.test_value = np.zeros((2, 2))
y = dvector('y') y = dvector('y')
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 diag_mult(x, y):
return np.diag(x) * y return np.diag(x) * y
fn = function([x, y], diag_mult(x, y)) fn = function([x, y], diag_mult(x, y))
r = fn([[1.5, 5],[2, 2]], [1, 100]) r = fn([[1.5, 5], [2, 2]], [1, 100])
r0 = np.array([1.5, 200]) r0 = np.array([1.5, 200])
assert allclose(r, r0), (r, r0) assert allclose(r, r0), (r, r0)
def test_infer_shape(self): def test_infer_shape(self):
x = dmatrix('x') x = dmatrix('x')
x.tag.test_value=np.zeros((2,2)) x.tag.test_value = np.zeros((2, 2))
y = dvector('y') y = dvector('y')
y.tag.test_value=[0,0] y.tag.test_value = [0, 0]
def infer_shape(node, shapes): def infer_shape(node, shapes):
x,y = shapes x, y = shapes
return [y] return [y]
@as_op([dmatrix, dvector], dvector, infer_shape) @as_op([dmatrix, dvector], dvector, infer_shape)
def diag_mult(x, y): def diag_mult(x, y):
return np.diag(x) * y return np.diag(x) * y
fn = function([x, y], diag_mult(x, y).shape) self._compile_and_check([x, y], [diag_mult(x, y)],
r = fn([[1.5, 5],[2, 2]], [1, 100]) [[[1.5, 5], [2, 2]], [1, 100]],
r0 = (2,) diag_mult.__class__, warn=False)
assert allclose(r, r0), (r, r0)
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论