Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
e05036f0
提交
e05036f0
authored
6月 23, 2015
作者:
Frédéric Bastien
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #2847 from harmdevries89/softmax_iss2050
[MRG] softmax function that builds expression instead of using softmax op
上级
414300e7
c398b58d
全部展开
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
41 行增加
和
21 行删除
+41
-21
test_scan.py
theano/scan_module/tests/test_scan.py
+1
-1
nnet.py
theano/tensor/nnet/nnet.py
+40
-20
test_nnet.py
theano/tensor/nnet/tests/test_nnet.py
+0
-0
没有找到文件。
theano/scan_module/tests/test_scan.py
浏览文件 @
e05036f0
...
@@ -739,7 +739,7 @@ class T_Scan(unittest.TestCase):
...
@@ -739,7 +739,7 @@ class T_Scan(unittest.TestCase):
def
forward_scanner
(
x_t
):
def
forward_scanner
(
x_t
):
a2_t
=
tensor
.
dot
(
x_t
,
W
)
a2_t
=
tensor
.
dot
(
x_t
,
W
)
y_t
=
tensor
.
nnet
.
softmax
(
a2_t
)
y_t
=
tensor
.
nnet
.
softmax
_graph
(
a2_t
)
return
y_t
return
y_t
y
,
_
=
theano
.
scan
(
fn
=
forward_scanner
,
sequences
=
x
,
y
,
_
=
theano
.
scan
(
fn
=
forward_scanner
,
sequences
=
x
,
...
...
theano/tensor/nnet/nnet.py
浏览文件 @
e05036f0
...
@@ -78,12 +78,17 @@ class SoftmaxWithBias(gof.Op):
...
@@ -78,12 +78,17 @@ class SoftmaxWithBias(gof.Op):
if
b
.
shape
[
0
]
!=
x
.
shape
[
1
]:
if
b
.
shape
[
0
]
!=
x
.
shape
[
1
]:
raise
ValueError
(
'b must have same number of columns as x'
)
raise
ValueError
(
'b must have same number of columns as x'
)
sm
=
numpy
.
zeros_like
(
x
)
# sm = numpy.zeros_like(x)
for
i
in
xrange
(
sm
.
shape
[
0
]):
# for i in xrange(sm.shape[0]):
row
=
x
[
i
]
+
b
# row = x[i] + b
sm
[
i
]
=
numpy
.
exp
(
row
-
numpy
.
max
(
row
))
# sm[i] = numpy.exp(row - numpy.max(row))
sm
[
i
]
*=
1.0
/
numpy
.
sum
(
sm
[
i
])
# sm[i] *= 1.0 / numpy.sum(sm[i])
output_storage
[
0
][
0
]
=
sm
# output_storage[0][0] = sm
x_plus_b
=
x
+
b
[
None
,
:]
e_x
=
numpy
.
exp
(
x_plus_b
-
x_plus_b
.
max
(
axis
=
1
)[:,
None
])
e_x
*=
1.0
/
e_x
.
sum
(
axis
=
1
)[:,
None
]
output_storage
[
0
][
0
]
=
e_x
def
grad
(
self
,
inp
,
grads
):
def
grad
(
self
,
inp
,
grads
):
x
,
b
=
inp
x
,
b
=
inp
...
@@ -304,8 +309,17 @@ class SoftmaxGrad(gof.Op):
...
@@ -304,8 +309,17 @@ class SoftmaxGrad(gof.Op):
dx
[
i
]
=
dy_times_sm_i
-
sum
(
dy_times_sm_i
)
*
sm
[
i
]
dx
[
i
]
=
dy_times_sm_i
-
sum
(
dy_times_sm_i
)
*
sm
[
i
]
output_storage
[
0
][
0
]
=
dx
output_storage
[
0
][
0
]
=
dx
def
grad
(
self
,
*
args
):
def
grad
(
self
,
inp
,
grads
):
raise
NotImplementedError
()
dy
,
sm
=
inp
g
,
=
grads
tmp
=
g
+
tensor
.
neg
(
tensor
.
sum
(
g
*
sm
,
axis
=
1
)
.
dimshuffle
((
0
,
'x'
)))
g_dy
=
tmp
*
sm
tmp2
=
tensor
.
sum
(
dy
*
sm
,
axis
=
1
)
.
dimshuffle
((
0
,
'x'
))
g_sm
=
tmp
*
dy
-
g
*
tmp2
return
g_dy
,
g_sm
def
infer_shape
(
self
,
node
,
shape
):
def
infer_shape
(
self
,
node
,
shape
):
return
[
shape
[
1
]]
return
[
shape
[
1
]]
...
@@ -414,7 +428,7 @@ class Softmax(gof.Op):
...
@@ -414,7 +428,7 @@ class Softmax(gof.Op):
def
grad
(
self
,
inp
,
grads
):
def
grad
(
self
,
inp
,
grads
):
x
,
=
inp
x
,
=
inp
g_sm
,
=
grads
g_sm
,
=
grads
sm
=
softmax
(
x
)
sm
=
softmax
_op
(
x
)
return
[
softmax_grad
(
g_sm
,
sm
)]
return
[
softmax_grad
(
g_sm
,
sm
)]
def
R_op
(
self
,
inputs
,
eval_points
):
def
R_op
(
self
,
inputs
,
eval_points
):
...
@@ -568,15 +582,20 @@ class Softmax(gof.Op):
...
@@ -568,15 +582,20 @@ class Softmax(gof.Op):
def
c_code_cache_version
():
def
c_code_cache_version
():
return
(
3
,)
return
(
3
,)
softmax
=
Softmax
()
softmax_op
=
Softmax
()
def
softmax_graph
(
c
):
return
tensor
.
exp
(
c
)
/
tensor
.
exp
(
c
)
.
sum
(
axis
=-
1
,
keepdims
=
True
)
def
softmax
(
c
):
return
softmax_op
(
c
)
@opt.register_specialize
(
'fast_compile_gpu'
)
@opt.register_specialize
(
'fast_compile_gpu'
)
@gof.local_optimizer
([
softmax
])
@gof.local_optimizer
([
softmax
_op
])
def
local_softmax_with_bias
(
node
):
def
local_softmax_with_bias
(
node
):
"""Try to turn softmax(sum_of_stuff) -> softmax_w_bias(matrix, bias)
"""Try to turn softmax(sum_of_stuff) -> softmax_w_bias(matrix, bias)
"""
"""
if
node
.
op
==
softmax
:
if
node
.
op
==
softmax
_op
:
x
,
=
node
.
inputs
x
,
=
node
.
inputs
if
x
.
owner
and
x
.
owner
.
op
==
tensor
.
add
:
if
x
.
owner
and
x
.
owner
.
op
==
tensor
.
add
:
vectors
=
[]
vectors
=
[]
...
@@ -638,7 +657,7 @@ def softmax_simplifier(numerators, denominators):
...
@@ -638,7 +657,7 @@ def softmax_simplifier(numerators, denominators):
if
not
numerator
.
type
.
dtype
.
startswith
(
'float'
):
if
not
numerator
.
type
.
dtype
.
startswith
(
'float'
):
continue
continue
if
n
ot
numerator
.
type
.
broadcastable
==
(
False
,
False
)
:
if
n
umerator
.
ndim
!=
2
:
continue
continue
if
numerator
.
owner
and
numerator
.
owner
.
op
==
tensor
.
exp
:
if
numerator
.
owner
and
numerator
.
owner
.
op
==
tensor
.
exp
:
x
=
numerator
.
owner
.
inputs
[
0
]
x
=
numerator
.
owner
.
inputs
[
0
]
...
@@ -664,7 +683,8 @@ def softmax_simplifier(numerators, denominators):
...
@@ -664,7 +683,8 @@ def softmax_simplifier(numerators, denominators):
if
matching_denom
:
if
matching_denom
:
numerators
.
remove
(
numerator
)
numerators
.
remove
(
numerator
)
denominators
.
remove
(
matching_denom
)
denominators
.
remove
(
matching_denom
)
numerators
.
append
(
softmax
(
x
))
numerators
.
append
(
softmax_op
(
x
))
return
numerators
,
denominators
return
numerators
,
denominators
opt
.
local_mul_canonizer
.
add_simplifier
(
softmax_simplifier
,
opt
.
local_mul_canonizer
.
add_simplifier
(
softmax_simplifier
,
'softmax_simplifier'
)
'softmax_simplifier'
)
...
@@ -1404,7 +1424,7 @@ def crossentropy_to_crossentropy_with_softmax(fgraph):
...
@@ -1404,7 +1424,7 @@ def crossentropy_to_crossentropy_with_softmax(fgraph):
if
node
.
op
==
crossentropy_categorical_1hot
:
if
node
.
op
==
crossentropy_categorical_1hot
:
nll
,
=
node
.
outputs
nll
,
=
node
.
outputs
sm
,
one_of_n
=
node
.
inputs
sm
,
one_of_n
=
node
.
inputs
if
sm
.
owner
and
sm
.
owner
.
op
==
softmax
:
if
sm
.
owner
and
sm
.
owner
.
op
==
softmax
_op
:
x
,
=
sm
.
owner
.
inputs
x
,
=
sm
.
owner
.
inputs
new_nll
,
new_sm
,
new_am
=
crossentropy_softmax_argmax_1hot_with_bias
(
x
,
new_nll
,
new_sm
,
new_am
=
crossentropy_softmax_argmax_1hot_with_bias
(
x
,
tensor
.
zeros_like
(
x
[
0
]),
one_of_n
)
tensor
.
zeros_like
(
x
[
0
]),
one_of_n
)
...
@@ -1450,7 +1470,7 @@ def local_softmax_grad_to_crossentropy_with_softmax_grad(node):
...
@@ -1450,7 +1470,7 @@ def local_softmax_grad_to_crossentropy_with_softmax_grad(node):
def
local_argmax_pushdown
(
node
):
def
local_argmax_pushdown
(
node
):
if
node
.
op
==
tensor
.
_max_and_argmax
and
node
.
inputs
[
0
]
.
owner
and
\
if
node
.
op
==
tensor
.
_max_and_argmax
and
node
.
inputs
[
0
]
.
owner
and
\
len
(
node
.
outputs
[
0
]
.
clients
)
>
0
and
node
.
inputs
[
0
]
.
owner
.
op
in
\
len
(
node
.
outputs
[
0
]
.
clients
)
>
0
and
node
.
inputs
[
0
]
.
owner
.
op
in
\
(
softmax
,
softplus
,
tensor
.
exp
,
tensor
.
log
,
tensor
.
tanh
,
sigmoid
,
(
softmax
_op
,
softplus
,
tensor
.
exp
,
tensor
.
log
,
tensor
.
tanh
,
sigmoid
,
softmax_with_bias
):
softmax_with_bias
):
if
theano
.
config
.
warn
.
argmax_pushdown_bug
:
if
theano
.
config
.
warn
.
argmax_pushdown_bug
:
logging
.
getLogger
(
'theano.tensor.nnet.nnet'
)
.
warn
(
"WARNING: there "
logging
.
getLogger
(
'theano.tensor.nnet.nnet'
)
.
warn
(
"WARNING: there "
...
@@ -1466,7 +1486,7 @@ def local_argmax_pushdown(node):
...
@@ -1466,7 +1486,7 @@ def local_argmax_pushdown(node):
x_max
,
x_argmax
=
node
.
outputs
x_max
,
x_argmax
=
node
.
outputs
x
,
axis
=
node
.
inputs
x
,
axis
=
node
.
inputs
# TODO: Make a list/set of monotonic ops...
# TODO: Make a list/set of monotonic ops...
if
x
.
owner
and
x
.
owner
.
op
in
(
softmax
,
softplus
,
tensor
.
exp
,
if
x
.
owner
and
x
.
owner
.
op
in
(
softmax
_op
,
softplus
,
tensor
.
exp
,
tensor
.
log
,
tensor
.
tanh
,
sigmoid
):
tensor
.
log
,
tensor
.
tanh
,
sigmoid
):
pre_x
,
=
x
.
owner
.
inputs
pre_x
,
=
x
.
owner
.
inputs
return
tensor
.
_max_and_argmax
(
pre_x
,
axis
)
return
tensor
.
_max_and_argmax
(
pre_x
,
axis
)
...
@@ -1554,7 +1574,7 @@ def local_advanced_indexing_crossentropy_onehot(node):
...
@@ -1554,7 +1574,7 @@ def local_advanced_indexing_crossentropy_onehot(node):
except
Exception
:
except
Exception
:
pass
pass
if
sm
is
not
None
and
sm
.
owner
and
sm
.
owner
.
op
in
(
softmax
,
if
sm
is
not
None
and
sm
.
owner
and
sm
.
owner
.
op
in
(
softmax
_op
,
softmax_with_bias
):
softmax_with_bias
):
sm_w_bias
=
local_softmax_with_bias
.
transform
(
sm
.
owner
)
sm_w_bias
=
local_softmax_with_bias
.
transform
(
sm
.
owner
)
if
sm_w_bias
:
if
sm_w_bias
:
...
@@ -1584,7 +1604,7 @@ def local_advanced_indexing_crossentropy_onehot_grad(node):
...
@@ -1584,7 +1604,7 @@ def local_advanced_indexing_crossentropy_onehot_grad(node):
except
Exception
:
except
Exception
:
return
return
if
(
sm
is
not
None
)
and
sm
.
owner
and
(
sm
.
owner
.
op
in
(
softmax
,
if
(
sm
is
not
None
)
and
sm
.
owner
and
(
sm
.
owner
.
op
in
(
softmax
_op
,
softmax_with_bias
)):
softmax_with_bias
)):
sm_w_bias
=
local_softmax_with_bias
.
transform
(
sm
.
owner
)
sm_w_bias
=
local_softmax_with_bias
.
transform
(
sm
.
owner
)
if
sm_w_bias
:
if
sm_w_bias
:
...
@@ -2054,7 +2074,7 @@ def make_out_pattern(X):
...
@@ -2054,7 +2074,7 @@ def make_out_pattern(X):
return
out_var
return
out_var
local_log_softmax
=
gof
.
PatternSub
(
in_pattern
=
(
tensor
.
log
,
(
softmax
,
'x'
)),
local_log_softmax
=
gof
.
PatternSub
(
in_pattern
=
(
tensor
.
log
,
(
softmax
_op
,
'x'
)),
out_pattern
=
(
make_out_pattern
,
'x'
),
out_pattern
=
(
make_out_pattern
,
'x'
),
allow_multiple_clients
=
True
)
allow_multiple_clients
=
True
)
...
...
theano/tensor/nnet/tests/test_nnet.py
浏览文件 @
e05036f0
差异被折叠。
点击展开。
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论