Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
9d55e60f
提交
9d55e60f
authored
2月 19, 2010
作者:
James Bergstra
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Various modifs to make Xent tests pass with new ShapeFeature.
上级
c6fc7c59
显示空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
110 行增加
和
19 行删除
+110
-19
nnet.py
theano/tensor/nnet/nnet.py
+42
-13
test_nnet.py
theano/tensor/nnet/tests/test_nnet.py
+49
-4
opt.py
theano/tensor/opt.py
+19
-2
没有找到文件。
theano/tensor/nnet/nnet.py
浏览文件 @
9d55e60f
...
@@ -6,7 +6,7 @@
...
@@ -6,7 +6,7 @@
from
theano
import
gof
from
theano
import
gof
from
theano
import
printing
from
theano
import
printing
from
theano.tensor
import
basic
as
tensor
from
theano.tensor
import
basic
as
tensor
from
theano.tensor
import
elemwise
from
theano.tensor
import
elemwise
,
dmatrix
,
fmatrix
,
dvector
,
fvector
from
theano.tensor
import
opt
from
theano.tensor
import
opt
from
theano.compile
import
optdb
from
theano.compile
import
optdb
import
numpy
import
numpy
...
@@ -919,6 +919,15 @@ def _check_rows_is_arange_len_labels(rows, labels):
...
@@ -919,6 +919,15 @@ def _check_rows_is_arange_len_labels(rows, labels):
shape_of
=
stop
.
owner
.
env
.
shape_feature
.
shape_of
shape_of
=
stop
.
owner
.
env
.
shape_feature
.
shape_of
return
shape_of
[
labels
][
0
]
is
stop
return
shape_of
[
labels
][
0
]
is
stop
def
_is_const
(
z
,
val
,
approx
=
False
):
try
:
maybe
=
opt
.
get_constant_value
(
z
)
except
TypeError
:
return
False
if
approx
:
return
numpy
.
allclose
(
maybe
,
val
)
else
:
return
numpy
.
all
(
maybe
==
val
)
@opt.register_specialize
@opt.register_specialize
@gof.local_optimizer
([])
@gof.local_optimizer
([])
def
local_advanced_indexing_crossentropy_onehot
(
node
):
def
local_advanced_indexing_crossentropy_onehot
(
node
):
...
@@ -969,7 +978,7 @@ def local_advanced_indexing_crossentropy_onehot_grad(node):
...
@@ -969,7 +978,7 @@ def local_advanced_indexing_crossentropy_onehot_grad(node):
except
:
except
:
return
return
if
sm
is
not
None
and
sm
.
owner
and
sm
.
owner
.
op
in
(
softmax
,
softmax_with_bias
):
if
(
sm
is
not
None
)
and
sm
.
owner
and
(
sm
.
owner
.
op
in
(
softmax
,
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
:
assert
sm_w_bias
[
0
]
.
owner
.
op
==
softmax_with_bias
assert
sm_w_bias
[
0
]
.
owner
.
op
==
softmax_with_bias
...
@@ -1023,13 +1032,7 @@ def local_advanced_indexing_crossentropy_onehot_grad(node):
...
@@ -1023,13 +1032,7 @@ def local_advanced_indexing_crossentropy_onehot_grad(node):
return
return
# Check that z == zeros_like(softmax(x))
# Check that z == zeros_like(softmax(x))
if
z
.
owner
and
z
.
owner
.
op
==
tensor
.
fill
:
if
not
_is_const
(
z
,
0
):
model
,
value
=
z
.
owner
.
inputs
if
not
(
model
is
sm
and
hasattr
(
value
,
'data'
)
and
numpy
.
all
(
value
.
data
==
0
)):
return
#else: OK
else
:
return
return
# In the base case (output gradient = 1), incr is -1./sm[arange(len(y)), y]
# In the base case (output gradient = 1), incr is -1./sm[arange(len(y)), y]
...
@@ -1112,11 +1115,17 @@ def local_advanced_indexing_crossentropy_onehot_grad(node):
...
@@ -1112,11 +1115,17 @@ def local_advanced_indexing_crossentropy_onehot_grad(node):
# Second case
# Second case
elif
out_grad
.
owner
and
out_grad
.
owner
.
op
==
tensor
.
true_div
:
elif
out_grad
.
owner
and
out_grad
.
owner
.
op
==
tensor
.
true_div
:
# we know
# we're looking for
# AdvIncSubtensor(zeros, grad_nll, arange(len(y)), y) / softmax
try
:
try
:
num
,
denom
=
out_grad
.
owner
.
inputs
num
,
denom
=
out_grad
.
owner
.
inputs
except
:
except
:
return
return
if
denom
!=
sm
:
return
# Check the numerator (AdvancedIncSubtensor)
# Check the numerator (AdvancedIncSubtensor)
if
num
.
owner
and
isinstance
(
num
.
owner
.
op
,
tensor
.
AdvancedIncSubtensor
):
if
num
.
owner
and
isinstance
(
num
.
owner
.
op
,
tensor
.
AdvancedIncSubtensor
):
try
:
try
:
...
@@ -1125,6 +1134,14 @@ def local_advanced_indexing_crossentropy_onehot_grad(node):
...
@@ -1125,6 +1134,14 @@ def local_advanced_indexing_crossentropy_onehot_grad(node):
return
return
# Check z is zeros_like(log(sm))
# Check z is zeros_like(log(sm))
# JB - do we really care if this is zeros?
if
not
_is_const
(
z
,
0
):
return
if
z
.
type
not
in
(
dmatrix
,
fmatrix
):
return
# here we know that we are incrementing a matrix of zeros
if
0
:
if
z
.
owner
and
z
.
owner
.
op
==
tensor
.
fill
:
if
z
.
owner
and
z
.
owner
.
op
==
tensor
.
fill
:
model
,
value
=
z
.
owner
.
inputs
model
,
value
=
z
.
owner
.
inputs
...
@@ -1142,6 +1159,21 @@ def local_advanced_indexing_crossentropy_onehot_grad(node):
...
@@ -1142,6 +1159,21 @@ def local_advanced_indexing_crossentropy_onehot_grad(node):
else
:
else
:
return
return
if
incr
.
type
not
in
(
dvector
,
fvector
):
return
# here we know that we are incrementing some part of matrix z by a vector
# unless the user has taken care to mark that the data and labels have the
# same number of rows, we cannot be sure here that
# len(y) == len(z)
# However, in the common case that these are predictions and labels it is true.
# We leave it to the Op to crash (and the user to complain) if this assumption is
# ever not true.
outgrad_factor
=
None
if
0
:
# Check incr is ((-1.) like log(softmax(x))[arange(len(y)), y])
# Check incr is ((-1.) like log(softmax(x))[arange(len(y)), y])
if
incr
.
owner
and
incr
.
owner
.
op
==
tensor
.
fill
:
if
incr
.
owner
and
incr
.
owner
.
op
==
tensor
.
fill
:
model
,
value
=
incr
.
owner
.
inputs
model
,
value
=
incr
.
owner
.
inputs
...
@@ -1189,10 +1221,7 @@ def local_advanced_indexing_crossentropy_onehot_grad(node):
...
@@ -1189,10 +1221,7 @@ def local_advanced_indexing_crossentropy_onehot_grad(node):
return
return
# else, arguments of AdvancedIncSubtensor are OK
# else, arguments of AdvancedIncSubtensor are OK
return
[
crossentropy_softmax_1hot_with_bias_dx
(
-
incr
,
sm
,
labels
)]
# Check the denominator (sm)
if
not
denom
is
sm
:
return
# else, numerator and denominator are OK,
# else, numerator and denominator are OK,
# it was really case 2.
# it was really case 2.
...
...
theano/tensor/nnet/tests/test_nnet.py
浏览文件 @
9d55e60f
...
@@ -306,14 +306,22 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
...
@@ -306,14 +306,22 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
# Verify the optimizer worked on the expressions
# Verify the optimizer worked on the expressions
f
=
theano
.
function
([
x
,
y
],
expr
,
mode
=
mode
)
f
=
theano
.
function
([
x
,
y
],
expr
,
mode
=
mode
)
if
verbose
:
print_graph
(
f
)
if
verbose
:
print_graph
(
f
)
try
:
assert
len
(
f
.
maker
.
env
.
toposort
())
==
4
assert
len
(
f
.
maker
.
env
.
toposort
())
==
4
f
(
x_val
,
y_val
)
f
(
x_val
,
y_val
)
except
:
theano
.
printing
.
debugprint
(
f
)
raise
# Also verify the gradient wrt x
# Also verify the gradient wrt x
g
=
theano
.
function
([
x
,
y
],
T
.
grad
(
expr
,
x
),
mode
=
mode
)
g
=
theano
.
function
([
x
,
y
],
T
.
grad
(
expr
,
x
),
mode
=
mode
)
if
verbose
:
print_graph
(
g
)
if
verbose
:
print_graph
(
g
)
try
:
assert
len
(
g
.
maker
.
env
.
toposort
())
==
4
assert
len
(
g
.
maker
.
env
.
toposort
())
==
4
g
(
x_val
,
y_val
)
g
(
x_val
,
y_val
)
except
:
theano
.
printing
.
debugprint
(
g
)
raise
## Test that a biased softmax is optimized correctly
## Test that a biased softmax is optimized correctly
...
@@ -326,13 +334,21 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
...
@@ -326,13 +334,21 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
for
expr
in
bias_expressions
:
for
expr
in
bias_expressions
:
f
=
theano
.
function
([
x
,
b
,
y
],
expr
,
mode
=
mode
)
f
=
theano
.
function
([
x
,
b
,
y
],
expr
,
mode
=
mode
)
if
verbose
:
print_graph
(
f
)
if
verbose
:
print_graph
(
f
)
try
:
assert
len
(
f
.
maker
.
env
.
toposort
())
==
2
# [big_op, sum]
assert
len
(
f
.
maker
.
env
.
toposort
())
==
2
# [big_op, sum]
f
(
x_val
,
b_val
,
y_val
)
f
(
x_val
,
b_val
,
y_val
)
except
:
theano
.
printing
.
debugprint
(
f
)
raise
g
=
theano
.
function
([
x
,
b
,
y
],
T
.
grad
(
expr
,
x
),
mode
=
mode
)
g
=
theano
.
function
([
x
,
b
,
y
],
T
.
grad
(
expr
,
x
),
mode
=
mode
)
if
verbose
:
print_graph
(
g
)
if
verbose
:
print_graph
(
g
)
try
:
assert
len
(
g
.
maker
.
env
.
toposort
())
==
4
assert
len
(
g
.
maker
.
env
.
toposort
())
==
4
g
(
x_val
,
b_val
,
y_val
)
g
(
x_val
,
b_val
,
y_val
)
except
:
theano
.
printing
.
debugprint
(
g
)
raise
## Test that using "mean" instead of sum works, too
## Test that using "mean" instead of sum works, too
mean_expressions
=
[
mean_expressions
=
[
...
@@ -344,13 +360,22 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
...
@@ -344,13 +360,22 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
for
expr
in
mean_expressions
:
for
expr
in
mean_expressions
:
f
=
theano
.
function
([
x
,
y
],
expr
,
mode
=
mode
)
f
=
theano
.
function
([
x
,
y
],
expr
,
mode
=
mode
)
if
verbose
:
print_graph
(
f
)
if
verbose
:
print_graph
(
f
)
assert
len
(
f
.
maker
.
env
.
toposort
())
==
7
try
:
assert
len
(
f
.
maker
.
env
.
toposort
())
==
6
f
(
x_val
,
y_val
)
f
(
x_val
,
y_val
)
except
:
theano
.
printing
.
debugprint
(
f
)
raise
g
=
theano
.
function
([
x
,
y
],
T
.
grad
(
expr
,
x
),
mode
=
mode
)
g
=
theano
.
function
([
x
,
y
],
T
.
grad
(
expr
,
x
),
mode
=
mode
)
if
verbose
:
print_graph
(
g
)
if
verbose
:
print_graph
(
g
)
assert
len
(
g
.
maker
.
env
.
toposort
())
==
8
try
:
assert
len
(
g
.
maker
.
env
.
toposort
())
in
(
6
,
7
)
#there's an extra dimshuffle in there
# but I can't think of a good rule to get rid of it
g
(
x_val
,
y_val
)
g
(
x_val
,
y_val
)
except
:
theano
.
printing
.
debugprint
(
g
)
raise
mean_bias_expressions
=
[
mean_bias_expressions
=
[
T
.
mean
(
-
T
.
log
(
softmax
(
x
+
b
)[
T
.
arange
(
y
.
shape
[
0
]),
y
])),
T
.
mean
(
-
T
.
log
(
softmax
(
x
+
b
)[
T
.
arange
(
y
.
shape
[
0
]),
y
])),
...
@@ -361,12 +386,20 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
...
@@ -361,12 +386,20 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
for
expr
in
mean_bias_expressions
:
for
expr
in
mean_bias_expressions
:
f
=
theano
.
function
([
x
,
b
,
y
],
expr
,
mode
=
mode
)
f
=
theano
.
function
([
x
,
b
,
y
],
expr
,
mode
=
mode
)
if
verbose
:
print_graph
(
f
)
if
verbose
:
print_graph
(
f
)
assert
len
(
f
.
maker
.
env
.
toposort
())
==
5
try
:
assert
len
(
f
.
maker
.
env
.
toposort
())
==
4
except
:
theano
.
printing
.
debugprint
(
f
)
raise
g
=
theano
.
function
([
x
,
b
,
y
],
T
.
grad
(
expr
,
x
),
mode
=
mode
)
g
=
theano
.
function
([
x
,
b
,
y
],
T
.
grad
(
expr
,
x
),
mode
=
mode
)
if
verbose
:
print_graph
(
g
)
if
verbose
:
print_graph
(
g
)
assert
len
(
g
.
maker
.
env
.
toposort
())
==
8
try
:
assert
len
(
g
.
maker
.
env
.
toposort
())
in
(
6
,
7
)
g
(
x_val
,
b_val
,
y_val
)
g
(
x_val
,
b_val
,
y_val
)
except
:
theano
.
printing
.
debugprint
(
g
)
raise
def
test_scale_cost
(
self
):
def
test_scale_cost
(
self
):
...
@@ -450,21 +483,33 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
...
@@ -450,21 +483,33 @@ class T_CrossentropyCategorical1Hot(unittest.TestCase):
for
expr
in
expressions
:
for
expr
in
expressions
:
# Verify the optimizer worked on the expressions
# Verify the optimizer worked on the expressions
f
=
theano
.
function
([
x
,
y
,
a
],
expr
,
mode
=
mode
)
f
=
theano
.
function
([
x
,
y
,
a
],
expr
,
mode
=
mode
)
try
:
assert
5
<=
len
(
f
.
maker
.
env
.
toposort
())
<=
10
assert
5
<=
len
(
f
.
maker
.
env
.
toposort
())
<=
10
validate_fn_graph
(
f
)
validate_fn_graph
(
f
)
f
(
x_val
,
y_val
,
0.1
)
f
(
x_val
,
y_val
,
0.1
)
except
:
theano
.
printing
.
debugprint
(
f
)
raise
# Verify the gradient wrt x
# Verify the gradient wrt x
g
=
theano
.
function
([
x
,
y
,
a
],
T
.
grad
(
expr
,
x
),
mode
=
mode
)
g
=
theano
.
function
([
x
,
y
,
a
],
T
.
grad
(
expr
,
x
),
mode
=
mode
)
try
:
assert
5
<=
len
(
g
.
maker
.
env
.
toposort
())
<=
12
assert
5
<=
len
(
g
.
maker
.
env
.
toposort
())
<=
12
validate_grad_graph
(
g
)
validate_grad_graph
(
g
)
g
(
x_val
,
y_val
,
0.1
)
g
(
x_val
,
y_val
,
0.1
)
except
:
theano
.
printing
.
debugprint
(
g
)
raise
# Verify the gradient when providing output gradient
# Verify the gradient when providing output gradient
h
=
theano
.
function
([
x
,
y
,
a
],
T
.
grad
(
expr
,
x
,
g_cost
=
a
*
x
.
sum
()),
mode
=
mode
)
h
=
theano
.
function
([
x
,
y
,
a
],
T
.
grad
(
expr
,
x
,
g_cost
=
a
*
x
.
sum
()),
mode
=
mode
)
try
:
assert
8
<=
len
(
h
.
maker
.
env
.
toposort
())
<=
17
assert
8
<=
len
(
h
.
maker
.
env
.
toposort
())
<=
17
validate_grad_graph
(
h
)
validate_grad_graph
(
h
)
h
(
x_val
,
y_val
,
0.1
)
h
(
x_val
,
y_val
,
0.1
)
except
:
theano
.
printing
.
debugprint
(
h
)
raise
def
test_argmax_pushdown
():
def
test_argmax_pushdown
():
...
...
theano/tensor/opt.py
浏览文件 @
9d55e60f
...
@@ -80,9 +80,12 @@ def get_constant_value(v):
...
@@ -80,9 +80,12 @@ def get_constant_value(v):
return
v
.
data
return
v
.
data
except
:
except
:
raise
TypeError
(
v
)
raise
TypeError
(
v
)
if
v
.
owner
and
isinstance
(
v
.
owner
.
op
,
T
.
DimShuffle
):
if
v
.
owner
:
if
isinstance
(
v
.
owner
.
op
,
T
.
Alloc
):
return
get_constant_value
(
v
.
owner
.
inputs
[
0
])
return
get_constant_value
(
v
.
owner
.
inputs
[
0
])
if
v
.
owner
and
v
.
owner
.
op
==
T
.
fill
:
if
isinstance
(
v
.
owner
.
op
,
T
.
DimShuffle
):
return
get_constant_value
(
v
.
owner
.
inputs
[
0
])
if
v
.
owner
.
op
==
T
.
fill
:
shape
,
val
=
v
.
owner
.
inputs
shape
,
val
=
v
.
owner
.
inputs
# fill(a,b) fills the shape of 'a' filled with 'b'
# fill(a,b) fills the shape of 'a' filled with 'b'
return
get_constant_value
(
val
)
return
get_constant_value
(
val
)
...
@@ -530,6 +533,20 @@ def local_subtensor_make_vector(node):
...
@@ -530,6 +533,20 @@ def local_subtensor_make_vector(node):
_logger
.
error
(
'failed to index with "
%
s"'
%
str
(
idx
))
_logger
.
error
(
'failed to index with "
%
s"'
%
str
(
idx
))
raise
raise
@register_specialize
@gof.local_optimizer
([
T
.
Alloc
])
def
local_alloc_unary
(
node
):
"""unary(alloc(x, shp)) -> alloc(unary(x), shp)
"""
if
isinstance
(
node
.
op
,
T
.
Elemwise
)
and
len
(
node
.
inputs
)
==
1
:
x
=
node
.
inputs
[
0
]
if
x
.
owner
and
isinstance
(
x
.
owner
.
op
,
T
.
Alloc
):
return
[
T
.
Alloc
(
node
.
outputs
[
0
]
.
dtype
)(
node
.
op
(
T
.
cast
(
x
.
owner
.
inputs
[
0
],
x
.
dtype
)),
*
x
.
owner
.
inputs
[
1
:]
)]
##################
##################
# Subtensor opts #
# Subtensor opts #
##################
##################
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论