Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
c8eedfae
提交
c8eedfae
authored
5月 14, 2012
作者:
Frederic
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Modif the doc to tell how to test infer_shape and refactored it a little.
上级
2f498b43
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
82 行增加
和
19 行删除
+82
-19
extending_theano.txt
doc/tutorial/extending_theano.txt
+82
-19
没有找到文件。
doc/tutorial/extending_theano.txt
浏览文件 @
c8eedfae
...
@@ -142,43 +142,107 @@ Op example
...
@@ -142,43 +142,107 @@ 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. Those help test the
infer_shape, grad and R_op method. Put the following code in a file
and execute with the `nosetests` program to run it.
Basic tests
===========
Basic tests are done by you just by using the Op and checking it
return the right answer. If you detect an error, you must raise an
exception. You can use the `assert` keywork to raise automatically 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 to result computed to the expected value.
assert numpy.allclose(inp * 2, out)
Testing the infer_shape
=======================
When a class inherit from the InferShapeTester class, it get the
`self._compile_and_check` method that test the Op infer_shape
method. It checks if the optimized graph obtained give the correct
values. It also tests that the Op get removed from the
graph. `self._compile_and_check with` compile theano function. So it
take as parameter the list of inputs and outputs that verify. Then
with a list of real values to pass the the compiled function (don't
use symetic shape!). It also take the class op the op to verify that
it get removed from the graph.
If there is an error, the function raises an exection. If you want to
see it fail, you can implement a wrong 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 exection. If you want to
see it fail, you can implement a wrong gradient (for instance 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 +250,18 @@ To verify the Rop method of the DoubleOp, you can use this:
...
@@ -186,19 +250,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()
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论