唯一¶
唯一 - 11¶
版本¶
名称: 唯一 (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
为便于理解,下面展示了中间数据:沿输入_x (shape = (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]
属性¶
轴 - INT :
(可选) 应用唯一操作的维度。如果未指定,则返回展平输入的唯一元素。负值表示从末尾开始计算维度。可接受的范围是 [-r, r-1],其中 r = rank(input)。
sorted - INT (默认值为
'1'
)(可选) 是否在返回输出之前按升序对唯一元素进行排序。必须为 0 或 1(默认值)。
输入¶
X (异构) - T
一个 N 维输入张量,用于处理。
输出¶
介于 1 和 4 之间
Y (异构) - T
一个与“X”类型相同的张量,包含“X”中沿提供的“axis”切片的所有唯一值或子张量,按升序排序或按输入“X”中出现的顺序保持不变
indices (可选,异构) - tensor(int64)
一个一维 INT64 张量,包含“Y”元素在“X”中首次出现的索引。当提供“axis”时,它包含输入“X”在“axis”上的子张量索引。当未提供“axis”时,它包含展平输入张量中的值索引。
inverse_indices (可选,异构) - tensor(int64)
一个一维 INT64 张量,包含“X”元素的对应“Y”中的索引。当提供“axis”时,它包含输出“Y”在“axis”上的子张量索引。当未提供“axis”时,它包含输出“Y”中的值索引。
counts (可选,异构) - tensor(int64)
一个一维 INT64 张量,包含输入“X”中“Y”每个元素的计数
类型约束¶
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)
)输入可以是任何张量类型。