Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
92fd85fd
提交
92fd85fd
authored
8月 30, 2012
作者:
Frederic
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
finish a solution to tutorial exercices.
上级
13237ee6
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
50 行增加
和
21 行删除
+50
-21
extending_theano_solution_1.py
doc/tutorial/extending_theano_solution_1.py
+50
-21
没有找到文件。
doc/tutorial/extending_theano_solution_1.py
浏览文件 @
92fd85fd
...
...
@@ -73,8 +73,12 @@ class SumDiffOp(theano.Op):
return
[
i0_shapes
[
0
],
i0_shapes
[
0
]]
def
grad
(
self
,
inputs
,
output_grads
):
return
[
output_grads
[
0
]
+
output_grads
[
1
],
output_grads
[
0
]
-
output_grads
[
1
]]
og1
,
og2
=
output_grads
if
og1
is
None
:
og1
=
theano
.
tensor
.
zeros_like
(
og2
)
if
og2
is
None
:
og2
=
theano
.
tensor
.
zeros_like
(
og1
)
return
[
og1
+
og2
,
og1
-
og2
]
# 3. Testing apparatus
...
...
@@ -85,54 +89,79 @@ from theano import tensor, function, printing
from
theano.tests
import
unittest_tools
as
utt
class
TestOp
(
utt
.
InferShapeTester
):
class
Test
Prod
Op
(
utt
.
InferShapeTester
):
rng
=
numpy
.
random
.
RandomState
(
43
)
def
setUp
(
self
):
super
(
TestOp
,
self
)
.
setUp
()
# adapt the choice of the next instruction to the op under test
super
(
TestProdOp
,
self
)
.
setUp
()
self
.
op_class
=
ProdOp
# case 1
#self.op_class = SumDiffOp # case 2
def
test_perform
(
self
):
x
=
theano
.
tensor
.
matrix
()
y
=
theano
.
tensor
.
matrix
()
f
=
theano
.
function
([
x
,
y
],
self
.
op_class
()(
x
,
y
))
import
numpy
x_val
=
numpy
.
random
.
rand
(
5
,
4
)
y_val
=
numpy
.
random
.
rand
(
5
,
4
)
out
=
f
(
x_val
,
y_val
)
# adapt the choice of the next instruction to the op under test
assert
numpy
.
allclose
(
x_val
*
y_val
,
out
)
# case 1
#assert numpy.allclose([x_val + y_val, x_val - y_val], out) # case 2
assert
numpy
.
allclose
(
x_val
*
y_val
,
out
)
def
test_gradient
(
self
):
utt
.
verify_grad
(
self
.
op_class
(),
[
numpy
.
random
.
rand
(
5
,
4
),
numpy
.
random
.
rand
(
5
,
4
)],
n_tests
=
1
,
rng
=
TestOp
.
rng
)
n_tests
=
1
,
rng
=
Test
Prod
Op
.
rng
)
def
test_infer_shape
(
self
):
x
=
tensor
.
dmatrix
()
y
=
tensor
.
dmatrix
()
# adapt the choice of the next instruction to the op under test
self
.
_compile_and_check
([
x
,
y
],
[
self
.
op_class
()(
x
,
y
)],
# case 1
self
.
_compile_and_check
([
x
,
y
],
[
self
.
op_class
()(
x
,
y
)],
[
numpy
.
random
.
rand
(
5
,
6
),
numpy
.
random
.
rand
(
5
,
6
)],
self
.
op_class
)
"""
self._compile_and_check([x, y], self.op_class()(x, y), # case 2
class
TestSumDiffOp
(
utt
.
InferShapeTester
):
rng
=
numpy
.
random
.
RandomState
(
43
)
def
setUp
(
self
):
super
(
TestSumDiffOp
,
self
)
.
setUp
()
self
.
op_class
=
SumDiffOp
def
test_perform
(
self
):
x
=
theano
.
tensor
.
matrix
()
y
=
theano
.
tensor
.
matrix
()
f
=
theano
.
function
([
x
,
y
],
self
.
op_class
()(
x
,
y
))
x_val
=
numpy
.
random
.
rand
(
5
,
4
)
y_val
=
numpy
.
random
.
rand
(
5
,
4
)
out
=
f
(
x_val
,
y_val
)
assert
numpy
.
allclose
([
x_val
+
y_val
,
x_val
-
y_val
],
out
)
def
test_gradient
(
self
):
def
output_0
(
x
,
y
):
return
self
.
op_class
()(
x
,
y
)[
0
]
def
output_1
(
x
,
y
):
return
self
.
op_class
()(
x
,
y
)[
1
]
utt
.
verify_grad
(
output_0
,
[
numpy
.
random
.
rand
(
5
,
4
),
numpy
.
random
.
rand
(
5
,
4
)],
n_tests
=
1
,
rng
=
TestSumDiffOp
.
rng
)
utt
.
verify_grad
(
output_1
,
[
numpy
.
random
.
rand
(
5
,
4
),
numpy
.
random
.
rand
(
5
,
4
)],
n_tests
=
1
,
rng
=
TestSumDiffOp
.
rng
)
def
test_infer_shape
(
self
):
x
=
tensor
.
dmatrix
()
y
=
tensor
.
dmatrix
()
# adapt the choice of the next instruction to the op under test
self
.
_compile_and_check
([
x
,
y
],
self
.
op_class
()(
x
,
y
),
[
numpy
.
random
.
rand
(
5
,
6
),
numpy
.
random
.
rand
(
5
,
6
)],
self
.
op_class
)
"""
if
__name__
==
"__main__"
:
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论