# Sharing memory when gathering vectors from a tensor?

**URL:** <https://discuss.pytorch.org/t/sharing-memory-when-gathering-vectors-from-a-tensor/13424>\
**Category:** Uncategorized\
**Created:** [February 7, 2018, 11:50pm UTC](https://discuss.pytorch.org/t/sharing-memory-when-gathering-vectors-from-a-tensor/13424 "2018-02-07T23:50:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jamesmullenbach](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/jamesmullenbach/32/3482_2.png) [@jamesmullenbach](https://discuss.pytorch.org/u/jamesmullenbach)\
**Post date:** [February 7, 2018, 11:50pm UTC](https://discuss.pytorch.org/t/sharing-memory-when-gathering-vectors-from-a-tensor/13424/1 "2018-02-07T23:50:07Z")

</div>

I need to gather vectors from a tensor, some of which may be redundant. Here’s a toy example:

```auto
tmp = Variable(torch.randn((5,2)))

Variable containing:
 0.2994 0.9508
 0.9271 0.0781
 0.2100 0.0150
-0.4117 0.8177
-1.0631 1.0050
[torch.FloatTensor of size 5x2]

ind = Variable(torch.LongTensor([0, 1, 0, 0, 1, 1, 0, 1])) #redundant, longer than tmp
tmp.gather(1, ind.expand(5,-1))

Variable containing:
 0.2994 0.9508 0.2994 0.2994 0.9508 0.9508 0.2994 0.9508
 0.9271 0.0781 0.9271 0.9271 0.0781 0.0781 0.9271 0.0781
 0.2100 0.0150 0.2100 0.2100 0.0150 0.0150 0.2100 0.0150
-0.4117 0.8177 -0.4117 -0.4117 0.8177 0.8177 -0.4117 0.8177
-1.0631 1.0050 -1.0631 -1.0631 1.0050 1.0050 -1.0631 1.0050
[torch.FloatTensor of size 5x8]

```

This works, but I’m trying to reduce memory usage as much as possible. Does `gather` copy data by default? If not, is there some other way to accomplish this without copying data?

I can’t get around the issue by removing redundancies; they are needed as each gathered vector will be handled by different parameters of my network.

Thank you!

---

<div class="post-metadata">

**Author:** ![albanD](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alband/32/215_2.png) [@albanD](https://discuss.pytorch.org/u/albanD)\
**Post date:** [February 8, 2018, 10:42am UTC](https://discuss.pytorch.org/t/sharing-memory-when-gathering-vectors-from-a-tensor/13424/2 "2018-02-08T10:42:12Z")

</div>

Hi,

`Tensor`s correspond to a specific way to see a 1D array in memory (or part of it).  
So the short answer is no you cannot do that, as what you want cannot be seen as a way to look at a 1D array.
