<p dir="auto">As you can see it's not a true shuffle as there are repeats and missing values.</p>
<blockquote>
<blockquote>
<blockquote>
<p dir="auto">import torch, random<br>
import numpy as np<br>
x1 = torch.arange(10)<br>
x2 = np.arange(10)<br>
x1, x2<br>
(tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]), array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]))<br>
random.shuffle(x1)<br>
random.shuffle(x2)<br>
x1, x2<br>
(tensor([0, 0, 2, 0, 2, 2, 6, 1, 4, 4]), array([1, 3, 8, 0, 2, 6, 5, 4, 7, 9]))</p>
</blockquote>
</blockquote>
</blockquote>
<h2 dir="auto"><g-emoji class="g-emoji" alias="bug" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/1f41b.png">🐛</g-emoji> Bug</h2>
<p dir="auto">Consider a Tensor <code class="notranslate">x</code>. I would like to swap elements <code class="notranslate">i</code> and <code class="notranslate">j</code> in a Pythonic way as follows:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="x[i], x[j] = x[j], x[i]"><pre class="notranslate"><span class="pl-s1">x</span>[<span class="pl-s1">i</span>], <span class="pl-s1">x</span>[<span class="pl-s1">j</span>] <span class="pl-c1">=</span> <span class="pl-s1">x</span>[<span class="pl-s1">j</span>], <span class="pl-s1">x</span>[<span class="pl-s1">i</span>]</pre></div>
<p dir="auto">However, what this ends up doing is replacing element <code class="notranslate">i</code> with <code class="notranslate">j</code> and not modifying element <code class="notranslate">j</code>.</p>
<h2 dir="auto">To Reproduce</h2>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import torch
x = torch.randn(4)  # tensor([-0.7905, -0.3574, -0.0493, -0.3152])
x[0], x[1] = x[1], x[0]
print(x)  # tensor([-0.3574, -0.3574, -0.0493, -0.3152])

x = torch.randn(4, 4)
assert not (x[0] == x[1]).all()
x[0], x[1] = x[1], x[0]
assert not (x[0] == x[1]).all()"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">torch</span>
<span class="pl-s1">x</span> <span class="pl-c1">=</span> <span class="pl-s1">torch</span>.<span class="pl-en">randn</span>(<span class="pl-c1">4</span>)  <span class="pl-c"># tensor([-0.7905, -0.3574, -0.0493, -0.3152])</span>
<span class="pl-s1">x</span>[<span class="pl-c1">0</span>], <span class="pl-s1">x</span>[<span class="pl-c1">1</span>] <span class="pl-c1">=</span> <span class="pl-s1">x</span>[<span class="pl-c1">1</span>], <span class="pl-s1">x</span>[<span class="pl-c1">0</span>]
<span class="pl-en">print</span>(<span class="pl-s1">x</span>)  <span class="pl-c"># tensor([-0.3574, -0.3574, -0.0493, -0.3152])</span>

<span class="pl-s1">x</span> <span class="pl-c1">=</span> <span class="pl-s1">torch</span>.<span class="pl-en">randn</span>(<span class="pl-c1">4</span>, <span class="pl-c1">4</span>)
<span class="pl-k">assert</span> <span class="pl-c1">not</span> (<span class="pl-s1">x</span>[<span class="pl-c1">0</span>] <span class="pl-c1">==</span> <span class="pl-s1">x</span>[<span class="pl-c1">1</span>]).<span class="pl-en">all</span>()
<span class="pl-s1">x</span>[<span class="pl-c1">0</span>], <span class="pl-s1">x</span>[<span class="pl-c1">1</span>] <span class="pl-c1">=</span> <span class="pl-s1">x</span>[<span class="pl-c1">1</span>], <span class="pl-s1">x</span>[<span class="pl-c1">0</span>]
<span class="pl-k">assert</span> <span class="pl-c1">not</span> (<span class="pl-s1">x</span>[<span class="pl-c1">0</span>] <span class="pl-c1">==</span> <span class="pl-s1">x</span>[<span class="pl-c1">1</span>]).<span class="pl-en">all</span>()</pre></div>
<h2 dir="auto">Expected behavior</h2>
<p dir="auto">Swap the elements correctly. NumPy seemingly supports this only in 1-D, and not in 2-D.</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import numpy as np
x = np.random.randn(4)  # array([ 0.11462874, -1.3748384 ,  0.24256925,  1.01256207])
x[0], x[1] = x[1], x[0]
print(x)  # array([-1.3748384 ,  0.11462874,  0.24256925,  1.01256207])"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>
<span class="pl-s1">x</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-s1">random</span>.<span class="pl-en">randn</span>(<span class="pl-c1">4</span>)  <span class="pl-c"># array([ 0.11462874, -1.3748384 ,  0.24256925,  1.01256207])</span>
<span class="pl-s1">x</span>[<span class="pl-c1">0</span>], <span class="pl-s1">x</span>[<span class="pl-c1">1</span>] <span class="pl-c1">=</span> <span class="pl-s1">x</span>[<span class="pl-c1">1</span>], <span class="pl-s1">x</span>[<span class="pl-c1">0</span>]
<span class="pl-en">print</span>(<span class="pl-s1">x</span>)  <span class="pl-c"># array([-1.3748384 ,  0.11462874,  0.24256925,  1.01256207])</span></pre></div>
<h2 dir="auto">Environment</h2>
<p dir="auto">PyTorch version: 1.1.0a0+6ce147c<br>
Is debug build: No<br>
CUDA used to build PyTorch: Could not collect</p>
<p dir="auto">OS: Ubuntu 18.04.1 LTS<br>
GCC version: (Ubuntu 7.3.0-16ubuntu3) 7.3.0<br>
CMake version: version 3.10.2</p>
<p dir="auto">Python version: 3.6<br>
Is CUDA available: No<br>
CUDA runtime version: 9.2.148<br>
GPU models and configuration: GPU 0: GeForce 940M<br>
Nvidia driver version: 396.54<br>
cuDNN version: /usr/local/cuda-9.2/lib64/libcudnn.so.7</p>
<p dir="auto">Versions of relevant libraries:<br>
[pip] numpy==1.15.4<br>
[pip] pytorch-sphinx-theme==0.0.24<br>
[pip] torch==1.1.0a0+6ce147c<br>
[pip] torchvision==0.2.1<br>
[conda] blas                      1.0                         mkl<br>
[conda] mkl                       2019.1                      144<br>
[conda] mkl-include               2018.0.3                      1<br>
[conda] mkl_fft                   1.0.6            py36hd81dba3_0<br>
[conda] mkl_random                1.0.2            py36hd81dba3_0<br>
[conda] pytorch-sphinx-theme      0.0.24                   pypi_0    pypi<br>
[conda] torch                     1.1.0a0+6ce147c           dev_0    </p>
<h2 dir="auto">Additional context</h2>
<p dir="auto">I think this might be related to <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="330026174" data-permission-text="Title is private" data-url="https://github.com/pytorch/pytorch/issues/8212" data-hovercard-type="issue" data-hovercard-url="/pytorch/pytorch/issues/8212/hovercard" href="https://github.com/pytorch/pytorch/issues/8212">#8212</a>, but I'm not sure.</p>