How to repeat a vector batch wise?
<SCODE>>>> images_vec.size() 
torch.Size([100, 784])
<ECODE> <SCODE>torch.Size([100, 784, 28])
<ECODE> Here’s the batch loading code, <SCODE>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() 
<ECODE> Cheers, Aj
AjayTalati
OK, answer my own question, this seems to work? <SCODE>>>> images_repeated = images_vec.repeat(1,sequence_length) 
>>> images_repeated = images_repeated.view(-1,input_size, sequence_length)
>>> images_repeated.size()
torch.Size([100, 784, 28])
<ECODE> I guess it could be checked by something like this? <SCODE>images_repeated[99,783,0] == images_vec
<ECODE> It should be all true? I expect there must be a more robust or cleaner way to do all this though?
AjayTalati
<SCODE>X = torch.randn(100, 700)
X = X.unsqueeze(2).repeat(1, 1, 28)
<ECODE> Or you can use expand: this will only create a view, without copying any data. <SCODE>X = torch.randn(100, 700)
X = X.unsqueeze(2).expand(100, 700, 28)
<ECODE>
elanmart
AjayTalati
What I mean is, <SCODE>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
<ECODE>
AjayTalati
elanmart
AjayTalati
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). Best regards Thomas
tom
AjayTalati
feipan664
<SCODE>X = torch.randn(100, 700)
X = X.unsqueeze(2).expand(100, -1, -1)
<ECODE>
Honghe