Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
70b5f2c1
提交
70b5f2c1
authored
6月 30, 2015
作者:
Iban Harlouchet
提交者:
Frederic
7月 23, 2015
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
flake8 for tensor/nnet/nnet.py
上级
f4edcc59
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
39 行增加
和
38 行删除
+39
-38
nnet.py
theano/tensor/nnet/nnet.py
+39
-37
test_flake8.py
theano/tests/test_flake8.py
+0
-1
没有找到文件。
theano/tensor/nnet/nnet.py
浏览文件 @
70b5f2c1
...
@@ -15,6 +15,7 @@ from six.moves import xrange
...
@@ -15,6 +15,7 @@ from six.moves import xrange
import
theano
import
theano
from
theano
import
gof
from
theano
import
gof
from
theano
import
scalar
from
theano.tensor
import
basic
as
tensor
from
theano.tensor
import
basic
as
tensor
from
theano.tensor
import
subtensor
from
theano.tensor
import
subtensor
from
theano.tensor
import
elemwise
from
theano.tensor
import
elemwise
...
@@ -27,12 +28,12 @@ from theano.gradient import DisconnectedType
...
@@ -27,12 +28,12 @@ from theano.gradient import DisconnectedType
from
theano.gradient
import
grad_not_implemented
from
theano.gradient
import
grad_not_implemented
from
theano.tensor.type
import
values_eq_approx_remove_nan
from
theano.tensor.type
import
values_eq_approx_remove_nan
############
############
#
#
# TENSOR OPS
# TENSOR OPS
#
#
class
SoftmaxWithBias
(
gof
.
Op
):
class
SoftmaxWithBias
(
gof
.
Op
):
"""
"""
An L{Op} for the output of neural-net multiclass classifiers.
An L{Op} for the output of neural-net multiclass classifiers.
...
@@ -300,11 +301,11 @@ class SoftmaxGrad(gof.Op):
...
@@ -300,11 +301,11 @@ class SoftmaxGrad(gof.Op):
dy
,
sm
=
inp
dy
,
sm
=
inp
g
,
=
grads
g
,
=
grads
tmp
=
g
+
tensor
.
neg
(
tensor
.
sum
(
g
*
sm
,
axis
=
1
)
.
dimshuffle
((
0
,
'x'
)))
tmp
=
g
+
tensor
.
neg
(
tensor
.
sum
(
g
*
sm
,
axis
=
1
)
.
dimshuffle
((
0
,
'x'
)))
g_dy
=
tmp
*
sm
g_dy
=
tmp
*
sm
tmp2
=
tensor
.
sum
(
dy
*
sm
,
axis
=
1
)
.
dimshuffle
((
0
,
'x'
))
tmp2
=
tensor
.
sum
(
dy
*
sm
,
axis
=
1
)
.
dimshuffle
((
0
,
'x'
))
g_sm
=
tmp
*
dy
-
g
*
tmp2
g_sm
=
tmp
*
dy
-
g
*
tmp2
return
g_dy
,
g_sm
return
g_dy
,
g_sm
...
@@ -571,12 +572,15 @@ class Softmax(gof.Op):
...
@@ -571,12 +572,15 @@ class Softmax(gof.Op):
softmax_op
=
Softmax
()
softmax_op
=
Softmax
()
def
softmax_graph
(
c
):
def
softmax_graph
(
c
):
return
tensor
.
exp
(
c
)
/
tensor
.
exp
(
c
)
.
sum
(
axis
=-
1
,
keepdims
=
True
)
return
tensor
.
exp
(
c
)
/
tensor
.
exp
(
c
)
.
sum
(
axis
=-
1
,
keepdims
=
True
)
def
softmax
(
c
):
def
softmax
(
c
):
return
softmax_op
(
c
)
return
softmax_op
(
c
)
@opt.register_specialize
(
'fast_compile_gpu'
)
@opt.register_specialize
(
'fast_compile_gpu'
)
@gof.local_optimizer
([
softmax_op
])
@gof.local_optimizer
([
softmax_op
])
def
local_softmax_with_bias
(
node
):
def
local_softmax_with_bias
(
node
):
...
@@ -593,9 +597,9 @@ def local_softmax_with_bias(node):
...
@@ -593,9 +597,9 @@ def local_softmax_with_bias(node):
# tensor.DimShuffle) since specialization comes
# tensor.DimShuffle) since specialization comes
# relatively late in optimization, we don't want to
# relatively late in optimization, we don't want to
# put in extra DimShuffles un-necessarily.
# put in extra DimShuffles un-necessarily.
if
(
x_in
.
owner
and
isinstance
(
x_in
.
owner
.
op
,
if
(
x_in
.
owner
and
tensor
.
DimShuffle
)
isinstance
(
x_in
.
owner
.
op
,
tensor
.
DimShuffle
)
and
and
list
(
x_in
.
owner
.
inputs
[
0
]
.
type
.
broadcastable
)
==
[
False
]):
list
(
x_in
.
owner
.
inputs
[
0
]
.
type
.
broadcastable
)
==
[
False
]):
# cut out the DimShuffle that was broadcasting a vector
# cut out the DimShuffle that was broadcasting a vector
vectors
.
append
(
x_in
.
owner
.
inputs
[
0
])
vectors
.
append
(
x_in
.
owner
.
inputs
[
0
])
else
:
else
:
...
@@ -673,8 +677,7 @@ def softmax_simplifier(numerators, denominators):
...
@@ -673,8 +677,7 @@ def softmax_simplifier(numerators, denominators):
numerators
.
append
(
softmax_op
(
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'
)
if
0
:
if
0
:
@opt.register_specialize
@opt.register_specialize
...
@@ -836,7 +839,7 @@ class CrossentropySoftmaxArgmax1HotWithBias(gof.Op):
...
@@ -836,7 +839,7 @@ class CrossentropySoftmaxArgmax1HotWithBias(gof.Op):
# TODO: Is this correct? It used to be y, not y_idx
# TODO: Is this correct? It used to be y, not y_idx
nll
=
tensor
.
TensorType
(
x
.
type
.
dtype
,
nll
=
tensor
.
TensorType
(
x
.
type
.
dtype
,
y_idx
.
type
.
broadcastable
)
()
y_idx
.
type
.
broadcastable
)
.
make_variable
()
# nll = TensorType(x.dtype, y.broadcastable)
# nll = TensorType(x.dtype, y.broadcastable)
sm
=
x
.
type
()
sm
=
x
.
type
()
am
=
y_idx
.
type
()
am
=
y_idx
.
type
()
...
@@ -866,15 +869,14 @@ class CrossentropySoftmaxArgmax1HotWithBias(gof.Op):
...
@@ -866,15 +869,14 @@ class CrossentropySoftmaxArgmax1HotWithBias(gof.Op):
if
any
(
y_idx
<
0
):
if
any
(
y_idx
<
0
):
raise
ValueError
(
"y_i value out of bounds"
)
raise
ValueError
(
"y_i value out of bounds"
)
sm
=
numpy
.
zeros_like
(
x
)
# softmax
sm
=
numpy
.
zeros_like
(
x
)
# softmax
nll
=
numpy
.
zeros
(
x
.
shape
[
0
],
dtype
=
node
.
outputs
[
0
]
.
type
.
nll
=
numpy
.
zeros
(
x
.
shape
[
0
],
dtype
=
node
.
outputs
[
0
]
.
type
.
dtype
)
# nll(y | softmax(x))
dtype
)
# nll(y | softmax(x))
am
=
numpy
.
zeros_like
(
y_idx
)
am
=
numpy
.
zeros_like
(
y_idx
)
for
i
in
xrange
(
sm
.
shape
[
0
]):
for
i
in
xrange
(
sm
.
shape
[
0
]):
# add the bias vector to the i'th row of x
# add the bias vector to the i'th row of x
row
=
x
[
i
]
+
b
row
=
x
[
i
]
+
b
# get the maximum value of i'th row for numerically safe
# get the maximum value of i'th row for numerically safe
#softmax / nll
#
softmax / nll
am
[
i
]
=
numpy
.
argmax
(
row
)
am
[
i
]
=
numpy
.
argmax
(
row
)
m
=
row
[
am
[
i
]]
m
=
row
[
am
[
i
]]
...
@@ -1083,8 +1085,7 @@ class CrossentropySoftmax1HotWithBiasDx(gof.Op):
...
@@ -1083,8 +1085,7 @@ class CrossentropySoftmax1HotWithBiasDx(gof.Op):
y_idx_range
=
tensor
.
arange
(
y_idx
.
shape
[
0
])
y_idx_range
=
tensor
.
arange
(
y_idx
.
shape
[
0
])
g_dy
=
tensor
.
sum
(
g_dy
=
tensor
.
sum
(
g_dx
*
subtensor
.
AdvancedIncSubtensor
()(
g_dx
*
subtensor
.
AdvancedIncSubtensor
()(
sm
,
tensor
.
fill
(
dy
,
-
1
),
y_idx_range
,
y_idx
),
sm
,
tensor
.
fill
(
dy
,
-
1
),
y_idx_range
,
y_idx
),
axis
=
1
)
axis
=
1
)
g_sm
=
dy
.
dimshuffle
(
0
,
'x'
)
*
g_dx
g_sm
=
dy
.
dimshuffle
(
0
,
'x'
)
*
g_dx
g_y_idx
=
grad_not_implemented
(
self
,
2
,
y_idx
)
g_y_idx
=
grad_not_implemented
(
self
,
2
,
y_idx
)
return
[
g_dy
,
g_sm
,
g_y_idx
]
return
[
g_dy
,
g_sm
,
g_y_idx
]
...
@@ -1226,8 +1227,7 @@ def crossentropy_softmax_max_and_argmax_1hot_with_bias(x, b, y_idx, **kwargs):
...
@@ -1226,8 +1227,7 @@ def crossentropy_softmax_max_and_argmax_1hot_with_bias(x, b, y_idx, **kwargs):
unnecessary? e.g. CrossentropySoftmaxArgmax1HotWithBias should return
unnecessary? e.g. CrossentropySoftmaxArgmax1HotWithBias should return
the appropriate information (i.e. the max probability)?
the appropriate information (i.e. the max probability)?
"""
"""
(
xent
,
softmax
)
=
crossentropy_softmax_1hot_with_bias
(
x
,
b
,
y_idx
,
(
xent
,
softmax
)
=
crossentropy_softmax_1hot_with_bias
(
x
,
b
,
y_idx
,
**
kwargs
)
**
kwargs
)
(
max_pr
,
argmax
)
=
tensor
.
max_and_argmax
(
softmax
,
axis
=-
1
)
(
max_pr
,
argmax
)
=
tensor
.
max_and_argmax
(
softmax
,
axis
=-
1
)
return
(
xent
,
softmax
,
max_pr
,
argmax
)
return
(
xent
,
softmax
,
max_pr
,
argmax
)
...
@@ -1251,8 +1251,8 @@ class CrossentropyCategorical1HotGrad(gof.Op):
...
@@ -1251,8 +1251,8 @@ class CrossentropyCategorical1HotGrad(gof.Op):
g_coding_strg
,
=
out
g_coding_strg
,
=
out
g_coding
=
numpy
.
zeros_like
(
coding_dist
)
g_coding
=
numpy
.
zeros_like
(
coding_dist
)
for
i
in
xrange
(
len
(
g_y
)):
for
i
in
xrange
(
len
(
g_y
)):
g_coding
[
i
,
true_one_of_n
[
i
]]
=
-
g_y
[
i
]
/
coding_dist
[
i
,
g_coding
[
i
,
true_one_of_n
[
i
]]
=
(
-
g_y
[
i
]
/
true_one_of_n
[
i
]]
coding_dist
[
i
,
true_one_of_n
[
i
]])
g_coding_strg
[
0
]
=
g_coding
g_coding_strg
[
0
]
=
g_coding
def
infer_shape
(
self
,
node
,
in_shapes
):
def
infer_shape
(
self
,
node
,
in_shapes
):
...
@@ -1346,9 +1346,10 @@ def crossentropy_to_crossentropy_with_softmax_with_bias(fgraph):
...
@@ -1346,9 +1346,10 @@ def crossentropy_to_crossentropy_with_softmax_with_bias(fgraph):
sm
,
one_of_n
=
node
.
inputs
sm
,
one_of_n
=
node
.
inputs
if
sm
.
owner
and
sm
.
owner
.
op
==
softmax_with_bias
:
if
sm
.
owner
and
sm
.
owner
.
op
==
softmax_with_bias
:
x
,
b
=
sm
.
owner
.
inputs
x
,
b
=
sm
.
owner
.
inputs
new_nll
,
new_sm
,
new_am
=
crossentropy_softmax_argmax_1hot_with_bias
(
x
,
b
,
new_nll
,
new_sm
,
new_am
=
crossentropy_softmax_argmax_1hot_with_bias
(
one_of_n
)
x
,
b
,
one_of_n
)
fgraph
.
replace_all_validate
([(
nll
,
new_nll
),
(
sm
,
new_sm
)],
fgraph
.
replace_all_validate
(
[(
nll
,
new_nll
),
(
sm
,
new_sm
)],
reason
=
"crossentropy_to_crossentropy_with_softmax_with_bias"
)
reason
=
"crossentropy_to_crossentropy_with_softmax_with_bias"
)
return
True
return
True
...
@@ -1381,16 +1382,18 @@ def crossentropy_to_crossentropy_with_softmax(fgraph):
...
@@ -1381,16 +1382,18 @@ def crossentropy_to_crossentropy_with_softmax(fgraph):
sm
,
one_of_n
=
node
.
inputs
sm
,
one_of_n
=
node
.
inputs
if
sm
.
owner
and
sm
.
owner
.
op
==
softmax_op
:
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
(
tensor
.
zeros_like
(
x
[
0
]),
one_of_n
)
x
,
tensor
.
zeros_like
(
x
[
0
]),
one_of_n
)
fgraph
.
replace_all_validate
([(
nll
,
new_nll
),
(
sm
,
new_sm
)],
fgraph
.
replace_all_validate
(
[(
nll
,
new_nll
),
(
sm
,
new_sm
)],
reason
=
"crossentropy_to_crossentropy_with_softmax"
)
reason
=
"crossentropy_to_crossentropy_with_softmax"
)
return
True
return
True
if
sm
.
owner
and
sm
.
owner
.
op
==
softmax_with_bias
:
if
sm
.
owner
and
sm
.
owner
.
op
==
softmax_with_bias
:
x
,
b
=
sm
.
owner
.
inputs
x
,
b
=
sm
.
owner
.
inputs
new_nll
,
new_sm
,
new_am
=
crossentropy_softmax_argmax_1hot_with_bias
(
x
,
b
,
new_nll
,
new_sm
,
new_am
=
crossentropy_softmax_argmax_1hot_with_bias
(
x
,
b
,
one_of_n
)
one_of_n
)
fgraph
.
replace_all_validate
([(
nll
,
new_nll
),
(
sm
,
new_sm
)],
fgraph
.
replace_all_validate
(
[(
nll
,
new_nll
),
(
sm
,
new_sm
)],
reason
=
"crossentropy_to_crossentropy_with_softmax"
)
reason
=
"crossentropy_to_crossentropy_with_softmax"
)
return
True
return
True
...
@@ -1415,8 +1418,8 @@ def local_softmax_grad_to_crossentropy_with_softmax_grad(node):
...
@@ -1415,8 +1418,8 @@ def local_softmax_grad_to_crossentropy_with_softmax_grad(node):
if
(
g_coding_dist
.
owner
and
if
(
g_coding_dist
.
owner
and
g_coding_dist
.
owner
.
op
==
crossentropy_categorical_1hot_grad
):
g_coding_dist
.
owner
.
op
==
crossentropy_categorical_1hot_grad
):
g_nll
,
coding_dist
,
true_one_of_n
=
g_coding_dist
.
owner
.
inputs
g_nll
,
coding_dist
,
true_one_of_n
=
g_coding_dist
.
owner
.
inputs
dx
=
crossentropy_softmax_1hot_with_bias_dx
(
g_nll
,
dx
=
crossentropy_softmax_1hot_with_bias_dx
(
g_nll
,
coding_dist
,
coding_dist
,
true_one_of_n
)
true_one_of_n
)
return
[
dx
]
return
[
dx
]
...
@@ -1428,7 +1431,8 @@ def local_argmax_pushdown(node):
...
@@ -1428,7 +1431,8 @@ def local_argmax_pushdown(node):
(
softmax_op
,
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 "
"was a bug in Theano fixed on May 27th, 2010 in this case."
"was a bug in Theano fixed on May 27th, 2010 in this case."
" I.E. when we take the max of a softplus, softmax, exp, "
" I.E. when we take the max of a softplus, softmax, exp, "
"log, tanh, sigmoid, softmax_with_bias op, we were doing "
"log, tanh, sigmoid, softmax_with_bias op, we were doing "
...
@@ -1657,15 +1661,15 @@ def local_advanced_indexing_crossentropy_onehot_grad(node):
...
@@ -1657,15 +1661,15 @@ def local_advanced_indexing_crossentropy_onehot_grad(node):
if
isinstance
(
denom
.
owner
.
op
,
subtensor
.
AdvancedSubtensor
):
if
isinstance
(
denom
.
owner
.
op
,
subtensor
.
AdvancedSubtensor
):
# Base case
# Base case
adv_subtensor
=
denom
adv_subtensor
=
denom
#out_grad /= 1.
#
out_grad /= 1.
elif
denom
.
owner
.
op
==
tensor
.
mul
:
elif
denom
.
owner
.
op
==
tensor
.
mul
:
# Try to find the AdvancedSubtensor node mentionned above,
# Try to find the AdvancedSubtensor node mentionned above,
# and the output gradient
# and the output gradient
for
i
,
input
in
enumerate
(
denom
.
owner
.
inputs
):
for
i
,
input
in
enumerate
(
denom
.
owner
.
inputs
):
if
input
.
owner
and
isinstance
(
input
.
owner
.
op
,
if
input
.
owner
and
isinstance
(
input
.
owner
.
op
,
subtensor
.
AdvancedSubtensor
):
subtensor
.
AdvancedSubtensor
):
other_inputs
=
[
in_
for
(
j
,
other_inputs
=
[
in_
for
(
j
,
in_
)
in
in_
)
in
enumerate
(
denom
.
owner
.
inputs
)
if
j
!=
i
]
enumerate
(
denom
.
owner
.
inputs
)
if
j
!=
i
]
if
len
(
other_inputs
)
==
1
:
if
len
(
other_inputs
)
==
1
:
rest
=
other_inputs
[
0
]
rest
=
other_inputs
[
0
]
else
:
else
:
...
@@ -1894,16 +1898,14 @@ def categorical_crossentropy(coding_dist, true_dist):
...
@@ -1894,16 +1898,14 @@ def categorical_crossentropy(coding_dist, true_dist):
"""
"""
if
true_dist
.
ndim
==
coding_dist
.
ndim
:
if
true_dist
.
ndim
==
coding_dist
.
ndim
:
return
-
tensor
.
sum
(
true_dist
*
tensor
.
log
(
coding_dist
),
axis
=
coding_dist
.
ndim
-
1
)
return
-
tensor
.
sum
(
true_dist
*
tensor
.
log
(
coding_dist
),
axis
=
coding_dist
.
ndim
-
1
)
elif
true_dist
.
ndim
==
coding_dist
.
ndim
-
1
:
elif
true_dist
.
ndim
==
coding_dist
.
ndim
-
1
:
return
crossentropy_categorical_1hot
(
coding_dist
,
true_dist
)
return
crossentropy_categorical_1hot
(
coding_dist
,
true_dist
)
else
:
else
:
raise
TypeError
(
'rank mismatch between coding and true distributions'
)
raise
TypeError
(
'rank mismatch between coding and true distributions'
)
from
theano
import
scalar
class
Prepend_scalar_constant_to_each_row
(
gof
.
Op
):
class
Prepend_scalar_constant_to_each_row
(
gof
.
Op
):
__props__
=
()
__props__
=
()
...
@@ -2026,7 +2028,7 @@ local_log_softmax = gof.PatternSub(in_pattern=(tensor.log, (softmax_op, 'x')),
...
@@ -2026,7 +2028,7 @@ local_log_softmax = gof.PatternSub(in_pattern=(tensor.log, (softmax_op, 'x')),
# don't do register_stabilize, this is to make local_log_softmax run
# don't do register_stabilize, this is to make local_log_softmax run
# only after another more specific optimization that stabilizes cross entropy
# only after another more specific optimization that stabilizes cross entropy
#opt.register_stabilize(local_log_softmax, name = 'local_log_softmax')
#
opt.register_stabilize(local_log_softmax, name = 'local_log_softmax')
opt
.
register_specialize
(
local_log_softmax
,
'fast_compile_gpu'
,
name
=
'local_log_softmax'
)
opt
.
register_specialize
(
local_log_softmax
,
'fast_compile_gpu'
,
name
=
'local_log_softmax'
)
...
...
theano/tests/test_flake8.py
浏览文件 @
70b5f2c1
...
@@ -88,7 +88,6 @@ whitelist_flake8 = [
...
@@ -88,7 +88,6 @@ whitelist_flake8 = [
"tensor/signal/conv.py"
,
"tensor/signal/conv.py"
,
"tensor/signal/tests/test_conv.py"
,
"tensor/signal/tests/test_conv.py"
,
"tensor/signal/tests/test_downsample.py"
,
"tensor/signal/tests/test_downsample.py"
,
"tensor/nnet/nnet.py"
,
"tensor/nnet/Conv3D.py"
,
"tensor/nnet/Conv3D.py"
,
"tensor/nnet/__init__.py"
,
"tensor/nnet/__init__.py"
,
"tensor/nnet/ConvTransp3D.py"
,
"tensor/nnet/ConvTransp3D.py"
,
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论