# Best way to run unique\_consecutive() on certain dimension?

**URL:** <https://discuss.pytorch.org/t/best-way-to-run-unique-consecutive-on-certain-dimension/112662>\
**Category:** Uncategorized\
**Created:** [February 23, 2021, 12:52am UTC](https://discuss.pytorch.org/t/best-way-to-run-unique-consecutive-on-certain-dimension/112662 "2021-02-23T00:52:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![galactica147](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/galactica147/32/19682_2.png) [@galactica147](https://discuss.pytorch.org/u/galactica147)\
**Post date:** [February 23, 2021, 12:52am UTC](https://discuss.pytorch.org/t/best-way-to-run-unique-consecutive-on-certain-dimension/112662/1 "2021-02-23T00:52:03Z")

</div>

What’s the best way to call `unique_consecutive()` on certain dimension of a tensor, and pad the left-out with specified values?  
For simplicity, we could use 2D tensor as an example:

```auto
input = tensor([[3, 3, 5, 5, 5],
                [3, 3, 2, 2, 3]])

```

if specifying padding value -1, what i hope to get is:

```auto
output = tensor([[3, 5, -1, -1, -1],
                 [3, 2, 3, -1, -1]])

```

Also, to save memory, i’d prefer to do so in-place if possible.  
Thanks!

---

<div class="post-metadata">

**Author:** ![Eta\_C](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/eta_c/32/17667_2.png) [@Eta\_C](https://discuss.pytorch.org/u/Eta_C)\
**Post date:** [February 23, 2021, 2:13am UTC](https://discuss.pytorch.org/t/best-way-to-run-unique-consecutive-on-certain-dimension/112662/2 "2021-02-23T02:13:21Z")

</div>

Try

```python
x = tensor([[3, 3, 5, 5, 5],
            [3, 3, 2, 2, 3]])
unique_x, indices = torch.unique_consecutive(x, return_inverse=True)
indices -= indices.min(dim=1, keepdims=True)[0]
result = -torch.ones_like(x)
result = result.scatter_(1, indices, x)

```

---

<div class="post-metadata">

**Author:** ![galactica147](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/galactica147/32/19682_2.png) [@galactica147](https://discuss.pytorch.org/u/galactica147)\
**Post date:** [February 23, 2021, 7:52am UTC](https://discuss.pytorch.org/t/best-way-to-run-unique-consecutive-on-certain-dimension/112662/3 "2021-02-23T07:52:46Z")

</div>

Thanks! This is working well!

Could you elaborate a bit what `indices -= indices.min(dim=1, keepdims=True)[0]` is doing?

This solution creates another tensor for the updated result, which is fine. I wonder if there is any in-place way that can save the extra memory cost.

---

<div class="post-metadata">

**Author:** ![Eta\_C](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/eta_c/32/17667_2.png) [@Eta\_C](https://discuss.pytorch.org/u/Eta_C)\
**Post date:** [February 23, 2021, 9:09am UTC](https://discuss.pytorch.org/t/best-way-to-run-unique-consecutive-on-certain-dimension/112662/4 "2021-02-23T09:09:55Z")

</div>

> [@galactica147](#):
>
> Could you elaborate a bit what `indices -= indices.min(dim=1, keepdims=True)[0]` is doing?

See,

```python
indices = [[0, 0, 1, 1, 1],
           [2, 2, 3, 3, 4]]

```

But here `scatter` needs **col index**.

```python
indices = [[0, 0, 1, 1, 1],
           [0, 0, 1, 1, 2]]

```

> [@galactica147](#):
>
> I wonder if there is any in-place way that can save the extra memory cost.

I have no idea now.

---

<div class="post-metadata">

**Author:** ![galactica147](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/galactica147/32/19682_2.png) [@galactica147](https://discuss.pytorch.org/u/galactica147)\
**Post date:** [February 23, 2021, 5:37pm UTC](https://discuss.pytorch.org/t/best-way-to-run-unique-consecutive-on-certain-dimension/112662/5 "2021-02-23T17:37:43Z")

</div>

This is helpful! Thanks a lot!
