RuntimeError: slice() cannot be applied to a 0-dim tensor

username: Niki
cooked: <p>I want to put labels and their shuffles beside each other, such as:</p>
<pre><code class="lang-auto">labels_total = [labels, labels_shuffel[1], labels_shuffel[2], ..]
</code></pre>
<p>I tried for loop:</p>
<pre><code class="lang-auto">labels_total =  labels
For i in range(5):
       labels_total = [labels_total, labels_shuffel[i]]
</code></pre>
<p>But this one gives a list, I used <code>torch.FloatTensor</code>, it gave this error:</p>
<pre><code class="lang-auto">TypeError: expected torch.FloatTensor (got torch.LongTensor)
</code></pre>
<p>Then I used <code>torch.LongTensor</code>, now it’s giving this error:</p>
<pre><code class="lang-auto">RuntimeError: slice() cannot be applied to a 0-dim tensor.
</code></pre>
<p>How can I solve it?</p>

username: Pranavan_Theivendira
cooked: <aside class="quote no-group" data-username="Niki" data-post="1" data-topic="48077">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/n/c57346/48.png" class="avatar"> Niki:</div>
<blockquote>
<pre><code class="lang-auto">TypeError: expected torch.FloatTensor (got torch.LongTensor)
</code></pre>
</blockquote>
</aside>
<p>Hi Niki,</p>
<p>Actually, you are concatenating tensors. You can use following code to concatenate tensors.(This is the conventional way to concatenate tensors in pytorch)</p>
<pre><code class="lang-auto">for i in range(5):
    labels_total = torch.cat((labels_total, labels_shuffel[i]))
</code></pre>
<p>Originally, you are getting a type error. Change that particular torch tensor to float. This should help.</p>

username: Niki
cooked: <p>Thank you for your reply <a class="mention" href="/u/pranavan_theivendira">@Pranavan_Theivendira</a>.<br>
It solved the error, but if later I want to extract all <code>labels_shuffel</code>s from  labels_total, how can I do that?</p>

username: Pranavan_Theivendira
cooked: <aside class="quote no-group" data-username="Pranavan_Theivendira" data-post="2" data-topic="48077">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/pranavan_theivendira/48/12366_2.png" class="avatar"> Pranavan_Theivendira:</div>
<blockquote>
<pre><code class="lang-auto">    labels_total = torch.cat((labels_total, labels_shuffel[i]))
</code></pre>
</blockquote>
</aside>
<p>Hi,</p>
<p>You can slice if you know the indices. For example, you can slice the initial content of labels_total as follows. If the initial number of elements is N. You can just slice as follows.</p>
<pre><code class="lang-auto">labels_total_initial = labels_total[:N]
</code></pre>
<p>Likewise, you can slice other labels_shuffel contents in a similar way. For example, if you need all the label suffels. You can do the following.</p>
<pre><code class="lang-auto">labels_shuffels_prev = labels_total[N:]
</code></pre>
<p>Thanks</p>

username: Niki
cooked: <p>Thank you for your reply, <a class="mention" href="/u/pranavan_theivendira">@Pranavan_Theivendira</a>.<br>
If I were looking for extracting each of those shuffeld ones, I mean <code>labels_shuffel[1], labels_shuffel[2],</code>  … how can I do that?</p>

username: Pranavan_Theivendira
cooked: <p>Hi Niki,</p>
<p>For that, you need the sizes of each individual shuffel sizes.  For example if the shuffel[1] size is M. You can extract shuffel[1] as follows.</p>
<pre><code class="lang-auto">shuffel_1 = labels_total[N:N+M]
</code></pre>
<p>You have to use the proper indices to slice them correctly.</p>
<p>FYI, you did not delete the shuffel arrays/lists. They are still in memory. Hence, you can just use it.</p>
<p>Thanks</p>

username: Niki
cooked: <p>Thank you very much for your help, <a class="mention" href="/u/pranavan_theivendira">@Pranavan_Theivendira</a>.</p>