Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
07661f2a
提交
07661f2a
authored
5月 18, 2012
作者:
David Warde-Farley
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #647 from nouiz/doc
Doc
上级
5e9bf42a
66673e83
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
88 行增加
和
19 行删除
+88
-19
extending_theano.txt
doc/tutorial/extending_theano.txt
+87
-19
unittest_tools.py
theano/tests/unittest_tools.py
+1
-0
没有找到文件。
doc/tutorial/extending_theano.txt
浏览文件 @
07661f2a
...
@@ -142,43 +142,112 @@ Op example
...
@@ -142,43 +142,112 @@ Op example
return eval_points
return eval_points
return self.grad(inputs, eval_points)
return self.grad(inputs, eval_points)
T
est
it!
T
ry
it!
.. code-block:: python
.. code-block:: python
x = theano.tensor.matrix()
x = theano.tensor.matrix()
f = theano.function([x], DoubleOp()(x))
f = theano.function([x], DoubleOp()(x))
import numpy
import numpy
inp = numpy.random.rand(5,
5
)
inp = numpy.random.rand(5,
4
)
out = f(inp)
out = f(inp)
assert numpy.allclose(inp * 2, out)
assert numpy.allclose(inp * 2, out)
print inp
print inp
print out
print out
How to test it
--------------
Theano has some functions to simplify testing. These help test the
``infer_shape``, ``grad`` and ``R_op`` methods. Put the following code
in a file and execute it with the ``nosetests`` program.
Basic tests
===========
Basic tests are done by you just by using the Op and checking that it
returns the right answer. If you detect an error, you must raise an
exception. You can use the `assert` keyword to automatically raise an
``AssertionError``.
.. code-block:: python
from theano.tests import unittest_tools as utt
from theano import config
class test_Double(utt.InferShapeTester):
def setUp(self):
super(test_Double, self).setUp()
self.op_class = DoubleOp
self.op = DoubleOp()
def test_basic(self):
x = theano.tensor.matrix()
f = theano.function([x], self.op(x))
inp = numpy.asarray(numpy.random.rand(5, 4), dtype=config.floatX)
out = f(inp)
# Compare the result computed to the expected value.
assert numpy.allclose(inp * 2, out)
Testing the infer_shape
=======================
When a class inherits from the ``InferShapeTester`` class, it gets the
`self._compile_and_check` method that tests the Op ``infer_shape``
method. It tests that the Op gets optimized out of the graph if only
the shape of the output is needed and not the output
itself. Additionally, it checks that such an optimized graph computes
the correct shape, by comparing it to the actual shape of the computed
output.
`self._compile_and_check` compiles a Theano function. It takes as
parameters the lists of input and output Theano variables, as would be
provided to theano.function, and a list of real values to pass to the
compiled function (don't use shapes that are symmetric, e.g. (3, 3),
as they can easily to hide errors). It also takes the Op class to
verify that no Ops of that type appear in the shape-optimized graph.
If there is an error, the function raises an exception. If you want to
see it fail, you can implement an incorrect ``infer_shape``.
.. code-block:: python
def test_infer_shape(self):
x = theano.tensor.matrix()
self._compile_and_check([x], # theano.function inputs
[self.op(x)], # theano.function outputs
# Always use not square matrix!
# inputs data
[numpy.asarray(numpy.random.rand(5, 4),
dtype=config.floatX)],
# Op that should be removed from the graph.
self.op_class)
Testing the gradient
Testing the gradient
--------------------
====================
The function :ref:`verify_grad <validating_grad>`
The function :ref:`verify_grad <validating_grad>`
verifies the gradient of an Op or Theano graph. It compares the
verifies the gradient of an Op or Theano graph. It compares the
analytic (symbolically computed) gradient and the numeric
analytic (symbolically computed) gradient and the numeric
gradient (computed through the Finite Difference Method).
gradient (computed through the Finite Difference Method).
To verify the grad method of the DoubleOp, you can use this:
If there is an error, the function raises an exception. If you want to
see it fail, you can implement an incorrect gradient (for instance, by removing
the multiplication by 2).
.. code-block:: python
.. code-block:: python
import numpy
def test_grad(self):
import theano.tests
theano.tests.unittest_tools.verify_grad(self.op,
theano.tests.unittest_tools.verify_grad(DoubleOp(), [numpy.random.rand(5,7,2)])
[numpy.random.rand(5, 7, 2)])
If nothing happens, then it works! If you want to see it fail, you can
implement a wrong gradient (for instance removing the multiplication by 2).
Testing the Rop
Testing the Rop
---------------
===============
The functions :func:`RopLop_checker.check_mat_rop_lop`,
The class :class:`RopLop_checker`, give the functions
:func:`RopLop_checker.check_rop_lop` and :func:`RopLop_checker.check_nondiff_rop` allow to test the implemntation of the Rop of one function.
:func:`RopLop_checker.check_mat_rop_lop`,
:func:`RopLop_checker.check_rop_lop` and
:func:`RopLop_checker.check_nondiff_rop` that allow to test the
implementation of the Rop method of one Op.
To verify the Rop method of the DoubleOp, you can use this:
To verify the Rop method of the DoubleOp, you can use this:
...
@@ -186,19 +255,18 @@ To verify the Rop method of the DoubleOp, you can use this:
...
@@ -186,19 +255,18 @@ To verify the Rop method of the DoubleOp, you can use this:
import numpy
import numpy
import theano.tests
import theano.tests
from theano.te
nsor.te
sts.test_rop import RopLop_checker
from theano.tests.test_rop import RopLop_checker
class test_Double(RopLop_checker):
class test_Double
Rop
(RopLop_checker):
def setUp(self):
def setUp(self):
super(test_Double, self).setUp()
super(test_Double
Rop
, self).setUp()
def test_double_rop(self):
def test_double_rop(self):
self.check_rop_lop(DoubleOp()(self.x), self.in_shape)
self.check_rop_lop(DoubleRop()(self.x), self.in_shape)
assert False
You can use `nosetests` to run it as all other test in Theano or you can run it like that in a python shell:
You can use `nosetests` to run it as all other test in Theano or you can run it like that in a python shell:
.. code-block:: python
.. code-block:: python
t = test_Double("test_double_rop")
t = test_Double
Rop
("test_double_rop")
t.setUp()
t.setUp()
t.test_double_rop()
t.test_double_rop()
...
...
theano/tests/unittest_tools.py
浏览文件 @
07661f2a
...
@@ -172,6 +172,7 @@ class InferShapeTester(unittest.TestCase):
...
@@ -172,6 +172,7 @@ class InferShapeTester(unittest.TestCase):
def
_compile_and_check
(
self
,
inputs
,
outputs
,
numeric_inputs
,
cls
,
def
_compile_and_check
(
self
,
inputs
,
outputs
,
numeric_inputs
,
cls
,
excluding
=
None
):
excluding
=
None
):
"""This tests the infer_shape method only"""
mode
=
self
.
mode
mode
=
self
.
mode
if
excluding
:
if
excluding
:
mode
=
mode
.
excluding
(
*
excluding
)
mode
=
mode
.
excluding
(
*
excluding
)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论