Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
d9b3924f
提交
d9b3924f
authored
10月 06, 2024
作者:
Ricardo Vieira
提交者:
Ricardo Vieira
10月 07, 2024
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Simplify Unique Op
上级
fa0ab9de
显示空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
37 行增加
和
58 行删除
+37
-58
extra_ops.py
pytensor/tensor/extra_ops.py
+37
-58
没有找到文件。
pytensor/tensor/extra_ops.py
浏览文件 @
d9b3924f
...
...
@@ -41,7 +41,7 @@ from pytensor.tensor.math import (
)
from
pytensor.tensor.math
import
max
as
pt_max
from
pytensor.tensor.math
import
sum
as
pt_sum
from
pytensor.tensor.shape
import
specify_broadcastable
from
pytensor.tensor.shape
import
Shape_i
,
specify_broadcastable
from
pytensor.tensor.subtensor
import
advanced_inc_subtensor1
,
set_subtensor
from
pytensor.tensor.type
import
TensorType
,
dvector
,
int_dtypes
,
integer_dtypes
,
vector
from
pytensor.tensor.variable
import
TensorVariable
...
...
@@ -1194,23 +1194,22 @@ class Unique(Op):
self
.
return_index
=
return_index
self
.
return_inverse
=
return_inverse
self
.
return_counts
=
return_counts
if
axis
is
not
None
and
axis
<
0
:
raise
ValueError
(
"Axis cannot be negative."
)
self
.
axis
=
axis
def
make_node
(
self
,
x
):
x
=
ptb
.
as_tensor_variable
(
x
)
self_
axis
=
self
.
axis
if
self_
axis
is
None
:
axis
=
self
.
axis
if
axis
is
None
:
out_shape
=
(
None
,)
else
:
if
self_axis
<
0
:
self_axis
+=
x
.
type
.
ndim
if
self_axis
<
0
or
self_axis
>=
x
.
type
.
ndim
:
if
axis
>=
x
.
type
.
ndim
:
raise
ValueError
(
f
"
Unique axis {self.axis} is outside of input ndim = {x.type.ndim}
"
f
"
Axis {axis} out of range for input {x} with ndim={x.type.ndim}.
"
)
out_shape
=
tuple
(
s
if
s
==
1
and
axis
!=
self_axis
else
None
for
axis
,
s
in
enumerate
(
x
.
type
.
shape
)
None
if
dim
==
axis
else
s
for
dim
,
s
in
enumerate
(
x
.
type
.
shape
)
)
outputs
=
[
TensorType
(
dtype
=
x
.
dtype
,
shape
=
out_shape
)()]
...
...
@@ -1224,60 +1223,37 @@ class Unique(Op):
return
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
if
self
.
axis
is
not
None
:
param
[
"axis"
]
=
self
.
axis
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
:
[
x
]
=
inputs
outs
=
np
.
unique
(
x
,
return_index
=
self
.
return_index
,
return_inverse
=
self
.
return_inverse
,
return_counts
=
self
.
return_counts
,
axis
=
self
.
axis
,
)
if
isinstance
(
outs
,
tuple
):
for
i
in
range
(
len
(
outs
)):
z
[
i
][
0
]
=
outs
[
i
]
output_storage
[
i
][
0
]
=
outs
[
i
]
else
:
output_storage
[
0
][
0
]
=
outs
def
infer_shape
(
self
,
fgraph
,
node
,
i0_shapes
):
ret
=
fgraph
.
shape_feature
.
default_infer_shape
(
fgraph
,
node
,
i0_shapes
)
if
self
.
axis
is
not
None
:
self_axis
=
self
.
axis
ndim
=
len
(
i0_shapes
[
0
])
if
self_axis
<
0
:
self_axis
+=
ndim
if
self_axis
<
0
or
self_axis
>=
ndim
:
raise
RuntimeError
(
f
"Unique axis `{self.axis}` is outside of input ndim = {ndim}."
)
ret
[
0
]
=
tuple
(
fgraph
.
shape_feature
.
shape_ir
(
i
,
node
.
outputs
[
0
])
for
i
in
range
(
ndim
)
)
[
x_shape
]
=
i0_shapes
shape0_op
=
Shape_i
(
0
)
out_shapes
=
[(
shape0_op
(
out
),)
for
out
in
node
.
outputs
]
axis
=
self
.
axis
if
axis
is
not
None
:
shape
=
list
(
x_shape
)
shape
[
axis
]
=
Shape_i
(
axis
)(
node
.
outputs
[
0
])
out_shapes
[
0
]
=
tuple
(
shape
)
if
self
.
return_inverse
:
if
self
.
axis
is
None
:
shape
=
(
prod
(
i0_shapes
[
0
]),)
else
:
shape
=
(
i0_shapes
[
0
][
self_axis
],)
if
self
.
return_index
:
ret
[
2
]
=
shape
return
ret
ret
[
1
]
=
shape
return
ret
return
ret
shape
=
prod
(
x_shape
)
if
self
.
axis
is
None
else
x_shape
[
axis
]
return_index_out_idx
=
2
if
self
.
return_index
else
1
out_shapes
[
return_index_out_idx
]
=
(
shape
,)
def
__setstate__
(
self
,
state
):
self
.
__dict__
.
update
(
state
)
# For backwards compatibility with pickled instances of Unique that
# did not have the axis parameter specified
if
"axis"
not
in
state
:
self
.
axis
=
None
return
out_shapes
def
unique
(
...
...
@@ -1293,6 +1269,9 @@ def unique(
* the number of times each unique value comes up in the input array
"""
ar
=
as_tensor_variable
(
ar
)
if
axis
is
not
None
:
axis
=
normalize_axis_index
(
axis
,
ar
.
ndim
)
return
Unique
(
return_index
,
return_inverse
,
return_counts
,
axis
)(
ar
)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论