Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
a33d82f6
提交
a33d82f6
authored
5月 22, 2017
作者:
Adam Becker
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
random fixes
上级
2d3baf8f
显示空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
16 行增加
和
64 行删除
+16
-64
sort.py
theano/gpuarray/sort.py
+3
-3
sort.py
theano/tensor/sort.py
+11
-11
test_sort.py
theano/tensor/tests/test_sort.py
+1
-49
unittest_tools.py
theano/tests/unittest_tools.py
+1
-1
没有找到文件。
theano/gpuarray/sort.py
浏览文件 @
a33d82f6
...
@@ -259,10 +259,10 @@ class GpuTopKOp(GpuKernelBase, TopKOp):
...
@@ -259,10 +259,10 @@ class GpuTopKOp(GpuKernelBase, TopKOp):
'''
'''
return
code
%
locals
()
return
code
%
locals
()
def
make_node
(
self
,
inp
,
k
):
def
make_node
(
self
,
inp
,
k
th
):
ctx_name
=
infer_context_name
(
inp
)
ctx_name
=
infer_context_name
(
inp
)
inp
=
as_gpuarray_variable
(
inp
,
ctx_name
)
inp
=
as_gpuarray_variable
(
inp
,
ctx_name
)
k
=
as_tensor_variable
(
k
)
k
th
=
as_tensor_variable
(
kth
)
bcast
=
inp
.
type
.
broadcastable
bcast
=
inp
.
type
.
broadcastable
outs
=
[]
outs
=
[]
if
self
.
return_values
:
if
self
.
return_values
:
...
@@ -272,7 +272,7 @@ class GpuTopKOp(GpuKernelBase, TopKOp):
...
@@ -272,7 +272,7 @@ class GpuTopKOp(GpuKernelBase, TopKOp):
dtype
=
self
.
idx_dtype
,
dtype
=
self
.
idx_dtype
,
broadcastable
=
bcast
,
broadcastable
=
bcast
,
context_name
=
ctx_name
)())
context_name
=
ctx_name
)())
return
Apply
(
self
,
[
inp
,
k
],
outs
)
return
Apply
(
self
,
[
inp
,
k
th
],
outs
)
def
get_params
(
self
,
node
):
def
get_params
(
self
,
node
):
return
node
.
inputs
[
0
]
.
type
.
context
return
node
.
inputs
[
0
]
.
type
.
context
...
...
theano/tensor/sort.py
浏览文件 @
a33d82f6
...
@@ -357,7 +357,7 @@ class TopKOp(theano.Op):
...
@@ -357,7 +357,7 @@ class TopKOp(theano.Op):
return
'
%(op)
s{axis=
%(axis)
d}'
%
dict
(
return
'
%(op)
s{axis=
%(axis)
d}'
%
dict
(
op
=
self
.
__class__
.
__name__
,
axis
=
self
.
axis
)
op
=
self
.
__class__
.
__name__
,
axis
=
self
.
axis
)
def
make_node
(
self
,
inp
,
k
):
def
make_node
(
self
,
inp
,
k
th
):
# numpy always uses int64 as output dtype for arg*() routines
# numpy always uses int64 as output dtype for arg*() routines
# however, we add this option as memory is more precious on gpu
# however, we add this option as memory is more precious on gpu
inp
=
theano
.
tensor
.
as_tensor_variable
(
inp
)
inp
=
theano
.
tensor
.
as_tensor_variable
(
inp
)
...
@@ -369,7 +369,7 @@ class TopKOp(theano.Op):
...
@@ -369,7 +369,7 @@ class TopKOp(theano.Op):
'"axis" parameter out of range,'
'"axis" parameter out of range,'
' expected integer within [
%
d,
%
d]'
%
(
-
ndim
,
ndim
-
1
))
' expected integer within [
%
d,
%
d]'
%
(
-
ndim
,
ndim
-
1
))
k
=
theano
.
tensor
.
as_tensor_variable
(
k
)
k
th
=
theano
.
tensor
.
as_tensor_variable
(
kth
)
bcast
=
inp
.
type
.
broadcastable
bcast
=
inp
.
type
.
broadcastable
outs
=
[]
outs
=
[]
if
self
.
return_values
:
if
self
.
return_values
:
...
@@ -377,7 +377,7 @@ class TopKOp(theano.Op):
...
@@ -377,7 +377,7 @@ class TopKOp(theano.Op):
if
self
.
return_indices
:
if
self
.
return_indices
:
outs
.
append
(
theano
.
tensor
.
TensorType
(
outs
.
append
(
theano
.
tensor
.
TensorType
(
dtype
=
self
.
idx_dtype
,
broadcastable
=
bcast
)())
dtype
=
self
.
idx_dtype
,
broadcastable
=
bcast
)())
return
theano
.
Apply
(
self
,
[
inp
,
k
],
outs
)
return
theano
.
Apply
(
self
,
[
inp
,
k
th
],
outs
)
def
perform
(
self
,
node
,
inputs
,
output_storage
):
def
perform
(
self
,
node
,
inputs
,
output_storage
):
x
,
k
=
inputs
x
,
k
=
inputs
...
@@ -428,7 +428,7 @@ class TopKOp(theano.Op):
...
@@ -428,7 +428,7 @@ class TopKOp(theano.Op):
return
[
x_grad
,
k_grad
]
return
[
x_grad
,
k_grad
]
def
topk
(
x
,
k
,
axis
=-
1
,
idx_dtype
=
'int64'
):
def
topk
(
x
,
k
th
,
axis
=-
1
,
idx_dtype
=
'int64'
):
"""
"""
Returns the k-largest elements along an axis.
Returns the k-largest elements along an axis.
...
@@ -437,7 +437,7 @@ def topk(x, k, axis=-1, idx_dtype='int64'):
...
@@ -437,7 +437,7 @@ def topk(x, k, axis=-1, idx_dtype='int64'):
x: tensor instance
x: tensor instance
k: integer constant/variable
k
th
: integer constant/variable
Must not be 0. If negative, gives k-smallest elements instead.
Must not be 0. If negative, gives k-smallest elements instead.
axis: integer or ``None``
axis: integer or ``None``
...
@@ -460,10 +460,10 @@ def topk(x, k, axis=-1, idx_dtype='int64'):
...
@@ -460,10 +460,10 @@ def topk(x, k, axis=-1, idx_dtype='int64'):
if
axis
is
None
:
if
axis
is
None
:
x
=
theano
.
tensor
.
flatten
(
x
)
x
=
theano
.
tensor
.
flatten
(
x
)
axis
=
-
1
axis
=
-
1
return
TopKOp
(
axis
=
axis
,
idx_dtype
=
idx_dtype
)(
x
,
k
)[
0
]
return
TopKOp
(
axis
=
axis
,
idx_dtype
=
idx_dtype
)(
x
,
k
th
)[
0
]
def
argtopk
(
x
,
k
,
axis
=-
1
,
idx_dtype
=
'int64'
):
def
argtopk
(
x
,
k
th
,
axis
=-
1
,
idx_dtype
=
'int64'
):
"""
"""
Returns the indices of k-largest elements along an axis.
Returns the indices of k-largest elements along an axis.
...
@@ -472,7 +472,7 @@ def argtopk(x, k, axis=-1, idx_dtype='int64'):
...
@@ -472,7 +472,7 @@ def argtopk(x, k, axis=-1, idx_dtype='int64'):
x: tensor instance
x: tensor instance
k: integer constant/variable
k
th
: integer constant/variable
Must not be 0. If negative, gives k-smallest elements instead.
Must not be 0. If negative, gives k-smallest elements instead.
axis: integer, tuple/list of integers, or ``None``
axis: integer, tuple/list of integers, or ``None``
...
@@ -499,10 +499,10 @@ def argtopk(x, k, axis=-1, idx_dtype='int64'):
...
@@ -499,10 +499,10 @@ def argtopk(x, k, axis=-1, idx_dtype='int64'):
axis
=
-
1
axis
=
-
1
return
TopKOp
(
return
TopKOp
(
axis
=
axis
,
axis
=
axis
,
idx_dtype
=
idx_dtype
)(
x
,
k
)[
1
]
idx_dtype
=
idx_dtype
)(
x
,
k
th
)[
1
]
def
topk_and_argtopk
(
x
,
k
,
axis
=-
1
,
idx_dtype
=
'int64'
):
def
topk_and_argtopk
(
x
,
k
th
,
axis
=-
1
,
idx_dtype
=
'int64'
):
"""
"""
Returns the results of both topk() and argtopk() in one Op.
Returns the results of both topk() and argtopk() in one Op.
...
@@ -518,4 +518,4 @@ def topk_and_argtopk(x, k, axis=-1, idx_dtype='int64'):
...
@@ -518,4 +518,4 @@ def topk_and_argtopk(x, k, axis=-1, idx_dtype='int64'):
axis
=
-
1
axis
=
-
1
return
TopKOp
(
return
TopKOp
(
axis
=
axis
,
axis
=
axis
,
idx_dtype
=
idx_dtype
)(
x
,
k
)
idx_dtype
=
idx_dtype
)(
x
,
k
th
)
theano/tensor/tests/test_sort.py
浏览文件 @
a33d82f6
...
@@ -22,7 +22,6 @@ def gen_unique_vector(size, dtype):
...
@@ -22,7 +22,6 @@ def gen_unique_vector(size, dtype):
return
(
retval
[
np
.
random
.
permutation
(
size
)]
-
size
*
1.5
)
.
astype
(
dtype
)
return
(
retval
[
np
.
random
.
permutation
(
size
)]
-
size
*
1.5
)
.
astype
(
dtype
)
'''
class
Test_sort
(
unittest
.
TestCase
):
class
Test_sort
(
unittest
.
TestCase
):
def
setUp
(
self
):
def
setUp
(
self
):
...
@@ -230,7 +229,6 @@ def test_argsort_grad():
...
@@ -230,7 +229,6 @@ def test_argsort_grad():
data
=
np
.
random
.
rand
(
2
,
3
,
3
)
.
astype
(
theano
.
config
.
floatX
)
data
=
np
.
random
.
rand
(
2
,
3
,
3
)
.
astype
(
theano
.
config
.
floatX
)
utt
.
verify_grad
(
lambda
x
:
argsort
(
x
,
axis
=
2
),
[
data
])
utt
.
verify_grad
(
lambda
x
:
argsort
(
x
,
axis
=
2
),
[
data
])
'''
class
Test_TopK
(
unittest
.
TestCase
):
class
Test_TopK
(
unittest
.
TestCase
):
...
@@ -408,59 +406,13 @@ class Test_TopK(unittest.TestCase):
...
@@ -408,59 +406,13 @@ class Test_TopK(unittest.TestCase):
continue
continue
def
op
(
x
):
def
op
(
x
):
return
theano
.
ggtensor
.
sort
(
topk
(
x
,
k
=
k
,
axis
=
axis
),
axis
=
axis
)
return
theano
.
tensor
.
sort
(
topk
(
x
,
k
,
axis
=
axis
),
axis
=
axis
)
xval
=
np
.
random
.
rand
(
*
shp
)
.
astype
(
theano
.
config
.
floatX
)
xval
=
np
.
random
.
rand
(
*
shp
)
.
astype
(
theano
.
config
.
floatX
)
utt
.
verify_grad
(
op
,
[
xval
])
utt
.
verify_grad
(
op
,
[
xval
])
class
TopKInferShapeTester
(
utt
.
InferShapeTester
):
class
TopKInferShapeTester
(
utt
.
InferShapeTester
):
@utt.parameterized.expand
(
product
(
((
15
,
17
),
(
11
,
7
,
5
),
(
2
,
3
,
5
,
7
,
11
),
(
2
,
4
,
3
,
1
)),
(
1
,
'(1+n)//2'
,
'n-1'
,
'-n'
)))
def
test_topk_infer_shape
(
self
,
shp
,
k_
):
ndim
=
len
(
shp
)
for
axis
in
range
(
-
ndim
,
ndim
):
if
isinstance
(
k_
,
str
):
k
=
eval
(
k_
.
replace
(
'n'
,
str
(
shp
[
axis
])))
else
:
k
=
k_
if
k
==
0
:
continue
x
=
theano
.
tensor
.
tensor
(
name
=
'x'
,
broadcastable
=
(
False
,)
*
len
(
shp
),
dtype
=
theano
.
config
.
floatX
)
y
=
topk
(
x
,
k
,
axis
=
axis
)
size
=
reduce
(
int
.
__mul__
,
shp
)
xval
=
gen_unique_vector
(
size
,
theano
.
config
.
floatX
)
.
reshape
(
shp
)
self
.
_compile_and_check
(
[
x
],
[
y
],
[
xval
],
TopKOp
)
@utt.parameterized.expand
(
product
(
((
15
,
17
),
(
11
,
7
,
5
),
(
2
,
3
,
5
,
7
,
11
),
(
2
,
4
,
3
,
1
)),
(
-
1
,
'(1+n)//2'
,
'1-n'
)))
def
test_argtopk_infer_shape
(
self
,
shp
,
k_
):
ndim
=
len
(
shp
)
for
axis
in
range
(
-
ndim
,
ndim
):
if
isinstance
(
k_
,
str
):
k
=
eval
(
k_
.
replace
(
'n'
,
str
(
shp
[
axis
])))
else
:
k
=
k_
if
k
==
0
:
continue
x
=
theano
.
tensor
.
tensor
(
name
=
'x'
,
broadcastable
=
(
False
,)
*
len
(
shp
),
dtype
=
theano
.
config
.
floatX
)
y
=
argtopk
(
x
,
k
,
axis
=
axis
,
idx_dtype
=
'int32'
)
size
=
reduce
(
int
.
__mul__
,
shp
)
xval
=
gen_unique_vector
(
size
,
theano
.
config
.
floatX
)
.
reshape
(
shp
)
self
.
_compile_and_check
(
[
x
],
[
y
],
[
xval
],
TopKOp
)
@utt.parameterized.expand
(
product
(
@utt.parameterized.expand
(
product
(
((
2
,
3
),
(
15
,
17
),
(
11
,
7
,
5
),
(
2
,
3
,
5
,
7
,
11
),
(
2
,
4
,
3
,
1
)),
((
2
,
3
),
(
15
,
17
),
(
11
,
7
,
5
),
(
2
,
3
,
5
,
7
,
11
),
(
2
,
4
,
3
,
1
)),
(
1
,
'(1+n)//2'
,
'n-1'
,
'n'
)))
(
1
,
'(1+n)//2'
,
'n-1'
,
'n'
)))
...
...
theano/tests/unittest_tools.py
浏览文件 @
a33d82f6
...
@@ -83,7 +83,7 @@ def seed_rng(pseed=None):
...
@@ -83,7 +83,7 @@ def seed_rng(pseed=None):
def
verify_grad
(
op
,
pt
,
n_tests
=
2
,
rng
=
None
,
*
args
,
**
kwargs
):
def
verify_grad
(
op
,
pt
,
n_tests
=
2
,
rng
=
None
,
*
args
,
**
kwargs
):
"""
"""
Wrapper for
tensor/basic
.py:verify_grad
Wrapper for
gradient
.py:verify_grad
Takes care of seeding the random number generator if None is given
Takes care of seeding the random number generator if None is given
"""
"""
if
rng
is
None
:
if
rng
is
None
:
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论