Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
180777ee
提交
180777ee
authored
7月 20, 2012
作者:
Eric Larsen
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
testing infer_shape: OP MakeVector
上级
9cc19f69
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
100 行增加
和
65 行删除
+100
-65
opt.py
theano/tensor/opt.py
+3
-0
test_opt.py
theano/tensor/tests/test_opt.py
+97
-65
没有找到文件。
theano/tensor/opt.py
浏览文件 @
180777ee
...
@@ -533,6 +533,9 @@ class MakeVector(T.Op):
...
@@ -533,6 +533,9 @@ class MakeVector(T.Op):
# assume that out has correct dtype. there is no cheap way to check
# assume that out has correct dtype. there is no cheap way to check
out
[
0
][
...
]
=
inputs
out
[
0
][
...
]
=
inputs
def
infer_shape
(
self
,
node
,
ishapes
):
return
[(
len
(
ishapes
),)]
def
grad
(
self
,
inputs
,
output_gradients
):
def
grad
(
self
,
inputs
,
output_gradients
):
# If the output is of an integer dtype, no gradient shall pass
# If the output is of an integer dtype, no gradient shall pass
if
'int'
in
self
.
dtype
:
if
'int'
in
self
.
dtype
:
...
...
theano/tensor/tests/test_opt.py
浏览文件 @
180777ee
...
@@ -29,7 +29,8 @@ from theano.tensor.opt import (
...
@@ -29,7 +29,8 @@ from theano.tensor.opt import (
mul_canonizer
,
mul_canonizer
,
out2in
,
out2in
,
Shape_i
,
Shape_i
,
Assert
Assert
,
MakeVector
)
)
from
theano
import
tensor
from
theano
import
tensor
from
theano
import
tensor
as
T
from
theano
import
tensor
as
T
...
@@ -3386,18 +3387,23 @@ class T_local_sum_dimshuffle(unittest.TestCase):
...
@@ -3386,18 +3387,23 @@ class T_local_sum_dimshuffle(unittest.TestCase):
# test_local_sum_divprod_dimshuffle ((a * b) / (c * d))
# test_local_sum_divprod_dimshuffle ((a * b) / (c * d))
def
test_make_vector
():
class
TestMakeVector
(
utt
.
InferShapeTester
):
b
=
T
.
bscalar
()
i
=
T
.
iscalar
()
d
=
T
.
dscalar
()
#TODO: draw random values instead. Not really important.
def
setUp
(
self
):
val
=
{
b
:
2
,
super
(
TestMakeVector
,
self
)
.
setUp
()
i
:
-
3
,
d
:
0.7
}
def
test_make_vector
():
b
=
T
.
bscalar
()
i
=
T
.
iscalar
()
d
=
T
.
dscalar
()
#TODO: draw random values instead. Not really important.
val
=
{
b
:
2
,
i
:
-
3
,
d
:
0.7
}
# Should work
# Should work
for
(
dtype
,
inputs
)
in
[(
"int8"
,
(
b
,
b
)),
for
(
dtype
,
inputs
)
in
[(
"int8"
,
(
b
,
b
)),
(
"int32"
,
(
i
,
b
)),
(
"int32"
,
(
i
,
b
)),
(
"int32"
,
(
b
,
i
)),
(
"int32"
,
(
b
,
i
)),
(
"float64"
,
(
b
,
i
)),
(
"float64"
,
(
b
,
i
)),
...
@@ -3406,55 +3412,55 @@ def test_make_vector():
...
@@ -3406,55 +3412,55 @@ def test_make_vector():
(
"float64"
,
()),
(
"float64"
,
()),
(
"int64"
,
()),
(
"int64"
,
()),
]:
]:
mv
=
opt
.
MakeVector
(
dtype
=
dtype
)(
*
inputs
)
mv
=
opt
.
MakeVector
(
dtype
=
dtype
)(
*
inputs
)
assert
mv
.
dtype
==
dtype
assert
mv
.
dtype
==
dtype
f
=
theano
.
function
([
b
,
i
,
d
],
mv
,
on_unused_input
=
'ignore'
)
f
=
theano
.
function
([
b
,
i
,
d
],
mv
,
on_unused_input
=
'ignore'
)
f_val
=
f
(
val
[
b
],
val
[
i
],
val
[
d
])
f_val
=
f
(
val
[
b
],
val
[
i
],
val
[
d
])
#print 'f_val =', f_val
#print 'f_val =', f_val
s
=
mv
.
sum
()
s
=
mv
.
sum
()
gb
=
T
.
grad
(
s
,
b
,
disconnected_inputs
=
'ignore'
)
gb
=
T
.
grad
(
s
,
b
,
disconnected_inputs
=
'ignore'
)
gi
=
T
.
grad
(
s
,
i
,
disconnected_inputs
=
'ignore'
)
gi
=
T
.
grad
(
s
,
i
,
disconnected_inputs
=
'ignore'
)
gd
=
T
.
grad
(
s
,
d
,
disconnected_inputs
=
'ignore'
)
gd
=
T
.
grad
(
s
,
d
,
disconnected_inputs
=
'ignore'
)
#print 'gb =', gb
#print 'gb =', gb
#print 'gi =', gi
#print 'gi =', gi
#print 'gd =', gd
#print 'gd =', gd
g
=
theano
.
function
([
b
,
i
,
d
],
[
gb
,
gi
,
gd
])
g
=
theano
.
function
([
b
,
i
,
d
],
[
gb
,
gi
,
gd
])
g_val
=
g
(
val
[
b
],
val
[
i
],
val
[
d
])
g_val
=
g
(
val
[
b
],
val
[
i
],
val
[
d
])
#print 'g_val =', g_val
#print 'g_val =', g_val
if
dtype
.
startswith
(
'int'
):
if
dtype
.
startswith
(
'int'
):
# The gradient should be 0
# The gradient should be 0
assert
numpy
.
allclose
(
g_val
,
0
)
assert
numpy
.
allclose
(
g_val
,
0
)
else
:
else
:
for
var
,
grval
in
zip
((
b
,
i
,
d
),
g_val
):
for
var
,
grval
in
zip
((
b
,
i
,
d
),
g_val
):
float_inputs
=
[]
float_inputs
=
[]
if
var
.
dtype
.
startswith
(
'int'
):
if
var
.
dtype
.
startswith
(
'int'
):
assert
grval
==
0
assert
grval
==
0
elif
var
not
in
inputs
:
elif
var
not
in
inputs
:
assert
grval
==
0
assert
grval
==
0
else
:
else
:
float_inputs
.
append
(
var
)
float_inputs
.
append
(
var
)
# Build a function that takes float_inputs, use fix values for the
# Build a function that takes float_inputs, use fix values for the
# other inputs, and returns the MakeVector. Use it for verify_grad.
# other inputs, and returns the MakeVector. Use it for verify_grad.
if
float_inputs
:
if
float_inputs
:
def
fun
(
*
fl_inputs
):
def
fun
(
*
fl_inputs
):
f_inputs
=
[]
f_inputs
=
[]
for
var
in
f_inputs
:
for
var
in
f_inputs
:
if
var
in
fl_inputs
:
if
var
in
fl_inputs
:
# use symbolic variable
# use symbolic variable
f_inputs
.
append
(
var
)
f_inputs
.
append
(
var
)
else
:
else
:
# use constant value
# use constant value
f_inputs
.
append
(
val
[
var
])
f_inputs
.
append
(
val
[
var
])
return
opt
.
MakeVector
(
dtype
=
dtype
)(
*
f_inputs
)
return
opt
.
MakeVector
(
dtype
=
dtype
)(
*
f_inputs
)
utt
.
verify_grad
(
fun
,
[
val
[
ri
]
for
ri
in
float_inputs
])
utt
.
verify_grad
(
fun
,
[
val
[
ri
]
for
ri
in
float_inputs
])
#should fail
#should fail
for
(
dtype
,
inputs
)
in
[(
"int8"
,
(
b
,
i
)),
for
(
dtype
,
inputs
)
in
[(
"int8"
,
(
b
,
i
)),
(
"int8"
,
(
i
,
b
)),
(
"int8"
,
(
i
,
b
)),
(
"int8"
,
(
b
,
d
)),
(
"int8"
,
(
b
,
d
)),
(
"int8"
,
(
i
,
i
)),
(
"int8"
,
(
i
,
i
)),
...
@@ -3462,11 +3468,37 @@ def test_make_vector():
...
@@ -3462,11 +3468,37 @@ def test_make_vector():
(
"int32"
,
(
i
,
d
)),
(
"int32"
,
(
i
,
d
)),
(
"float32"
,
(
i
,
d
)),
(
"float32"
,
(
i
,
d
)),
]:
]:
try
:
try
:
opt
.
MakeVector
(
dtype
=
dtype
)(
*
inputs
)
opt
.
MakeVector
(
dtype
=
dtype
)(
*
inputs
)
raise
Exception
(
"Theano should have raised an error"
)
raise
Exception
(
"Theano should have raised an error"
)
except
AssertionError
:
except
AssertionError
:
pass
pass
def
test_infer_shape
(
self
):
adscal
=
dscalar
()
bdscal
=
dscalar
()
aiscal
=
iscalar
()
biscal
=
iscalar
()
ciscal
=
iscalar
()
discal
=
iscalar
()
adscal_val
=
numpy
.
random
.
rand
()
bdscal_val
=
numpy
.
random
.
rand
()
aiscal_val
=
numpy
.
random
.
randint
(
10
)
biscal_val
=
numpy
.
random
.
randint
(
10
)
ciscal_val
=
numpy
.
random
.
randint
(
10
)
discal_val
=
numpy
.
random
.
randint
(
10
)
self
.
_compile_and_check
([
adscal
,
aiscal
],
[
MakeVector
(
'float64'
)(
adscal
,
aiscal
)],
[
adscal_val
,
aiscal_val
],
MakeVector
)
self
.
_compile_and_check
([
adscal
,
bdscal
,
aiscal
],
[
MakeVector
(
'float64'
)(
adscal
,
bdscal
,
aiscal
)],
[
adscal_val
,
bdscal_val
,
aiscal_val
],
MakeVector
)
self
.
_compile_and_check
([
aiscal
,
biscal
,
ciscal
,
discal
],
[
MakeVector
(
'int32'
)(
aiscal
,
biscal
,
ciscal
,
discal
)],
[
aiscal_val
,
biscal_val
,
ciscal_val
,
discal_val
],
MakeVector
)
def
test_local_join_1
():
def
test_local_join_1
():
...
@@ -3684,9 +3716,9 @@ class TestShape_i(utt.InferShapeTester):
...
@@ -3684,9 +3716,9 @@ class TestShape_i(utt.InferShapeTester):
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
t
=
Test
Shape_i
(
'setUp'
)
t
=
Test
MakeVector
(
'setUp'
)
t
.
setUp
()
t
.
setUp
()
t
.
test_perform
()
#
t.test_perform()
t
.
test_infer_shape
()
t
.
test_infer_shape
()
"""
"""
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论