Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
ceb31338
提交
ceb31338
authored
4月 19, 2012
作者:
Nicolas Pinto
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
MISC: start refactoring basic.py into smaller pieces
上级
bc0c3b4e
隐藏空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
317 行增加
和
292 行删除
+317
-292
__init__.py
theano/tensor/__init__.py
+2
-0
basic.py
theano/tensor/basic.py
+0
-161
sort.py
theano/tensor/sort.py
+167
-0
test_basic.py
theano/tensor/tests/test_basic.py
+2
-131
test_sort.py
theano/tensor/tests/test_sort.py
+146
-0
没有找到文件。
theano/tensor/__init__.py
浏览文件 @
ceb31338
...
...
@@ -52,3 +52,5 @@ import nnet # used for softmax, sigmoid, etc.
from
theano.gradient
import
Rop
,
Lop
,
grad
,
numeric_grad
,
verify_grad
,
\
jacobian
,
hessian
from
sort
import
sort
theano/tensor/basic.py
浏览文件 @
ceb31338
...
...
@@ -6267,164 +6267,3 @@ def any(x, axis=None):
def
all
(
x
,
axis
=
None
):
return
elemwise
.
All
(
axis
)(
x
)
class
SortOp
(
theano
.
Op
):
"""
This class is a wrapper for numpy sort function
"""
def
__init__
(
self
,
kind
,
order
=
None
):
self
.
kind
=
kind
self
.
order
=
order
def
__eq__
(
self
,
other
):
return
(
type
(
self
)
==
type
(
other
)
and
self
.
order
==
other
.
order
and
self
.
kind
==
other
.
kind
)
def
__hash__
(
self
):
return
hash
(
type
(
self
))
^
hash
(
self
.
order
)
^
hash
(
self
.
kind
)
def
__str__
(
self
):
return
self
.
__class__
.
__name__
+
"{
%
s,
%
s}"
%
(
self
.
kind
,
str
(
self
.
order
))
def
make_node
(
self
,
input
,
axis
=-
1
):
input
=
theano
.
tensor
.
as_tensor_variable
(
input
)
if
axis
is
None
:
axis
=
Constant
(
gof
.
generic
,
None
)
# axis=None flattens the array before sorting
out_type
=
tensor
(
dtype
=
input
.
dtype
,
broadcastable
=
[
False
])
else
:
axis
=
theano
.
tensor
.
as_tensor_variable
(
axis
)
out_type
=
input
.
type
()
return
theano
.
Apply
(
self
,
[
input
,
axis
],
[
out_type
])
def
perform
(
self
,
node
,
inputs
,
output_storage
):
a
=
inputs
[
0
]
axis
=
inputs
[
1
]
z
=
output_storage
[
0
]
z
[
0
]
=
numpy
.
sort
(
a
,
axis
,
self
.
kind
,
self
.
order
)
def
infer_shape
(
self
,
node
,
inputs_shapes
):
if
(
isinstance
(
node
.
inputs
[
1
],
Constant
)
and
node
.
inputs
[
1
]
.
data
is
None
):
# That means axis = None,
# So the array is flattened before being sorted
return
[(
mul
(
*
inputs_shapes
[
0
]),)]
# axis should not be None
# So there should be the same number of dimensions
# in the input and output
assert
node
.
inputs
[
0
]
.
ndim
==
node
.
outputs
[
0
]
.
ndim
assert
inputs_shapes
[
1
]
==
()
return
[
inputs_shapes
[
0
]]
#**** It need the argsort, so we can't do it now.
#def grad(self, inputs, output_grads):
"""
def R_op(self, inputs, eval_points):
# R_op can receive None as eval_points.
# That mean there is no diferientiable path through that input
# If this imply that you cannot compute some outputs,
# return None for those.
if eval_points[0] is None:
return eval_points
return self.grad(inputs, eval_points)
"""
def
sort
(
a
,
axis
=-
1
,
kind
=
'quicksort'
,
order
=
None
):
"""
Return a sorted copy of an array.
a : Tensor
Tensor to be sorted
axis : Tensor
Axis along which to sort. If None, the array is
flattened before sorting.
kind : {'quicksort', 'mergesort', 'heapsort'}, optional
Sorting algorithm. Default is 'quicksort'.
order : list, optional
When `a` is a structured array, this argument specifies which
fields to compare first, second, and so on. This list does not
need to include all of the fields.
"""
return
SortOp
(
kind
,
order
)(
a
,
axis
)
class
ArgSortOp
(
theano
.
Op
):
"""
This class is a wrapper for numpy argsort function
"""
def
__init__
(
self
,
kind
,
order
=
None
):
self
.
kind
=
kind
self
.
order
=
order
def
__eq__
(
self
,
other
):
return
(
type
(
self
)
==
type
(
other
)
and
self
.
order
==
other
.
order
and
self
.
kind
==
other
.
kind
)
def
__hash__
(
self
):
return
hash
(
type
(
self
))
^
hash
(
self
.
order
)
^
hash
(
self
.
kind
)
def
__str__
(
self
):
return
(
self
.
__class__
.
__name__
+
"{
%
s,
%
s}"
%
(
self
.
kind
,
str
(
self
.
order
)))
def
make_node
(
self
,
input
,
axis
=-
1
):
input
=
theano
.
tensor
.
as_tensor_variable
(
input
)
if
axis
is
None
:
axis
=
Constant
(
gof
.
generic
,
None
)
bcast
=
[
False
]
else
:
axis
=
theano
.
tensor
.
as_tensor_variable
(
axis
)
bcast
=
input
.
type
.
broadcastable
return
theano
.
Apply
(
self
,
[
input
,
axis
],
[
theano
.
tensor
.
TensorType
(
dtype
=
"int64"
,
broadcastable
=
bcast
)()])
def
perform
(
self
,
node
,
inputs
,
output_storage
):
a
=
inputs
[
0
]
axis
=
inputs
[
1
]
z
=
output_storage
[
0
]
z
[
0
]
=
numpy
.
argsort
(
a
,
axis
,
self
.
kind
,
self
.
order
)
def
infer_shape
(
self
,
node
,
inputs_shapes
):
if
(
isinstance
(
node
.
inputs
[
1
],
Constant
)
and
node
.
inputs
[
1
]
.
data
is
None
):
return
[(
mul
(
*
inputs_shapes
[
0
]),)]
# axis should not be None, so there should be the same number of
# dimensions in the input and output
assert
node
.
inputs
[
0
]
.
ndim
==
node
.
outputs
[
0
]
.
ndim
assert
inputs_shapes
[
1
]
==
()
return
[
inputs_shapes
[
0
]]
def
grad
(
self
,
inputs
,
output_grads
):
#No grad defined for intergers.
return
[
None
,
None
]
"""
def R_op(self, inputs, eval_points):
# R_op can receive None as eval_points.
# That mean there is no diferientiable path through that input
# If this imply that you cannot compute some outputs,
# return None for those.
if eval_points[0] is None:
return eval_points
return self.grad(inputs, eval_points)
"""
def
argsort
(
a
,
axis
=-
1
,
kind
=
'quicksort'
,
order
=
None
):
"""
Returns the indices that would sort an array.
Perform an indirect sort along the given axis using the algorithm
specified by the kind keyword. It returns an array of indices of
the same shape as a that index data along the given axis in sorted
order.
"""
return
ArgSortOp
(
kind
,
order
)(
a
,
axis
)
theano/tensor/sort.py
0 → 100644
浏览文件 @
ceb31338
import
numpy
as
np
import
theano
from
theano.tensor
import
tensor
from
basic
import
mul
class
SortOp
(
theano
.
Op
):
"""
This class is a wrapper for numpy sort function
"""
def
__init__
(
self
,
kind
,
order
=
None
):
self
.
kind
=
kind
self
.
order
=
order
def
__eq__
(
self
,
other
):
return
(
type
(
self
)
==
type
(
other
)
and
self
.
order
==
other
.
order
and
self
.
kind
==
other
.
kind
)
def
__hash__
(
self
):
return
hash
(
type
(
self
))
^
hash
(
self
.
order
)
^
hash
(
self
.
kind
)
def
__str__
(
self
):
return
self
.
__class__
.
__name__
+
"{
%
s,
%
s}"
%
(
self
.
kind
,
str
(
self
.
order
))
def
make_node
(
self
,
input
,
axis
=-
1
):
input
=
theano
.
tensor
.
as_tensor_variable
(
input
)
if
axis
is
None
:
axis
=
theano
.
Constant
(
theano
.
gof
.
generic
,
None
)
# axis=None flattens the array before sorting
out_type
=
tensor
(
dtype
=
input
.
dtype
,
broadcastable
=
[
False
])
else
:
axis
=
theano
.
tensor
.
as_tensor_variable
(
axis
)
out_type
=
input
.
type
()
return
theano
.
Apply
(
self
,
[
input
,
axis
],
[
out_type
])
def
perform
(
self
,
node
,
inputs
,
output_storage
):
a
=
inputs
[
0
]
axis
=
inputs
[
1
]
z
=
output_storage
[
0
]
z
[
0
]
=
np
.
sort
(
a
,
axis
,
self
.
kind
,
self
.
order
)
def
infer_shape
(
self
,
node
,
inputs_shapes
):
if
(
isinstance
(
node
.
inputs
[
1
],
theano
.
Constant
)
and
node
.
inputs
[
1
]
.
data
is
None
):
# That means axis = None,
# So the array is flattened before being sorted
return
[(
mul
(
*
inputs_shapes
[
0
]),)]
# axis should not be None
# So there should be the same number of dimensions
# in the input and output
assert
node
.
inputs
[
0
]
.
ndim
==
node
.
outputs
[
0
]
.
ndim
assert
inputs_shapes
[
1
]
==
()
return
[
inputs_shapes
[
0
]]
#**** It need the argsort, so we can't do it now.
#def grad(self, inputs, output_grads):
"""
def R_op(self, inputs, eval_points):
# R_op can receive None as eval_points.
# That mean there is no diferientiable path through that input
# If this imply that you cannot compute some outputs,
# return None for those.
if eval_points[0] is None:
return eval_points
return self.grad(inputs, eval_points)
"""
def
sort
(
a
,
axis
=-
1
,
kind
=
'quicksort'
,
order
=
None
):
"""
Return a sorted copy of an array.
a : Tensor
Tensor to be sorted
axis : Tensor
Axis along which to sort. If None, the array is
flattened before sorting.
kind : {'quicksort', 'mergesort', 'heapsort'}, optional
Sorting algorithm. Default is 'quicksort'.
order : list, optional
When `a` is a structured array, this argument specifies which
fields to compare first, second, and so on. This list does not
need to include all of the fields.
"""
return
SortOp
(
kind
,
order
)(
a
,
axis
)
class
ArgSortOp
(
theano
.
Op
):
"""
This class is a wrapper for numpy argsort function
"""
def
__init__
(
self
,
kind
,
order
=
None
):
self
.
kind
=
kind
self
.
order
=
order
def
__eq__
(
self
,
other
):
return
(
type
(
self
)
==
type
(
other
)
and
self
.
order
==
other
.
order
and
self
.
kind
==
other
.
kind
)
def
__hash__
(
self
):
return
hash
(
type
(
self
))
^
hash
(
self
.
order
)
^
hash
(
self
.
kind
)
def
__str__
(
self
):
return
(
self
.
__class__
.
__name__
+
"{
%
s,
%
s}"
%
(
self
.
kind
,
str
(
self
.
order
)))
def
make_node
(
self
,
input
,
axis
=-
1
):
input
=
theano
.
tensor
.
as_tensor_variable
(
input
)
if
axis
is
None
:
axis
=
theano
.
Constant
(
theano
.
gof
.
generic
,
None
)
bcast
=
[
False
]
else
:
axis
=
theano
.
tensor
.
as_tensor_variable
(
axis
)
bcast
=
input
.
type
.
broadcastable
return
theano
.
Apply
(
self
,
[
input
,
axis
],
[
theano
.
tensor
.
TensorType
(
dtype
=
"int64"
,
broadcastable
=
bcast
)()])
def
perform
(
self
,
node
,
inputs
,
output_storage
):
a
=
inputs
[
0
]
axis
=
inputs
[
1
]
z
=
output_storage
[
0
]
z
[
0
]
=
np
.
argsort
(
a
,
axis
,
self
.
kind
,
self
.
order
)
def
infer_shape
(
self
,
node
,
inputs_shapes
):
if
(
isinstance
(
node
.
inputs
[
1
],
theano
.
Constant
)
and
node
.
inputs
[
1
]
.
data
is
None
):
return
[(
mul
(
*
inputs_shapes
[
0
]),)]
# axis should not be None, so there should be the same number of
# dimensions in the input and output
assert
node
.
inputs
[
0
]
.
ndim
==
node
.
outputs
[
0
]
.
ndim
assert
inputs_shapes
[
1
]
==
()
return
[
inputs_shapes
[
0
]]
def
grad
(
self
,
inputs
,
output_grads
):
#No grad defined for intergers.
return
[
None
,
None
]
"""
def R_op(self, inputs, eval_points):
# R_op can receive None as eval_points.
# That mean there is no diferientiable path through that input
# If this imply that you cannot compute some outputs,
# return None for those.
if eval_points[0] is None:
return eval_points
return self.grad(inputs, eval_points)
"""
def
argsort
(
a
,
axis
=-
1
,
kind
=
'quicksort'
,
order
=
None
):
"""
Returns the indices that would sort an array.
Perform an indirect sort along the given axis using the algorithm
specified by the kind keyword. It returns an array of indices of
the same shape as a that index data along the given axis in sorted
order.
"""
return
ArgSortOp
(
kind
,
order
)(
a
,
axis
)
theano/tensor/tests/test_basic.py
浏览文件 @
ceb31338
...
...
@@ -24,7 +24,7 @@ from theano.tensor import (_shared, wvector, bvector, autocast_float_as,
horizontal_stack
,
vertical_stack
,
argmax
,
get_vector_length
,
fscalar
,
zeros_like
,
sum
,
tensor3
,
vector
,
izip
,
add
,
addbroadcast
,
alloc
,
as_tensor_variable
,
tensor_from_scalar
,
ARange
,
autocast_float
,
basic
,
clip
,
constant
,
default
,
dot
,
inc_subtensor
,
set_subtensor
,
clip
,
constant
,
default
,
dot
,
inc_subtensor
,
set_subtensor
,
dmatrix
,
dscalar
,
dvector
,
eq
,
eye
,
fill
,
flatten
,
inverse_permutation
,
tensor4
,
permute_row_elements
,
Flatten
,
fmatrix
,
fscalars
,
grad
,
inplace
,
iscalar
,
matrix
,
minimum
,
matrices
,
maximum
,
mul
,
neq
,
...
...
@@ -34,7 +34,7 @@ from theano.tensor import (_shared, wvector, bvector, autocast_float_as,
get_constant_value
,
ivector
,
reshape
,
scalar_from_tensor
,
scal
,
iscalars
,
arange
,
dscalars
,
fvector
,
imatrix
,
numeric_grad
,
opt
,
ComplexError
,
TensorDot
,
lvector
,
true_div
,
max
,
min
,
Split
,
roll
,
tile
,
patternbroadcast
,
sort
,
SortOp
,
argsort
,
ArgSortOp
,
)
tile
,
patternbroadcast
)
from
theano.tests
import
unittest_tools
as
utt
...
...
@@ -5663,135 +5663,6 @@ def test_transpose():
assert
numpy
.
all
(
t3d
==
numpy
.
transpose
(
x3v
,
[
0
,
2
,
1
]))
class
test_sort
(
unittest
.
TestCase
):
def
setUp
(
self
):
self
.
rng
=
numpy
.
random
.
RandomState
(
seed
=
utt
.
fetch_seed
())
self
.
m_val
=
self
.
rng
.
rand
(
3
,
2
)
self
.
v_val
=
self
.
rng
.
rand
(
4
)
def
test1
(
self
):
a
=
theano
.
tensor
.
dmatrix
()
w
=
sort
(
a
)
f
=
theano
.
function
([
a
],
w
)
assert
numpy
.
allclose
(
f
(
self
.
m_val
),
numpy
.
sort
(
self
.
m_val
))
def
test2
(
self
):
a
=
theano
.
tensor
.
dmatrix
()
axis
=
theano
.
tensor
.
scalar
()
w
=
sort
(
a
,
axis
)
f
=
theano
.
function
([
a
,
axis
],
w
)
for
axis_val
in
0
,
1
:
assert
numpy
.
allclose
(
f
(
self
.
m_val
,
axis_val
),
numpy
.
sort
(
self
.
m_val
,
axis_val
))
def
test3
(
self
):
a
=
theano
.
tensor
.
dvector
()
w2
=
sort
(
a
)
f
=
theano
.
function
([
a
],
w2
)
assert
numpy
.
allclose
(
f
(
self
.
v_val
),
numpy
.
sort
(
self
.
v_val
))
def
test4
(
self
):
a
=
theano
.
tensor
.
dmatrix
()
axis
=
theano
.
tensor
.
scalar
()
l
=
sort
(
a
,
axis
,
"mergesort"
)
f
=
theano
.
function
([
a
,
axis
],
l
)
for
axis_val
in
0
,
1
:
assert
numpy
.
allclose
(
f
(
self
.
m_val
,
axis_val
),
numpy
.
sort
(
self
.
m_val
,
axis_val
))
def
test5
(
self
):
a
=
theano
.
tensor
.
dmatrix
()
axis
=
theano
.
tensor
.
scalar
()
a1
=
SortOp
(
"mergesort"
,
[])
a2
=
SortOp
(
"quicksort"
,
[])
#All the below should give true
assert
a1
!=
a2
assert
a1
==
SortOp
(
"mergesort"
,
[])
assert
a2
==
SortOp
(
"quicksort"
,
[])
def
test_None
(
self
):
a
=
theano
.
tensor
.
dmatrix
()
l
=
sort
(
a
,
None
)
f
=
theano
.
function
([
a
],
l
)
assert
numpy
.
allclose
(
f
(
self
.
m_val
),
numpy
.
sort
(
self
.
m_val
,
None
))
class
TensorInferShapeTester
(
utt
.
InferShapeTester
):
def
test_sort
(
self
):
x
=
tensor
.
matrix
()
self
.
_compile_and_check
(
[
x
],
[
sort
(
x
)],
[
numpy
.
random
.
randn
(
10
,
40
)
.
astype
(
config
.
floatX
)],
SortOp
)
self
.
_compile_and_check
(
[
x
],
[
sort
(
x
,
axis
=
None
)],
[
numpy
.
random
.
randn
(
10
,
40
)
.
astype
(
config
.
floatX
)],
SortOp
)
def
test_argsort
():
#Set up
rng
=
numpy
.
random
.
RandomState
(
seed
=
utt
.
fetch_seed
())
m_val
=
rng
.
rand
(
3
,
2
)
v_val
=
rng
.
rand
(
4
)
#Example 1
a
=
theano
.
tensor
.
dmatrix
()
w
=
argsort
(
a
)
f
=
theano
.
function
([
a
],
w
)
assert
numpy
.
allclose
(
f
(
m_val
),
numpy
.
argsort
(
m_val
))
#Example 2
a
=
theano
.
tensor
.
dmatrix
()
axis
=
theano
.
tensor
.
scalar
()
w
=
argsort
(
a
,
axis
)
f
=
theano
.
function
([
a
,
axis
],
w
)
for
axis_val
in
0
,
1
:
assert
numpy
.
allclose
(
f
(
m_val
,
axis_val
),
numpy
.
argsort
(
m_val
,
axis_val
))
#Example 3
a
=
theano
.
tensor
.
dvector
()
w2
=
argsort
(
a
)
f
=
theano
.
function
([
a
],
w2
)
assert
numpy
.
allclose
(
f
(
v_val
),
numpy
.
argsort
(
v_val
))
#Example 4
a
=
theano
.
tensor
.
dmatrix
()
axis
=
theano
.
tensor
.
scalar
()
l
=
argsort
(
a
,
axis
,
"mergesort"
)
f
=
theano
.
function
([
a
,
axis
],
l
)
for
axis_val
in
0
,
1
:
assert
numpy
.
allclose
(
f
(
m_val
,
axis_val
),
numpy
.
argsort
(
m_val
,
axis_val
))
#Example 5
a
=
theano
.
tensor
.
dmatrix
()
axis
=
theano
.
tensor
.
scalar
()
a1
=
ArgSortOp
(
"mergesort"
,
[])
a2
=
ArgSortOp
(
"quicksort"
,
[])
#All the below should give true
assert
a1
!=
a2
assert
a1
==
ArgSortOp
(
"mergesort"
,
[])
assert
a2
==
ArgSortOp
(
"quicksort"
,
[])
#Example 6: Testing axis=None
a
=
theano
.
tensor
.
dmatrix
()
w2
=
argsort
(
a
,
None
)
f
=
theano
.
function
([
a
],
w2
)
assert
numpy
.
allclose
(
f
(
m_val
),
numpy
.
argsort
(
m_val
,
None
))
if
__name__
==
'__main__'
:
if
0
:
unittest
.
main
()
...
...
theano/tensor/tests/test_sort.py
0 → 100644
浏览文件 @
ceb31338
import
unittest
from
numpy.testing
import
assert_allclose
from
theano.tests
import
unittest_tools
as
utt
import
numpy
as
np
import
theano
from
theano
import
tensor
from
theano.tensor.sort
import
sort
,
SortOp
from
theano.tensor.sort
import
argsort
,
ArgSortOp
class
test_sort
(
unittest
.
TestCase
):
def
setUp
(
self
):
self
.
rng
=
np
.
random
.
RandomState
(
seed
=
utt
.
fetch_seed
())
self
.
m_val
=
self
.
rng
.
rand
(
3
,
2
)
self
.
v_val
=
self
.
rng
.
rand
(
4
)
def
test1
(
self
):
a
=
tensor
.
dmatrix
()
w
=
sort
(
a
)
f
=
theano
.
function
([
a
],
w
)
assert_allclose
(
f
(
self
.
m_val
),
np
.
sort
(
self
.
m_val
))
def
test2
(
self
):
a
=
tensor
.
dmatrix
()
axis
=
tensor
.
scalar
()
w
=
sort
(
a
,
axis
)
f
=
theano
.
function
([
a
,
axis
],
w
)
for
axis_val
in
0
,
1
:
gv
=
f
(
self
.
m_val
,
axis_val
)
gt
=
np
.
sort
(
self
.
m_val
,
axis_val
)
assert_allclose
(
gv
,
gt
)
def
test3
(
self
):
a
=
tensor
.
dvector
()
w2
=
sort
(
a
)
f
=
theano
.
function
([
a
],
w2
)
gv
=
f
(
self
.
v_val
)
gt
=
np
.
sort
(
self
.
v_val
)
assert_allclose
(
gv
,
gt
)
def
test4
(
self
):
a
=
tensor
.
dmatrix
()
axis
=
tensor
.
scalar
()
l
=
sort
(
a
,
axis
,
"mergesort"
)
f
=
theano
.
function
([
a
,
axis
],
l
)
for
axis_val
in
0
,
1
:
gv
=
f
(
self
.
m_val
,
axis_val
)
gt
=
np
.
sort
(
self
.
m_val
,
axis_val
)
assert_allclose
(
gv
,
gt
)
def
test5
(
self
):
a1
=
SortOp
(
"mergesort"
,
[])
a2
=
SortOp
(
"quicksort"
,
[])
#All the below should give true
assert
a1
!=
a2
assert
a1
==
SortOp
(
"mergesort"
,
[])
assert
a2
==
SortOp
(
"quicksort"
,
[])
def
test_None
(
self
):
a
=
tensor
.
dmatrix
()
l
=
sort
(
a
,
None
)
f
=
theano
.
function
([
a
],
l
)
gv
=
f
(
self
.
m_val
)
gt
=
np
.
sort
(
self
.
m_val
,
None
)
assert_allclose
(
gv
,
gt
)
class
TensorInferShapeTester
(
utt
.
InferShapeTester
):
def
test_sort
(
self
):
x
=
tensor
.
matrix
()
self
.
_compile_and_check
(
[
x
],
[
sort
(
x
)],
[
np
.
random
.
randn
(
10
,
40
)
.
astype
(
theano
.
config
.
floatX
)],
SortOp
)
self
.
_compile_and_check
(
[
x
],
[
sort
(
x
,
axis
=
None
)],
[
np
.
random
.
randn
(
10
,
40
)
.
astype
(
theano
.
config
.
floatX
)],
SortOp
)
def
test_argsort
():
#Set up
rng
=
np
.
random
.
RandomState
(
seed
=
utt
.
fetch_seed
())
m_val
=
rng
.
rand
(
3
,
2
)
v_val
=
rng
.
rand
(
4
)
#Example 1
a
=
tensor
.
dmatrix
()
w
=
argsort
(
a
)
f
=
theano
.
function
([
a
],
w
)
gv
=
f
(
m_val
)
gt
=
np
.
argsort
(
m_val
)
assert_allclose
(
gv
,
gt
)
#Example 2
a
=
tensor
.
dmatrix
()
axis
=
tensor
.
scalar
()
w
=
argsort
(
a
,
axis
)
f
=
theano
.
function
([
a
,
axis
],
w
)
for
axis_val
in
0
,
1
:
gv
=
f
(
m_val
,
axis_val
)
gt
=
np
.
argsort
(
m_val
,
axis_val
)
assert_allclose
(
gv
,
gt
)
#Example 3
a
=
tensor
.
dvector
()
w2
=
argsort
(
a
)
f
=
theano
.
function
([
a
],
w2
)
gv
=
f
(
v_val
)
gt
=
np
.
argsort
(
v_val
)
assert_allclose
(
gv
,
gt
)
#Example 4
a
=
tensor
.
dmatrix
()
axis
=
tensor
.
scalar
()
l
=
argsort
(
a
,
axis
,
"mergesort"
)
f
=
theano
.
function
([
a
,
axis
],
l
)
for
axis_val
in
0
,
1
:
gv
=
f
(
m_val
,
axis_val
)
gt
=
np
.
argsort
(
m_val
,
axis_val
)
assert_allclose
(
gv
,
gt
)
#Example 5
a
=
tensor
.
dmatrix
()
axis
=
tensor
.
scalar
()
a1
=
ArgSortOp
(
"mergesort"
,
[])
a2
=
ArgSortOp
(
"quicksort"
,
[])
#All the below should give true
assert
a1
!=
a2
assert
a1
==
ArgSortOp
(
"mergesort"
,
[])
assert
a2
==
ArgSortOp
(
"quicksort"
,
[])
#Example 6: Testing axis=None
a
=
tensor
.
dmatrix
()
w2
=
argsort
(
a
,
None
)
f
=
theano
.
function
([
a
],
w2
)
gv
=
f
(
m_val
)
gt
=
np
.
argsort
(
m_val
,
None
)
assert_allclose
(
gv
,
gt
)
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论