Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
06c15dc1
提交
06c15dc1
authored
5月 04, 2010
作者:
James Bergstra
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
added grad to IncSubtensor
上级
563bdb57
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
52 行增加
和
8 行删除
+52
-8
basic.py
theano/tensor/basic.py
+17
-4
test_incsubtensor.py
theano/tensor/tests/test_incsubtensor.py
+35
-4
没有找到文件。
theano/tensor/basic.py
浏览文件 @
06c15dc1
...
...
@@ -2465,7 +2465,7 @@ class IncSubtensor(Op):
This is like numpy's
z[i,j,k] += <something>
x[i,j,k] += y
It is used internally to implement the gradient on SubTensor.
...
...
@@ -2583,6 +2583,15 @@ class IncSubtensor(Op):
x
.
__setitem__
(
cdata
,
y
)
out
[
0
]
=
x
def
grad
(
self
,
inputs
,
(
g_output
,)):
x
,
y
=
inputs
[:
2
]
idx_list
=
inputs
[
2
:]
gx
=
g_output
gy
=
Subtensor
(
idx_list
=
self
.
idx_list
)(
g_output
,
*
idx_list
)
return
[
gx
,
gy
]
+
[
None
]
*
len
(
idx_list
)
def
split
(
x
,
splits_size
,
n_splits
,
axis
=
0
):
the_split
=
Split
(
n_splits
)
return
the_split
(
x
,
axis
,
splits_size
)
...
...
@@ -3906,7 +3915,7 @@ class numeric_grad:
apt
[
i
]
=
x
[
cur_pos
:
cur_pos
+
p_size
]
.
reshape
(
p
.
shape
)
self
.
gf
.
append
(
gx
[
cur_pos
:
cur_pos
+
p_size
]
.
reshape
(
p
.
shape
))
# initialize with p's value
apt
[
i
][
:
]
=
p
apt
[
i
][
...
]
=
p
cur_pos
+=
p_size
f_x
=
f
(
*
[
p
.
copy
()
for
p
in
apt
])
...
...
@@ -3945,7 +3954,10 @@ class numeric_grad:
def
verify_grad
(
op
,
pt
,
n_tests
=
2
,
rng
=
None
,
eps
=
None
,
tol
=
None
,
mode
=
None
,
cast_to_output_type
=
False
):
""" WRITEME
""" Test an Op's gradient by side effect. Return None on success, raise error on failure.
Example:
>>> verify_grad(theano.tensor.tanh, (numpy.asarray([[2,3,4], [-1, 3.3, 9.9]]),))
Raises an Exception if the difference between the analytic gradient and
numerical gradient (computed through the Finite Difference Method) exceeds
...
...
@@ -3953,7 +3965,8 @@ def verify_grad(op, pt, n_tests=2, rng=None, eps=None, tol=None, mode=None, cast
:param op: something that behaves like an Op instance with a single output
(can be a python function combining multiple ops, but see note below)
:param pt: the list of numpy.ndarrays to use as inputs to the op
:param pt: the list of numpy.ndarrays to use as inputs to the op. These arrays must be
either float32 or float64 arrays.
:param n_tests: number of times to run the test
:param rng: random number generator from which to draw random samples
:param eps: stepsize used in the Finite Difference Method (Default None is type-dependent)
...
...
theano/tensor/tests/test_incsubtensor.py
浏览文件 @
06c15dc1
import
numpy
as
N
import
numpy
import
unittest
from
theano.tests
import
unittest_tools
as
utt
import
theano
...
...
@@ -36,18 +36,49 @@ class Test_incsubtensor(unittest.TestCase):
f
=
theano
.
function
([
a
,
increment
,
sl2_end
],
resut
)
val_a
=
N
.
ones
((
5
,
5
))
val_a
=
numpy
.
ones
((
5
,
5
))
val_inc
=
2.3
val_sl2_end
=
2
result
=
f
(
val_a
,
val_inc
,
val_sl2_end
)
expected_result
=
N
.
copy
(
val_a
)
expected_result
=
numpy
.
copy
(
val_a
)
if
do_set
:
expected_result
[:,:
val_sl2_end
]
=
val_inc
else
:
expected_result
[:,:
val_sl2_end
]
+=
val_inc
self
.
failUnless
(
N
.
array_equal
(
result
,
expected_result
))
self
.
failUnless
(
numpy
.
array_equal
(
result
,
expected_result
))
return
def
test_grad
(
self
):
a
=
T
.
dvector
()
b
=
T
.
dvector
()
def
inc_slice
(
*
s
):
def
just_numeric_args
(
a
,
b
):
return
T
.
incsubtensor
(
a
,
b
,
s
)
return
just_numeric_args
# vector
utt
.
verify_grad
(
inc_slice
(
slice
(
2
,
4
,
None
)),
(
numpy
.
asarray
([
0
,
1
,
2
,
3
,
4
,
5.
]),
numpy
.
asarray
([
9
,
9.
]),))
# matrix
utt
.
verify_grad
(
inc_slice
(
slice
(
1
,
2
,
None
),
slice
(
None
,
None
,
None
)),
(
numpy
.
asarray
([[
0
,
1
],[
2
,
3
],[
4
,
5.
]]),
numpy
.
asarray
([[
9
,
9.
]]),))
#single element
utt
.
verify_grad
(
inc_slice
(
2
,
1
),
(
numpy
.
asarray
([[
0
,
1
],[
2
,
3
],[
4
,
5.
]]),
numpy
.
asarray
(
9.
),))
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论