唯一

唯一 - 11

版本

  • 名称: Unique (GitHub)

  • 域名: main

  • since_version: 11

  • 函数: False

  • support_level: SupportType.COMMON

  • 形状推断: True

此版本的运算符自版本11起可用。

摘要

找到张量的唯一元素。当提供了可选的属性‘axis’时,将返回沿‘axis’切片的唯一子张量。 否则,输入张量将被展平,并返回展平张量的唯一值。

此操作符返回输入张量的唯一值或切片的唯一子张量以及三个可选的输出。 第一个输出张量‘Y’包含输入的所有唯一值或子张量。 第二个可选的输出张量‘indices’包含‘Y’元素在‘X’中首次出现的索引。 第三个可选的输出张量‘inverse_indices’包含,对于‘X’的元素,其在‘Y’中的对应索引。 第四个可选的输出张量‘counts’包含输入中每个‘Y’元素的计数。

输出可以按升序排序,或者可选地按输入中值首次出现的顺序排序。

https://docs.scipy.org/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]

输出计数:

[2, 1, 1]

属性

  • axis - 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):

    一个包含输入‘X’中‘Y’的每个元素的计数的1-D 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) ):

    输入可以是任何张量类型。