How to repeat a vector batch wise?

username: AjayTalati
cooked: <p>I have a batch of <code>100</code>, data vectors each of length <code>784</code>,  So my batch has size,</p>
<pre><code class="lang-auto">&gt;&gt;&gt; images_vec.size() 
torch.Size([100, 784])
</code></pre>
<p>How do I copy each vector, <code>seq_length=28</code> times? So that I now have a batch of data vectors, which are simply  repeated <code>seq_length</code> times. So the batch should now have size,</p>
<pre><code class="lang-auto">torch.Size([100, 784, 28])
</code></pre>
<p>Here’s the batch loading code,</p>
<pre><code class="lang-auto">import torch 
import torchvision.datasets as dsets
import torchvision.transforms as transforms

# Hyper Parameters
sequence_length = 28
input_size = 28*28
batch_size = 100

# MNIST Dataset
train_dataset = dsets.MNIST(root='../data_tmp/',
                            train=True, 
                            transform=transforms.ToTensor(),
                            download=True)

# Data Loader (Input Pipeline)
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
                                           batch_size=batch_size, 
                                           shuffle=True)

for i, (images, labels) in enumerate(train_loader):
    #images_repeated = images.view(-1, sequence_length, input_size) 
    images_vec = images.view(-1, input_size)
    #images_repeated = torch.randn(100,784,28) # pre-allocate
    images_repeated = torch.randn(batch_size, input_size, sequence_length) # pre-allocate
    #for j in range(sequence_length):
        #images_repeated [,,j] = images_vec
    y_true = labels
    if i == 1:
        break

images.size()
images_vec.size() 
</code></pre>
<p>Cheers,</p>
<p>Aj</p>

username: AjayTalati
cooked: <p>OK, answer my own question, this seems to work?</p>
<pre><code class="lang-auto">&gt;&gt;&gt; images_repeated = images_vec.repeat(1,sequence_length) 
&gt;&gt;&gt; images_repeated = images_repeated.view(-1,input_size, sequence_length)
&gt;&gt;&gt; images_repeated.size()
torch.Size([100, 784, 28])
</code></pre>
<p>I guess it could be checked by something like this?</p>
<pre><code class="lang-auto">images_repeated[99,783,0] == images_vec
</code></pre>
<p>It should be all true? I expect there must be a more robust or cleaner way to do all this though?</p>

username: elanmart
cooked: <p>You can use <code>repeat</code> – this will copy each vector 28 times.</p>
<pre><code class="lang-python">X = torch.randn(100, 700)
X = X.unsqueeze(2).repeat(1, 1, 28)
</code></pre>
<p>Or you can use expand: this will only create a view, without copying any data.</p>
<pre><code class="lang-python">X = torch.randn(100, 700)
X = X.unsqueeze(2).expand(100, 700, 28)
</code></pre>

username: AjayTalati
cooked: <p>Ha ha, very nice - thank Q <img src="//discuss.pytorch.org/images/emoji/apple/smile.png?v=5" title=":smile:" class="emoji" alt=":smile:"></p>

username: AjayTalati
cooked: <p>How do check, <code>all true</code> vectorially in PyTorch?</p>
<p>What I mean is,</p>
<pre><code class="lang-auto">X = torch.randn(100, 700)
X_tmp = X.unsqueeze(2).repeat(1, 1, 28)

X[1] == X_tmp[1,:,0] # should be all true - want to check it vectorially
</code></pre>

username: elanmart
cooked: <p><code>np.alltrue(X[1].numpy() == X_tmp[1, :, 0].numpy())</code>?</p>

username: AjayTalati
cooked: <p>Ha ha – <a class="mention" href="/u/elanmart">@elanmart</a> – you’re quick dude – good to have you here  <img src="//discuss.pytorch.org/images/emoji/apple/smile.png?v=5" title=":smile:" class="emoji" alt=":smile:"></p>

username: tom
cooked: <p>If you want to stay in pytorch (e.g. for GPU arrays), you can do (X[1] != X_tmp[1,:,0]).sum()==0 . The caveat is that NaN != NaN (X != X seemed to be easiest way to check for NaN in pytorch a while back).</p>
<p>Best regards</p>
<p>Thomas</p>

username: AjayTalati
cooked: <p>Thanks a lot <a class="mention" href="/u/tom">@tom</a> – seems you’re mastering PyTorch pretty quick!</p>
<p>Good to hear from you <img src="//discuss.pytorch.org/images/emoji/apple/smile.png?v=5" title=":smile:" class="emoji" alt=":smile:"></p>

username: feipan664
cooked: <p><a class="mention" href="/u/elanmart">@elanmart</a> His code for me perfectly.<br>
My situation is this, I want to copy 128 times along a dimension:</p>
<blockquote>
<p>x=torch.randn(100, 1, 32, 32)<br>
print(x.shape)</p>
</blockquote>
<p>you can get <code>[100, 1, 32, 32]</code></p>
<blockquote>
<p>x_hat = x.repeat(1,128,32,32)<br>
print(x_hat)</p>
</blockquote>
<p>then you can get <code>[100, 128, 32, 32]</code>. And you can also print out the inside value:</p>
<blockquote>
<p>print(x[0,0,:20, :20])<br>
print(x_hat[0,33,:20,:20])</p>
</blockquote>

username: Honghe
cooked: <pre><code class="lang-auto">X = torch.randn(100, 700)
X = X.unsqueeze(2).expand(100, -1, -1)
</code></pre>
<p>Passing -1 as the size for a dimension means not changing the size of<br>
that dimension.</p>