Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
96acd167
提交
96acd167
authored
2月 05, 2010
作者:
Olivier Delalleau
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fixed recent buggy implementation of CrossentropySoftmax1HotWithBiasDx.grad and…
Fixed recent buggy implementation of CrossentropySoftmax1HotWithBiasDx.grad and added unit test to check it
上级
a7d3d3e5
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
22 行增加
和
4 行删除
+22
-4
nnet.py
theano/tensor/nnet/nnet.py
+8
-4
test_nnet.py
theano/tensor/nnet/tests/test_nnet.py
+14
-0
没有找到文件。
theano/tensor/nnet/nnet.py
浏览文件 @
96acd167
...
@@ -682,16 +682,20 @@ class CrossentropySoftmax1HotWithBiasDx (gof.Op):
...
@@ -682,16 +682,20 @@ class CrossentropySoftmax1HotWithBiasDx (gof.Op):
y_idx
=
tensor
.
as_tensor_variable
(
y_idx
)
y_idx
=
tensor
.
as_tensor_variable
(
y_idx
)
return
gof
.
Apply
(
self
,
[
dy
,
sm
,
y_idx
],[
sm
.
type
.
make_variable
()])
return
gof
.
Apply
(
self
,
[
dy
,
sm
,
y_idx
],[
sm
.
type
.
make_variable
()])
def
perform
(
self
,
node
,
input_storage
,
output_storage
):
def
perform
(
self
,
node
,
input_storage
,
output_storage
):
dy
,
sm
,
y_idx
=
input_storage
dy
,
sm
,
y_idx
=
input_storage
dx
=
numpy
.
zeros_like
(
sm
)
dx
=
numpy
.
zeros_like
(
sm
)
for
i
in
xrange
(
sm
.
shape
[
0
]):
for
i
in
xrange
(
sm
.
shape
[
0
]):
dx
[
i
]
=
dy
[
i
]
*
sm
[
i
]
#vector scale
dx
[
i
]
=
dy
[
i
]
*
sm
[
i
]
#vector scale
dx
[
i
,
y_idx
[
i
]]
-=
dy
[
i
]
#scalar decrement
dx
[
i
,
y_idx
[
i
]]
-=
dy
[
i
]
#scalar decrement
output_storage
[
0
][
0
]
=
dx
output_storage
[
0
][
0
]
=
dx
def
grad
(
self
,
(
dy
,
sm
,
y_idx
),
(
g_dx
,
)):
def
grad
(
self
,
(
dy
,
sm
,
y_idx
),
(
g_dx
,
)):
# Note: currently we do not care about computing the gradient of dy,
# TODO: currently we do not compute the gradient w.r.t. dy, because
# since we usually should not need it.
# advanced indexing is not working yet. When it works, do it to avoid
return
[
None
,
dy
*
g_dx
,
None
]
# potentially misleading behavior in gradient computations! (although
# typically we should not need the gradient w.r.t. dy).
# y_idx_range = tensor.arange(y_idx.shape[0])
# return [g_dx * tensor.AdvancedIncSubtensor((y_idx_range, y_idx))(sm, -1, y_idx_range, y_idx), dy.dimshuffle(0, 'x') * g_dx, None]
return
[
None
,
dy
.
dimshuffle
(
0
,
'x'
)
*
g_dx
,
None
]
def
c_code_cache_version
(
self
):
def
c_code_cache_version
(
self
):
return
(
2
,)
return
(
2
,)
def
c_code
(
self
,
node
,
name
,
(
dnll
,
sm
,
y_idx
),
(
dx
,),
sub
):
def
c_code
(
self
,
node
,
name
,
(
dnll
,
sm
,
y_idx
),
(
dx
,),
sub
):
...
...
theano/tensor/nnet/tests/test_nnet.py
浏览文件 @
96acd167
...
@@ -82,6 +82,20 @@ class T_CrossentropySoftmax1Hot(unittest.TestCase):
...
@@ -82,6 +82,20 @@ class T_CrossentropySoftmax1Hot(unittest.TestCase):
return
crossentropy_softmax_1hot
(
a
,
y_idx
)[
0
]
return
crossentropy_softmax_1hot
(
a
,
y_idx
)[
0
]
utt
.
verify_grad
(
f
,
[
numpy
.
random
.
rand
(
3
,
4
)])
utt
.
verify_grad
(
f
,
[
numpy
.
random
.
rand
(
3
,
4
)])
class
T_CrossentropySoftmax1HotWithBiasDx
(
unittest
.
TestCase
):
def
setUp
(
self
):
utt
.
seed_rng
()
def
test0
(
self
):
def
f
(
sm
):
return
(
theano
.
tensor
.
nnet
.
crossentropy_softmax_1hot_with_bias_dx
(
numpy
.
random
.
rand
(
10
),
# Gradient w.r.t. NLL.
sm
,
# Softmax output.
numpy
.
random
.
randint
(
low
=
0
,
high
=
5
,
size
=
10
)))
# Class indices.
# Build a random softmax output whose rows sum to 1.
softmax_output
=
numpy
.
random
.
rand
(
10
,
5
)
softmax_output
/=
softmax_output
.
sum
(
axis
=
1
)
.
reshape
(
10
,
1
)
utt
.
verify_grad
(
f
,
[
softmax_output
])
class
T_prepend
(
unittest
.
TestCase
):
class
T_prepend
(
unittest
.
TestCase
):
def
setUp
(
self
):
def
setUp
(
self
):
utt
.
seed_rng
()
utt
.
seed_rng
()
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论