Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
73a6651f
提交
73a6651f
authored
3月 30, 2015
作者:
carriepl
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #2628 from aalmah/bincount-op
fixes #2580: theano function for bincount
上级
6a732dd7
df8e957d
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
86 行增加
和
17 行删除
+86
-17
extra_ops.py
theano/tensor/extra_ops.py
+46
-7
test_extra_ops.py
theano/tensor/tests/test_extra_ops.py
+40
-10
没有找到文件。
theano/tensor/extra_ops.py
浏览文件 @
73a6651f
import
numpy
as
np
import
numpy
as
np
import
numpy
import
numpy
import
warnings
import
theano
import
theano
from
theano.tensor
import
basic
from
theano.tensor
import
basic
...
@@ -332,8 +332,11 @@ def diff(x, n=1, axis=-1):
...
@@ -332,8 +332,11 @@ def diff(x, n=1, axis=-1):
class
BinCountOp
(
theano
.
Op
):
class
BinCountOp
(
theano
.
Op
):
# See function bincount for docstring
"""
DEPRECATED: use bincount() instead.
See function bincount for docstring
"""
compatible_type
=
(
'int8'
,
'int16'
,
'int32'
,
'int64'
,
compatible_type
=
(
'int8'
,
'int16'
,
'int32'
,
'int64'
,
'uint8'
,
'uint16'
,
'uint32'
,
'uint64'
)
'uint8'
,
'uint16'
,
'uint32'
,
'uint64'
)
"""Tuple of all compatible dtype for the parameter of this op."""
"""Tuple of all compatible dtype for the parameter of this op."""
...
@@ -355,6 +358,10 @@ class BinCountOp(theano.Op):
...
@@ -355,6 +358,10 @@ class BinCountOp(theano.Op):
return
hash
(
type
(
self
))
^
hash
(
self
.
minlength
)
return
hash
(
type
(
self
))
^
hash
(
self
.
minlength
)
def
make_node
(
self
,
x
,
weights
):
def
make_node
(
self
,
x
,
weights
):
warnings
.
warn
((
"Tile op is deprecated, use tile function instead."
),
stacklevel
=
3
)
x
=
basic
.
as_tensor_variable
(
x
)
x
=
basic
.
as_tensor_variable
(
x
)
if
x
.
dtype
not
in
BinCountOp
.
compatible_type
:
if
x
.
dtype
not
in
BinCountOp
.
compatible_type
:
...
@@ -429,8 +436,8 @@ class BinCountOp(theano.Op):
...
@@ -429,8 +436,8 @@ class BinCountOp(theano.Op):
return
self
.
__class__
.
__name__
return
self
.
__class__
.
__name__
def
bincount
(
x
,
weights
=
None
,
minlength
=
None
):
def
bincount
(
x
,
weights
=
None
,
minlength
=
None
,
assert_nonneg
=
False
):
"""Count number of occurrences of each value in array of
non-negative
ints.
"""Count number of occurrences of each value in array of ints.
The number of bins (of size 1) is one larger than the largest
The number of bins (of size 1) is one larger than the largest
value in x. If minlength is specified, there will be at least
value in x. If minlength is specified, there will be at least
...
@@ -439,7 +446,6 @@ def bincount(x, weights=None, minlength=None):
...
@@ -439,7 +446,6 @@ def bincount(x, weights=None, minlength=None):
number of occurrences of its index value in x. If weights is
number of occurrences of its index value in x. If weights is
specified the input array is weighted by it, i.e. if a value n
specified the input array is weighted by it, i.e. if a value n
is found at position i, out[n] += weight[i] instead of out[n] += 1.
is found at position i, out[n] += weight[i] instead of out[n] += 1.
Wraping of numpy.bincount
:param x: 1 dimension, nonnegative ints
:param x: 1 dimension, nonnegative ints
...
@@ -447,10 +453,43 @@ def bincount(x, weights=None, minlength=None):
...
@@ -447,10 +453,43 @@ def bincount(x, weights=None, minlength=None):
Optional.
Optional.
:param minlength: A minimum number of bins for the output array.
:param minlength: A minimum number of bins for the output array.
Optional.
Optional.
:param assert_nonneg: A flag that inserts an assert_op to check if
every input x is nonnegative.
Optional.
.. versionadded:: 0.6
.. versionadded:: 0.6
"""
"""
return
BinCountOp
(
minlength
=
minlength
)(
x
,
weights
)
compatible_type
=
(
'int8'
,
'int16'
,
'int32'
,
'int64'
,
'uint8'
,
'uint16'
,
'uint32'
)
unsupported_dtypes
=
(
'uint64'
,)
if
x
.
dtype
in
unsupported_dtypes
:
raise
TypeError
(
(
"Input dtype
%
s is not supported, "
%
unsupported_dtypes
),
x
.
dtype
)
if
x
.
dtype
not
in
compatible_type
:
raise
TypeError
(
"Inputs dtype must be an integer."
)
if
x
.
ndim
!=
1
:
raise
TypeError
(
"Inputs must be of dimension 1."
)
if
assert_nonneg
:
from
theano.tensor.opt
import
Assert
assert_op
=
Assert
(
'Input to bincount has negative values!'
)
x
=
assert_op
(
x
,
theano
.
tensor
.
all
(
x
>=
0
))
max_value
=
theano
.
tensor
.
cast
(
x
.
max
()
+
1
,
'int64'
)
if
minlength
is
not
None
:
max_value
=
theano
.
tensor
.
maximum
(
max_value
,
minlength
)
if
weights
is
None
:
out
=
theano
.
tensor
.
zeros
([
max_value
],
dtype
=
x
.
dtype
)
out
=
theano
.
tensor
.
inc_subtensor
(
out
[
x
],
1
)
else
:
out
=
theano
.
tensor
.
zeros
([
max_value
],
dtype
=
weights
.
dtype
)
out
=
theano
.
tensor
.
inc_subtensor
(
out
[
x
],
weights
)
return
out
def
squeeze
(
x
):
def
squeeze
(
x
):
...
...
theano/tensor/tests/test_extra_ops.py
浏览文件 @
73a6651f
...
@@ -115,6 +115,36 @@ class TestBinCountOp(utt.InferShapeTester):
...
@@ -115,6 +115,36 @@ class TestBinCountOp(utt.InferShapeTester):
self
.
op_class
=
BinCountOp
self
.
op_class
=
BinCountOp
self
.
op
=
BinCountOp
()
self
.
op
=
BinCountOp
()
def
test_bincountFn
(
self
):
w
=
T
.
vector
(
'w'
)
for
dtype
in
(
'int8'
,
'int16'
,
'int32'
,
'int64'
,
'uint8'
,
'uint16'
,
'uint32'
,
'uint64'
):
x
=
T
.
vector
(
'x'
,
dtype
=
dtype
)
# uint64 always fails
if
dtype
in
(
'uint64'
,):
self
.
assertRaises
(
TypeError
,
bincount
,
x
)
else
:
a
=
np
.
random
.
random_integers
(
50
,
size
=
(
25
))
.
astype
(
dtype
)
weights
=
np
.
random
.
random
((
25
,))
.
astype
(
config
.
floatX
)
f1
=
theano
.
function
([
x
],
bincount
(
x
))
f2
=
theano
.
function
([
x
,
w
],
bincount
(
x
,
weights
=
w
))
assert
(
np
.
bincount
(
a
)
==
f1
(
a
))
.
all
()
assert
np
.
allclose
(
np
.
bincount
(
a
,
weights
=
weights
),
f2
(
a
,
weights
))
f3
=
theano
.
function
([
x
],
bincount
(
x
,
minlength
=
23
))
f4
=
theano
.
function
([
x
],
bincount
(
x
,
minlength
=
5
))
assert
(
np
.
bincount
(
a
,
minlength
=
23
)
==
f3
(
a
))
.
all
()
assert
(
np
.
bincount
(
a
,
minlength
=
5
)
==
f4
(
a
))
.
all
()
# skip the following test when using unsigned ints
if
not
dtype
.
startswith
(
'u'
):
a
[
0
]
=
-
1
f5
=
theano
.
function
([
x
],
bincount
(
x
,
assert_nonneg
=
True
))
self
.
assertRaises
(
AssertionError
,
f5
,
a
)
def
test_bincountOp
(
self
):
def
test_bincountOp
(
self
):
w
=
T
.
vector
(
'w'
)
w
=
T
.
vector
(
'w'
)
for
dtype
in
(
'int8'
,
'int16'
,
'int32'
,
'int64'
,
for
dtype
in
(
'int8'
,
'int16'
,
'int32'
,
'int64'
,
...
@@ -130,22 +160,22 @@ class TestBinCountOp(utt.InferShapeTester):
...
@@ -130,22 +160,22 @@ class TestBinCountOp(utt.InferShapeTester):
x
=
T
.
vector
(
'x'
,
dtype
=
dtype
)
x
=
T
.
vector
(
'x'
,
dtype
=
dtype
)
if
dtype
in
numpy_unsupported_dtypes
:
if
dtype
in
numpy_unsupported_dtypes
:
self
.
assertRaises
(
TypeError
,
bincount
,
x
)
self
.
assertRaises
(
TypeError
,
BinCountOp
()
,
x
)
else
:
else
:
a
=
np
.
random
.
random_integers
(
50
,
size
=
(
25
))
.
astype
(
dtype
)
a
=
np
.
random
.
random_integers
(
50
,
size
=
(
25
))
.
astype
(
dtype
)
weights
=
np
.
random
.
random
((
25
,))
.
astype
(
config
.
floatX
)
weights
=
np
.
random
.
random
((
25
,))
.
astype
(
config
.
floatX
)
f1
=
theano
.
function
([
x
],
bincount
(
x
))
f1
=
theano
.
function
([
x
],
BinCountOp
()(
x
,
weights
=
None
))
f2
=
theano
.
function
([
x
,
w
],
bincount
(
x
,
weights
=
w
))
f2
=
theano
.
function
([
x
,
w
],
BinCountOp
()
(
x
,
weights
=
w
))
assert
(
np
.
bincount
(
a
)
==
f1
(
a
))
.
all
()
assert
(
np
.
bincount
(
a
)
==
f1
(
a
))
.
all
()
assert
np
.
allclose
(
np
.
bincount
(
a
,
weights
=
weights
),
assert
np
.
allclose
(
np
.
bincount
(
a
,
weights
=
weights
),
f2
(
a
,
weights
))
f2
(
a
,
weights
))
if
not
numpy_16
:
if
not
numpy_16
:
continue
continue
f3
=
theano
.
function
([
x
],
bincount
(
x
,
minlength
=
23
))
f3
=
theano
.
function
([
x
],
BinCountOp
(
minlength
=
23
)(
x
,
weights
=
None
))
f4
=
theano
.
function
([
x
],
bincount
(
x
,
minlength
=
5
))
f4
=
theano
.
function
([
x
],
BinCountOp
(
minlength
=
5
)(
x
,
weights
=
None
))
assert
(
np
.
bincount
(
a
,
minlength
=
23
)
==
f3
(
a
))
.
all
()
assert
(
np
.
bincount
(
a
,
minlength
=
23
)
==
f3
(
a
))
.
all
()
assert
(
np
.
bincount
(
a
,
minlength
=
5
)
==
f4
(
a
))
.
all
()
assert
(
np
.
bincount
(
a
,
minlength
=
5
)
==
f4
(
a
))
.
all
()
...
@@ -162,12 +192,12 @@ class TestBinCountOp(utt.InferShapeTester):
...
@@ -162,12 +192,12 @@ class TestBinCountOp(utt.InferShapeTester):
x
=
T
.
vector
(
'x'
,
dtype
=
dtype
)
x
=
T
.
vector
(
'x'
,
dtype
=
dtype
)
if
dtype
in
numpy_unsupported_dtypes
:
if
dtype
in
numpy_unsupported_dtypes
:
self
.
assertRaises
(
TypeError
,
bincount
,
x
)
self
.
assertRaises
(
TypeError
,
BinCountOp
()
,
x
)
else
:
else
:
self
.
_compile_and_check
(
self
.
_compile_and_check
(
[
x
],
[
x
],
[
bincount
(
x
)],
[
BinCountOp
()(
x
,
None
)],
[
np
.
random
.
random_integers
(
[
np
.
random
.
random_integers
(
50
,
size
=
(
25
,))
.
astype
(
dtype
)],
50
,
size
=
(
25
,))
.
astype
(
dtype
)],
self
.
op_class
)
self
.
op_class
)
...
@@ -175,7 +205,7 @@ class TestBinCountOp(utt.InferShapeTester):
...
@@ -175,7 +205,7 @@ class TestBinCountOp(utt.InferShapeTester):
weights
=
np
.
random
.
random
((
25
,))
.
astype
(
config
.
floatX
)
weights
=
np
.
random
.
random
((
25
,))
.
astype
(
config
.
floatX
)
self
.
_compile_and_check
(
self
.
_compile_and_check
(
[
x
],
[
x
],
[
bincount
(
x
,
weights
=
weights
)],
[
BinCountOp
()
(
x
,
weights
=
weights
)],
[
np
.
random
.
random_integers
(
[
np
.
random
.
random_integers
(
50
,
size
=
(
25
,))
.
astype
(
dtype
)],
50
,
size
=
(
25
,))
.
astype
(
dtype
)],
self
.
op_class
)
self
.
op_class
)
...
@@ -184,14 +214,14 @@ class TestBinCountOp(utt.InferShapeTester):
...
@@ -184,14 +214,14 @@ class TestBinCountOp(utt.InferShapeTester):
continue
continue
self
.
_compile_and_check
(
self
.
_compile_and_check
(
[
x
],
[
x
],
[
bincount
(
x
,
minlength
=
60
)],
[
BinCountOp
(
minlength
=
60
)(
x
,
weights
=
weights
)],
[
np
.
random
.
random_integers
(
[
np
.
random
.
random_integers
(
50
,
size
=
(
25
,))
.
astype
(
dtype
)],
50
,
size
=
(
25
,))
.
astype
(
dtype
)],
self
.
op_class
)
self
.
op_class
)
self
.
_compile_and_check
(
self
.
_compile_and_check
(
[
x
],
[
x
],
[
bincount
(
x
,
minlength
=
5
)],
[
BinCountOp
(
minlength
=
5
)(
x
,
weights
=
weights
)],
[
np
.
random
.
random_integers
(
[
np
.
random
.
random_integers
(
50
,
size
=
(
25
,))
.
astype
(
dtype
)],
50
,
size
=
(
25
,))
.
astype
(
dtype
)],
self
.
op_class
)
self
.
op_class
)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论