Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
f53a585d
提交
f53a585d
authored
8月 29, 2012
作者:
Ian Goodfellow
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
upgraded some op's grad methods to support DisconnectedType
上级
345e4745
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
36 行增加
和
19 行删除
+36
-19
nnet.py
theano/tensor/nnet/nnet.py
+36
-19
没有找到文件。
theano/tensor/nnet/nnet.py
浏览文件 @
f53a585d
...
@@ -14,6 +14,7 @@ from theano.compile import optdb
...
@@ -14,6 +14,7 @@ from theano.compile import optdb
from
theano.gof
import
Apply
from
theano.gof
import
Apply
from
theano.tensor.nnet.sigm
import
sigmoid
,
softplus
from
theano.tensor.nnet.sigm
import
sigmoid
,
softplus
from
theano.gradient
import
DisconnectedType
############
############
...
@@ -76,6 +77,10 @@ class SoftmaxWithBias(gof.Op):
...
@@ -76,6 +77,10 @@ class SoftmaxWithBias(gof.Op):
def
grad
(
self
,
inp
,
grads
):
def
grad
(
self
,
inp
,
grads
):
x
,
b
=
inp
x
,
b
=
inp
g_sm
,
=
grads
g_sm
,
=
grads
if
isinstance
(
g_sm
.
type
,
DisconnectedType
):
return
[
DisconnectedType
()(),
DisconnectedType
()()
]
sm
=
softmax_with_bias
(
x
,
b
)
sm
=
softmax_with_bias
(
x
,
b
)
dx
=
softmax_grad
(
g_sm
,
sm
)
dx
=
softmax_grad
(
g_sm
,
sm
)
db
=
tensor
.
sum
(
dx
,
axis
=
0
)
db
=
tensor
.
sum
(
dx
,
axis
=
0
)
...
@@ -710,28 +715,40 @@ class CrossentropySoftmaxArgmax1HotWithBias(gof.Op):
...
@@ -710,28 +715,40 @@ class CrossentropySoftmaxArgmax1HotWithBias(gof.Op):
def
grad
(
self
,
inp
,
grads
):
def
grad
(
self
,
inp
,
grads
):
x
,
b
,
y_idx
=
inp
x
,
b
,
y_idx
=
inp
g_nll
,
g_sm
,
g_am
=
grads
g_nll
,
g_sm
,
g_am
=
grads
#argmax is integer valued, so no gradient flows through it, even if there is
#incoming gradient
g_am
=
None
dx_terms
=
[]
if
g_sm
is
not
None
:
db_terms
=
[]
# There is a gradient w.r.t. the softmax's output itself.
d_idx_terms
=
[]
if
g_nll
is
not
None
:
dx
,
db
=
softmax_with_bias
.
grad
((
x
,
b
,),
(
g_sm
,
))
nll
,
sm
=
crossentropy_softmax_1hot_with_bias
(
x
,
b
,
y_idx
)
if
not
isinstance
(
g_nll
.
type
,
DisconnectedType
):
dx_nll
=
crossentropy_softmax_1hot_with_bias_dx
(
g_nll
,
sm
,
y_idx
)
dx
+=
dx_nll
db
+=
tensor
.
sum
(
dx_nll
,
axis
=
[
0
])
return
dx
,
db
,
None
return
softmax_with_bias
.
grad
((
x
,
b
,
),
(
g_sm
,
))
+
(
None
,
)
else
:
# There is a gradient w.r.t. the NLL.
assert
g_nll
is
not
None
nll
,
sm
=
crossentropy_softmax_1hot_with_bias
(
x
,
b
,
y_idx
)
nll
,
sm
=
crossentropy_softmax_1hot_with_bias
(
x
,
b
,
y_idx
)
#dx = CrossentropySoftmax1HotWithBiasDx()(g_nll, sm, y_idx)
dx
=
crossentropy_softmax_1hot_with_bias_dx
(
g_nll
,
sm
,
y_idx
)
dx
=
crossentropy_softmax_1hot_with_bias_dx
(
g_nll
,
sm
,
y_idx
)
db
=
tensor
.
sum
(
dx
,
axis
=
[
0
])
db
=
tensor
.
sum
(
dx
,
axis
=
[
0
])
return
dx
,
db
,
None
dx_terms
.
append
(
dx
)
db_terms
.
append
(
db
)
if
not
isinstance
(
g_sm
.
type
,
DisconnectedType
):
dx
,
db
=
softmax_with_bias
.
grad
((
x
,
b
),
(
g_sm
,
))
dx_terms
.
append
(
dx
)
db_terms
.
append
(
db
)
if
not
isinstance
(
g_am
.
type
,
DisconnectedType
):
dx_terms
.
append
(
x
.
zeros_like
())
db_terms
.
append
(
b
.
zeros_like
())
d_idx_terms
.
append
(
y_idx
.
zeros_like
())
def
fancy_sum
(
terms
):
if
len
(
terms
)
==
0
:
return
DisconnectedType
()()
rval
=
terms
[
0
]
for
term
in
terms
[
1
:]:
rval
=
rval
+
term
return
rval
return
[
fancy_sum
(
terms
)
for
terms
in
[
dx_terms
,
db_terms
,
d_idx_terms
]
]
def
c_headers
(
self
):
def
c_headers
(
self
):
return
[
'<iostream>'
,
'<cmath>'
]
return
[
'<iostream>'
,
'<cmath>'
]
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论