Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
d9735d34
提交
d9735d34
authored
10月 24, 2014
作者:
Frederic
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Better chooce.infer_shape error.
上级
f31a4118
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
81 行增加
和
27 行删除
+81
-27
basic.py
theano/tensor/basic.py
+20
-3
test_basic.py
theano/tensor/tests/test_basic.py
+61
-24
没有找到文件。
theano/tensor/basic.py
浏览文件 @
d9735d34
...
@@ -5198,6 +5198,8 @@ class Choose(Op):
...
@@ -5198,6 +5198,8 @@ class Choose(Op):
def
infer_shape
(
self
,
node
,
shapes
):
def
infer_shape
(
self
,
node
,
shapes
):
if
isinstance
(
node
.
inputs
[
1
],
TensorVariable
):
if
isinstance
(
node
.
inputs
[
1
],
TensorVariable
):
# We have padded node.inputs[0] to the right number of
# dimensions for the output
return
[(
shapes
[
0
])]
return
[(
shapes
[
0
])]
else
:
else
:
import
theano.typed_list
import
theano.typed_list
...
@@ -5214,13 +5216,28 @@ class Choose(Op):
...
@@ -5214,13 +5216,28 @@ class Choose(Op):
# import at the top as it would cause circular import.
# import at the top as it would cause circular import.
import
theano.typed_list
import
theano.typed_list
a
=
as_tensor_variable
(
a
)
a
=
as_tensor_variable
(
a
)
if
"int"
not
in
a
.
dtype
:
raise
TypeError
(
'choose first argument must have an [u]int* dtype. Got
%
s.'
%
a
.
dtype
)
if
isinstance
(
choices
,
(
tuple
,
list
)):
if
isinstance
(
choices
,
(
tuple
,
list
)):
choice
=
theano
.
typed_list
.
make_list
(
choices
)
choice
=
theano
.
typed_list
.
make_list
(
choices
)
dtype
=
choice
.
ttype
.
dtype
choice_ndim
=
choice
.
ttype
.
ndim
choice_bcast
=
choice
.
ttype
.
broadcastable
else
:
else
:
choice
=
as_tensor_variable
(
choices
)
choice
=
as_tensor_variable
(
choices
)
o
=
TensorType
(
choice
.
dtype
,
a
.
broadcastable
)
choice_ndim
=
choice
.
ndim
-
1
choice_bcast
=
choice
.
broadcastable
[
1
:]
out_ndim
=
numpy
.
max
([
a
.
ndim
,
choice_ndim
])
a
=
shape_padleft
(
a
,
out_ndim
-
a
.
ndim
)
bcast
=
[
False
]
*
out_ndim
for
idx
,
(
b1
,
b2
)
in
enumerate
(
zip
(
a
.
broadcastable
,
(
True
,)
*
(
out_ndim
-
choice_ndim
)
+
choice_bcast
)):
if
b1
and
b2
:
bcast
[
idx
]
=
True
o
=
TensorType
(
choice
.
dtype
,
bcast
)
return
Apply
(
self
,
[
a
,
choice
],
[
o
()])
return
Apply
(
self
,
[
a
,
choice
],
[
o
()])
def
perform
(
self
,
node
,
inputs
,
(
z
,
)):
def
perform
(
self
,
node
,
inputs
,
(
z
,
)):
...
...
theano/tensor/tests/test_basic.py
浏览文件 @
d9735d34
...
@@ -7045,6 +7045,7 @@ class T_Power(unittest.TestCase):
...
@@ -7045,6 +7045,7 @@ class T_Power(unittest.TestCase):
class
T_Choose
(
utt
.
InferShapeTester
):
class
T_Choose
(
utt
.
InferShapeTester
):
op
=
staticmethod
(
choose
)
op
=
staticmethod
(
choose
)
op_class
=
Choose
op_class
=
Choose
modes
=
[
'raise'
,
'wrap'
,
'clip'
]
def
test_numpy_compare
(
self
):
def
test_numpy_compare
(
self
):
...
@@ -7055,14 +7056,44 @@ class T_Choose(utt.InferShapeTester):
...
@@ -7055,14 +7056,44 @@ class T_Choose(utt.InferShapeTester):
dtype
=
'int32'
)
dtype
=
'int32'
)
B
=
numpy
.
asarray
(
numpy
.
random
.
rand
(
4
,
4
),
dtype
=
'float32'
)
B
=
numpy
.
asarray
(
numpy
.
random
.
rand
(
4
,
4
),
dtype
=
'float32'
)
modes
=
[
'raise'
,
'wrap'
,
'clip'
]
for
m
in
self
.
modes
:
f
=
function
([
a
,
b
],
choose
(
a
,
b
,
mode
=
m
))
t_c
=
f
(
A
,
B
)
n_c
=
numpy
.
choose
(
A
,
B
,
mode
=
m
)
assert
numpy
.
allclose
(
t_c
,
n_c
)
for
m
in
modes
:
def
test_broadcasted
(
self
):
a
=
tensor
.
scalar
(
dtype
=
'int32'
)
b
=
tensor
.
matrix
(
dtype
=
'float32'
)
# Test when a is broadcastable
A
=
3
B
=
numpy
.
asarray
(
numpy
.
random
.
rand
(
4
,
4
),
dtype
=
'float32'
)
for
m
in
self
.
modes
:
f
=
function
([
a
,
b
],
choose
(
a
,
b
,
mode
=
m
))
f
=
function
([
a
,
b
],
choose
(
a
,
b
,
mode
=
m
))
t_c
=
f
(
A
,
B
)
t_c
=
f
(
A
,
B
)
n_c
=
numpy
.
choose
(
A
,
B
,
mode
=
m
)
n_c
=
numpy
.
choose
(
A
,
B
,
mode
=
m
)
assert
numpy
.
allclose
(
t_c
,
n_c
)
assert
numpy
.
allclose
(
t_c
,
n_c
)
# Test when the result should be broadcastable
b
=
theano
.
tensor
.
col
(
dtype
=
'float32'
)
B
=
numpy
.
asarray
(
numpy
.
random
.
rand
(
4
,
1
),
dtype
=
'float32'
)
for
m
in
self
.
modes
:
f
=
function
([
a
,
b
],
choose
(
a
,
b
,
mode
=
m
))
assert
choose
(
a
,
b
,
mode
=
m
)
.
broadcastable
[
0
]
t_c
=
f
(
A
,
B
)
n_c
=
numpy
.
choose
(
A
,
B
,
mode
=
m
)
assert
numpy
.
allclose
(
t_c
,
n_c
)
def
test_dtype_error
(
self
):
a
=
tensor
.
scalar
(
dtype
=
'float32'
)
b
=
tensor
.
matrix
(
dtype
=
'float32'
)
A
=
3
B
=
numpy
.
asarray
(
numpy
.
random
.
rand
(
4
,
4
),
dtype
=
'float32'
)
self
.
assertRaises
(
TypeError
,
choose
,
a
,
b
)
def
test_numpy_compare_tuple
(
self
):
def
test_numpy_compare_tuple
(
self
):
a
=
tensor
.
tensor3
(
dtype
=
'int32'
)
a
=
tensor
.
tensor3
(
dtype
=
'int32'
)
...
@@ -7074,34 +7105,40 @@ class T_Choose(utt.InferShapeTester):
...
@@ -7074,34 +7105,40 @@ class T_Choose(utt.InferShapeTester):
B
=
numpy
.
asarray
(
numpy
.
random
.
rand
(
1
,
6
,
1
),
dtype
=
'float32'
)
B
=
numpy
.
asarray
(
numpy
.
random
.
rand
(
1
,
6
,
1
),
dtype
=
'float32'
)
C
=
numpy
.
asarray
(
numpy
.
random
.
rand
(
1
,
1
,
5
),
dtype
=
'float32'
)
C
=
numpy
.
asarray
(
numpy
.
random
.
rand
(
1
,
1
,
5
),
dtype
=
'float32'
)
f
=
function
([
a
,
b
,
c
],
choose
(
a
,
(
b
,
c
)))
for
m
in
self
.
modes
:
f
=
function
([
a
,
b
,
c
],
choose
(
a
,
(
b
,
c
),
mode
=
m
))
t_c
=
f
(
A
,
B
,
C
)
t_c
=
f
(
A
,
B
,
C
)
n_c
=
numpy
.
choose
(
A
,
(
B
,
C
)
)
n_c
=
numpy
.
choose
(
A
,
(
B
,
C
),
mode
=
m
)
assert
numpy
.
allclose
(
t_c
,
n_c
)
assert
numpy
.
allclose
(
t_c
,
n_c
)
def
test_infer_shape
(
self
):
def
test_infer_shape
(
self
):
for
shp1
,
shp2
in
[
a
=
tensor
.
matrix
(
dtype
=
'int32'
)
((
5
,
4
),
(
7
,
4
)),
b
=
tensor
.
vector
(
dtype
=
'int32'
)
((
4
,),
(
4
,)),
c
=
tensor
.
matrix
(
dtype
=
'int32'
)
((
5
,
4
),
(
4
,)),
d
=
tensor
.
vector
(
dtype
=
'int32'
)
((
4
,),
(
5
,
4
)),
A
=
numpy
.
asarray
(
numpy
.
random
.
rand
(
5
,
4
)
*
4
,
dtype
=
'int32'
)
((
1
,
4
),
(
7
,
4
)),
B
=
numpy
.
asarray
(
numpy
.
random
.
rand
(
4
)
*
4
,
dtype
=
'int32'
)
((
1
,),
(
4
,)),
C
=
numpy
.
asarray
(
numpy
.
random
.
rand
(
7
,
4
)
*
4
,
dtype
=
'int32'
)
((
1
,
4
),
(
4
,)),
D
=
numpy
.
asarray
(
numpy
.
random
.
rand
(
4
)
*
4
,
dtype
=
'int32'
)
# The following case cause error from NumPy.
# ((5, 4), (1, 4)),
var1
=
[
a
,
b
,
a
,
b
]
# ((1,), (1,)),
var2
=
[
c
,
d
,
b
,
a
]
# ((4,), (1,)),
mat1
=
[
A
,
B
,
A
,
B
]
# ((4,), (1, 4)),
mat2
=
[
C
,
D
,
B
,
A
]
# ((4,), (3, 1)),
]:
for
v
,
m
,
w
,
n
in
zip
(
var1
,
mat1
,
var2
,
mat2
):
a
=
tensor
.
tensor
(
dtype
=
'int32'
,
self
.
_compile_and_check
([
v
,
w
],
# theano.function inputs
broadcastable
=
[
n
==
1
for
n
in
shp1
])
[
self
.
op
(
v
,
w
)],
# theano.function outputs
c
=
tensor
.
tensor
(
dtype
=
'float32'
,
broadcastable
=
[
n
==
1
for
n
in
shp2
])
A
=
numpy
.
asarray
(
numpy
.
random
.
rand
(
*
shp1
)
*
4
,
dtype
=
'int32'
)
C
=
numpy
.
asarray
(
numpy
.
random
.
rand
(
*
shp2
)
*
4
,
dtype
=
'float32'
)
self
.
_compile_and_check
([
a
,
c
],
# theano.function inputs
[
self
.
op
(
a
,
c
)],
# theano.function outputs
# Always use not square matrix!
# Always use not square matrix!
# inputs data
# inputs data
[
m
,
n
],
[
A
,
C
],
# Op that should be removed from the graph.
# Op that should be removed from the graph.
self
.
op_class
)
self
.
op_class
)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论