Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PyTorch torch.unique_consecutive function

PyTorch torch.unique_consecutive function


PyTorch torch reference manual PyTorch torch reference manual

torch.unique_consecutive is a function in PyTorch used to get consecutive unique values. It returns adjacent consecutive unique values in a tensor.

Function definition

torch.unique_consecutive(input, return_inverse=False, return_counts=False, dim=None)


Usage examples

Example

import

torch

Creating tensors

x

=

torch.

tensor

(

[

1

,

1

,

1

,

2

,

2

,

3

,

3

,

3

,

1

,

1

]

)

Get consecutive unique values

unique_vals

=

torch.

unique_consecutive

(

x

)

print

(

f

"Consecutive unique values: {unique_vals}"

)

Return the counts

unique_vals

,

counts

=

torch.

unique_consecutive

(

x

,

return_counts

=

True

)

print

(

f

"Counts: {counts}"

)


PyTorch torch reference manual PyTorch torch reference manual