Unique¶
Unique - 11¶
版本¶
名称: Unique (GitHub)
域:
main
起始版本:
11
函数:
False
支持级别:
SupportType.COMMON
形状推断:
True
此版本的运算符自版本 11 起可用。
摘要¶
查找张量中的唯一元素。当提供了可选属性 ‘axis’ 时,返回沿 ‘axis’ 切片的唯一子张量。否则,输入张量将被展平,并返回展平张量中的唯一值。
此运算符返回输入张量的唯一值或沿指定轴切片的唯一子张量,以及三个可选输出。第一个输出张量 ‘Y’ 包含输入中所有唯一值或子张量。第二个可选输出张量 ‘indices’ 包含 ‘Y’ 中元素在 ‘X’ 中首次出现的索引。第三个可选输出张量 ‘inverse_indices’ 包含 ‘X’ 中元素在 ‘Y’ 中的对应索引。第四个可选输出张量 ‘counts’ 包含 ‘Y’ 中每个元素在输入中的出现次数。
输出按升序排序,或者可选地按值在输入中首次出现的顺序排列。
https://docs.scipy.org.cn/doc/numpy/reference/generated/numpy.unique.html
示例 1
input_X = [2, 1, 1, 3, 4, 3]
attribute_sorted = 0
attribute_axis = None
output_Y = [2, 1, 3, 4]
output_indices = [0, 1, 3, 4]
output_inverse_indices = [0, 1, 1, 2, 3, 2]
output_counts = [1, 2, 2, 1]
示例 2
input_X = [[1, 3], [2, 3]]
attribute_sorted = 1
attribute_axis = None
output_Y = [1, 2, 3]
output_indices = [0, 2, 1]
output_inverse_indices = [0, 2, 1, 2]
output_counts = [1, 1, 2]
示例 3
input_X = [[1, 0, 0], [1, 0, 0], [2, 3, 4]]
attribute_sorted = 1
attribute_axis = 0
output_Y = [[1, 0, 0], [2, 3, 4]]
output_indices = [0, 2]
output_inverse_indices = [0, 0, 1]
output_counts = [2, 1]
示例 4
input_x = [[[1., 1.], [0., 1.], [2., 1.], [0., 1.]],
[[1., 1.], [0., 1.], [2., 1.], [0., 1.]]]
attribute_sorted = 1
attribute_axis = 1
以下显示中间数据以便更好地理解:沿着 input_x (形状 = (2, 4, 2)) 的轴 1 切片得到 4 个子张量
A: [[1, 1], [1, 1]],
[[0, 1], [0, 1]],
[[2, 1], [2, 1]],
[[0, 1], [0, 1]].
有 3 个唯一子张量
[[1, 1], [1, 1]],
[[0, 1], [0, 1]],
[[2, 1], [2, 1]].
排序后的唯一子张量
B: [[0, 1], [0, 1]],
[[1, 1], [1, 1]],
[[2, 1], [2, 1]].
output_Y 由 B 构建
[[[0. 1.], [1. 1.], [2. 1.]],
[[0. 1.], [1. 1.], [2. 1.]]]
output_indices 用于从 B 映射到 A
[1, 0, 2]
output_inverse_indices 用于从 A 映射到 B
[1, 0, 2, 0]
output_counts (计数)
[2, 1, 1]
属性¶
axis - 整型 :
(可选)应用 unique 的维度。如果未指定,则返回展平输入的唯一元素。负值表示从后往前计算维度。接受的范围是 [-r, r-1],其中 r = rank(input)。
sorted - 整型 (默认值是
'1'
)(可选)是否在返回输出前将唯一元素按升序排序。必须是 0 或 1(默认值)之一。
输入¶
X (异构) - T
待处理的 N 维输入张量。
输出¶
1 到 4 个输出。
Y (异构) - T
一个与 ‘X’ 类型相同的张量,包含 ‘X’ 中所有唯一值或沿提供的 ‘axis’ 切片的子张量,按排序或与输入 ‘X’ 中出现的顺序相同的方式排列。
indices (可选, 异构) - tensor(int64)
一个包含 ‘Y’ 中元素在 ‘X’ 中首次出现的索引的 1 维 INT64 张量。当提供了 ‘axis’ 时,它包含输入 ‘X’ 中沿 ‘axis’ 的子张量的索引。当未提供 ‘axis’ 时,它包含展平输入张量中值的索引。
inverse_indices (可选, 异构) - tensor(int64)
一个包含 ‘X’ 中元素在 ‘Y’ 中对应索引的 1 维 INT64 张量。当提供了 ‘axis’ 时,它包含输出 ‘Y’ 中沿 ‘axis’ 的子张量的索引。当未提供 ‘axis’ 时,它包含输出 ‘Y’ 中值的索引。
counts (可选, 异构) - tensor(int64)
一个包含 ‘Y’ 中每个元素在输入 ‘X’ 中出现次数的 1 维 INT64 张量。
类型约束¶
T 在 (
tensor(bool)
,tensor(complex128)
,tensor(complex64)
,tensor(double)
,tensor(float)
,tensor(float16)
,tensor(int16)
,tensor(int32)
,tensor(int64)
,tensor(int8)
,tensor(string)
,tensor(uint16)
,tensor(uint32)
,tensor(uint64)
,tensor(uint8)
)输入可以是任何张量类型。