Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
9973e963
提交
9973e963
authored
6月 16, 2015
作者:
Frédéric Bastien
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #3009 from harlouci/trunk
Class Unique
上级
6c3d8e26
4f968fb0
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
166 行增加
和
4 行删除
+166
-4
extra_ops.py
theano/tensor/extra_ops.py
+63
-2
test_extra_ops.py
theano/tensor/tests/test_extra_ops.py
+103
-2
没有找到文件。
theano/tensor/extra_ops.py
浏览文件 @
9973e963
import
numpy
as
np
import
numpy
as
np
import
numpy
import
numpy
import
warnings
import
warnings
import
theano
import
theano
from
theano.tensor
import
basic
from
theano.tensor
import
basic
from
theano.tensor
import
nlinalg
from
theano.tensor
import
nlinalg
from
theano
import
gof
,
scalar
from
theano
import
gof
,
scalar
from
theano.gradient
import
DisconnectedType
from
theano.gradient
import
DisconnectedType
tensor
=
basic
tensor
=
basic
...
@@ -1006,3 +1005,65 @@ def to_one_hot(y, nb_class, dtype=None):
...
@@ -1006,3 +1005,65 @@ def to_one_hot(y, nb_class, dtype=None):
ret
=
theano
.
tensor
.
set_subtensor
(
ret
[
theano
.
tensor
.
arange
(
y
.
shape
[
0
]),
y
],
ret
=
theano
.
tensor
.
set_subtensor
(
ret
[
theano
.
tensor
.
arange
(
y
.
shape
[
0
]),
y
],
1
)
1
)
return
ret
return
ret
class
Unique
(
theano
.
Op
):
"""
Wraps numpy.unique.
This op is not implemented on the GPU.
"""
__props__
=
(
"return_index"
,
"return_inverse"
,
"return_counts"
)
def
__init__
(
self
,
return_index
=
False
,
return_inverse
=
False
,
return_counts
=
False
):
self
.
return_index
=
return_index
self
.
return_inverse
=
return_inverse
self
.
return_counts
=
return_counts
numpy_ver
=
[
int
(
n
)
for
n
in
numpy
.
__version__
.
split
(
'.'
)[:
2
]]
if
self
.
return_counts
==
True
and
bool
(
numpy_ver
<
[
1
,
9
])
:
raise
RuntimeError
(
"Numpy version = "
+
np
.
__version__
+
". Option 'return_counts=True' works starting"
" from version 1.9.0."
)
def
make_node
(
self
,
x
):
x
=
basic
.
as_tensor_variable
(
x
)
outputs
=
[
basic
.
TensorType
(
broadcastable
=
[
False
],
dtype
=
x
.
dtype
)()]
typ
=
basic
.
TensorType
(
broadcastable
=
[
False
],
dtype
=
'int64'
)
if
self
.
return_index
:
outputs
.
append
(
typ
())
if
self
.
return_inverse
:
outputs
.
append
(
typ
())
if
self
.
return_counts
:
outputs
.
append
(
typ
())
return
theano
.
Apply
(
self
,
[
x
],
outputs
)
def
perform
(
self
,
node
,
inputs
,
output_storage
):
x
=
inputs
[
0
]
z
=
output_storage
param
=
{}
if
self
.
return_index
:
param
[
'return_index'
]
=
True
if
self
.
return_inverse
:
param
[
'return_inverse'
]
=
True
if
self
.
return_counts
:
param
[
'return_counts'
]
=
True
outs
=
np
.
unique
(
x
,
**
param
)
if
((
not
self
.
return_inverse
)
and
(
not
self
.
return_index
)
and
(
not
self
.
return_counts
)):
z
[
0
][
0
]
=
outs
else
:
for
i
in
range
(
len
(
outs
)):
z
[
i
][
0
]
=
outs
[
i
]
def
infer_shape
(
self
,
node
,
i0_shapes
):
ret
=
node
.
fgraph
.
shape_feature
.
default_infer_shape
(
node
,
i0_shapes
)
if
self
.
return_inverse
:
shape
=
(
basic
.
prod
(
i0_shapes
[
0
]),
)
if
self
.
return_index
:
ret
[
2
]
=
shape
return
ret
ret
[
1
]
=
shape
return
ret
return
ret
theano/tensor/tests/test_extra_ops.py
浏览文件 @
9973e963
...
@@ -5,14 +5,13 @@ import unittest
...
@@ -5,14 +5,13 @@ import unittest
import
theano
import
theano
from
theano.tests
import
unittest_tools
as
utt
from
theano.tests
import
unittest_tools
as
utt
from
theano.tensor.extra_ops
import
(
CumsumOp
,
cumsum
,
CumprodOp
,
cumprod
,
from
theano.tensor.extra_ops
import
(
CumsumOp
,
cumsum
,
CumprodOp
,
cumprod
,
BinCountOp
,
bincount
,
DiffOp
,
diff
,
BinCountOp
,
bincount
,
DiffOp
,
diff
,
squeeze
,
compress
,
RepeatOp
,
repeat
,
squeeze
,
compress
,
RepeatOp
,
repeat
,
Bartlett
,
bartlett
,
Bartlett
,
bartlett
,
FillDiagonal
,
fill_diagonal
,
FillDiagonal
,
fill_diagonal
,
FillDiagonalOffset
,
fill_diagonal_offset
,
FillDiagonalOffset
,
fill_diagonal_offset
,
to_one_hot
)
to_one_hot
,
Unique
)
from
theano
import
tensor
as
T
from
theano
import
tensor
as
T
from
theano
import
config
,
tensor
,
function
from
theano
import
config
,
tensor
,
function
...
@@ -661,3 +660,105 @@ def test_to_one_hot():
...
@@ -661,3 +660,105 @@ def test_to_one_hot():
[
0.
,
0.
,
0.
,
1.
,
0.
,
0.
,
0.
,
0.
,
0.
,
0.
],
[
0.
,
0.
,
0.
,
1.
,
0.
,
0.
,
0.
,
0.
,
0.
,
0.
],
[
0.
,
0.
,
0.
,
0.
,
0.
,
1.
,
0.
,
0.
,
0.
,
0.
],
[
0.
,
0.
,
0.
,
0.
,
0.
,
1.
,
0.
,
0.
,
0.
,
0.
],
[
0.
,
0.
,
0.
,
0.
,
0.
,
0.
,
1.
,
0.
,
0.
,
0.
]])
[
0.
,
0.
,
0.
,
0.
,
0.
,
0.
,
1.
,
0.
,
0.
,
0.
]])
class
test_Unique
(
utt
.
InferShapeTester
):
def
setUp
(
self
):
super
(
test_Unique
,
self
)
.
setUp
()
self
.
op_class
=
Unique
self
.
ops
=
[
Unique
(),
Unique
(
True
),
Unique
(
False
,
True
),
Unique
(
True
,
True
)]
if
bool
(
numpy_ver
>=
[
1
,
9
])
:
self
.
ops
.
extend
([
Unique
(
False
,
False
,
True
),
Unique
(
True
,
False
,
True
),
Unique
(
False
,
True
,
True
),
Unique
(
True
,
True
,
True
)])
def
test_basic_vector
(
self
):
"""
Basic test for a vector.
Done by using the op and checking that it returns the right answer.
"""
x
=
theano
.
tensor
.
vector
()
inp
=
np
.
asarray
([
2
,
1
,
3
,
2
],
dtype
=
config
.
floatX
)
list_outs_expected
=
[[
np
.
unique
(
inp
)],
np
.
unique
(
inp
,
True
),
np
.
unique
(
inp
,
False
,
True
),
np
.
unique
(
inp
,
True
,
True
)]
if
bool
(
numpy_ver
>=
[
1
,
9
])
:
list_outs_expected
.
extend
([
np
.
unique
(
inp
,
False
,
False
,
True
),
np
.
unique
(
inp
,
True
,
False
,
True
),
np
.
unique
(
inp
,
False
,
True
,
True
),
np
.
unique
(
inp
,
True
,
True
,
True
)])
for
op
,
outs_expected
in
zip
(
self
.
ops
,
list_outs_expected
)
:
f
=
theano
.
function
(
inputs
=
[
x
],
outputs
=
op
(
x
,
return_list
=
True
))
outs
=
f
(
inp
)
# Compare the result computed to the expected value.
for
out
,
out_exp
in
zip
(
outs
,
outs_expected
):
utt
.
assert_allclose
(
out
,
out_exp
)
def
test_basic_matrix
(
self
):
""" Basic test for a matrix.
Done by using the op and checking that it returns the right answer.
"""
x
=
theano
.
tensor
.
matrix
()
inp
=
np
.
asarray
([[
2
,
1
],
[
3
,
2
],
[
2
,
3
]],
dtype
=
config
.
floatX
)
list_outs_expected
=
[[
np
.
unique
(
inp
)],
np
.
unique
(
inp
,
True
),
np
.
unique
(
inp
,
False
,
True
),
np
.
unique
(
inp
,
True
,
True
)]
if
bool
(
numpy_ver
>=
[
1
,
9
])
:
list_outs_expected
.
extend
([
np
.
unique
(
inp
,
False
,
False
,
True
),
np
.
unique
(
inp
,
True
,
False
,
True
),
np
.
unique
(
inp
,
False
,
True
,
True
),
np
.
unique
(
inp
,
True
,
True
,
True
)])
for
op
,
outs_expected
in
zip
(
self
.
ops
,
list_outs_expected
):
f
=
theano
.
function
(
inputs
=
[
x
],
outputs
=
op
(
x
,
return_list
=
True
))
outs
=
f
(
inp
)
# Compare the result computed to the expected value.
for
out
,
out_exp
in
zip
(
outs
,
outs_expected
):
utt
.
assert_allclose
(
out
,
out_exp
)
def
test_infer_shape_vector
(
self
):
"""
Testing the infer_shape with a vector.
"""
x
=
theano
.
tensor
.
vector
()
for
op
in
self
.
ops
:
if
not
op
.
return_inverse
:
continue
if
op
.
return_index
:
f
=
op
(
x
)[
2
]
else
:
f
=
op
(
x
)[
1
]
self
.
_compile_and_check
([
x
],
[
f
],
[
np
.
asarray
(
np
.
array
([
2
,
1
,
3
,
2
]),
dtype
=
config
.
floatX
)],
self
.
op_class
)
def
test_infer_shape_matrix
(
self
):
"""
Testing the infer_shape with a matrix.
"""
x
=
theano
.
tensor
.
matrix
()
for
op
in
self
.
ops
:
if
not
op
.
return_inverse
:
continue
if
op
.
return_index
:
f
=
op
(
x
)[
2
]
else
:
f
=
op
(
x
)[
1
]
self
.
_compile_and_check
([
x
],
[
f
],
[
np
.
asarray
(
np
.
array
([[
2
,
1
],
[
3
,
2
],[
2
,
3
]]),
dtype
=
config
.
floatX
)],
self
.
op_class
)
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论