How to shuffle one value position in the tensor?

username: John1231983
cooked: <p>I have a tensor that defined as one hot as</p>
<pre><code class="lang-auto">label = torch.from_numpy(np.asarray([0, 0, 1, 0, 0, 0 , 0])).long()
</code></pre>
<p>I want to randomly shuffle the <code>1</code> position to another position in the label such as the new tensor does not equal the label. Do we have any function to do it in pytorch? For example</p>
<pre><code class="lang-auto">new_label = torch.from_numpy(np.asarray([0, 1, 0, 0, 0, 0 , 0])).long()
</code></pre>

username: andreaskoepf
cooked: <p>To shuffle one dimension of a tensor you could use <a href="https://pytorch.org/docs/stable/torch.html?highlight=randperm#torch.randperm" rel="nofollow noopener">torch.randperm()</a>, e.g.:</p>
<pre><code class="lang-auto">&gt;&gt;&gt; a = torch.eye(10)
&gt;&gt;&gt; b = a[torch.randperm(10)]
&gt;&gt;&gt; a
tensor([[1., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 1., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 1., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 1., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 1., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 1., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 1., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0., 1., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0., 0., 1.]])
&gt;&gt;&gt; b
tensor([[1., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 1., 0., 0., 0.],
        [0., 0., 0., 0., 0., 1., 0., 0., 0., 0.],
        [0., 0., 0., 1., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
        [0., 0., 0., 0., 0., 0., 0., 1., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0., 1., 0.],
        [0., 0., 1., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 1., 0., 0., 0., 0., 0.]])
&gt;&gt;&gt; 
</code></pre>

username: John1231983
cooked: <aside class="quote no-group" data-username="andreaskoepf" data-post="2" data-topic="54406">
<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/andreaskoepf/48/15327_2.png" class="avatar"> andreaskoepf:</div>
<blockquote>
<p>b = a[torch.randperm(10)]</p>
</blockquote>
</aside>
<p>Thanks. After 1M iterations, does b will equal a? I do not want to the new tensor equal the input</p>

username: John1231983
cooked: <p><a class="mention" href="/u/andreaskoepf">@andreaskoepf</a>: How it work for my example?<br>
This is error when I used your code</p>
<pre><code class="lang-auto">/opt/conda/conda-bld/pytorch_1565287025495/work/aten/src/ATen/native/cuda/IndexKernel.cu:60: lambda [](int)-&gt;auto::operator()(int)-&gt;auto: block: [0,0,0], thread: [3,0,0] Assertion `index &gt;= -sizes[i] &amp;&amp; index &lt; sizes[i] &amp;&amp; "index out of bounds"` failed.
/opt/conda/conda-bld/pytorch_1565287025495/work/aten/src/ATen/native/cuda/IndexKernel.cu:60: lambda [](int)-&gt;auto::operator()(int)-&gt;auto: block: [0,0,0], thread: [4,0,0] Assertion `index &gt;= -sizes[i] &amp;&amp; index &lt; sizes[i] &amp;&amp; "index out of bounds"` failed.
/opt/conda/conda-bld/pytorch_1565287025495/work/aten/src/ATen/native/cuda/IndexKernel.cu:60: lambda [](int)-&gt;auto::operator()(int)-&gt;auto: block: [0,0,0], thread: [5,0,0] Assertion `index &gt;= -sizes[i] &amp;&amp; index &lt; sizes[i] &amp;&amp; "index out of bounds"` failed.
/opt/conda/conda-bld/pytorch_1565287025495/work/aten/src/ATen/native/cuda/IndexKernel.cu:60: lambda [](int)-&gt;auto::operator()(int)-&gt;auto: block: [0,0,0], thread: [6,0,0] Assertion `index &gt;= -sizes[i] &amp;&amp; index &lt; sizes[i] &amp;&amp; "index out of bounds"` failed.
</code></pre>