Create my own dataset from tensor

Hi there,
I have a torch tensor whose size is [100000, 15, 2] and I want to use it as my dataset (because I am working with GANs so no label needed). and here is my code:

shuffle = True
batch_size = 125
num_worker = 2
pin_memory = True

tensor_input_data = torch.Tensor(input_data) 

my_dataset = TensorDataset(tensor_input_data)
my_dataloader = DataLoader(dataset= my_dataset, 
                            shuffle=shuffle,
                            batch_size=batch_size,
                            num_workers=num_worker,
                            pin_memory=pin_memory)

when I load the dataloader for training

for epoch in range(n_epochs):
        for real in tqdm(dataloader):
            cur_batch_size = len(real)
            real = real.view(cur_batch_size, -1).to(device)

It has the traceback that says ‘list’ object has no attribute ‘view’, I know this mean that “real” is a list, but why?

Appreciate any suggestions!

You can replace the line real = real.view(cur_batch_size, -1) with real = torch.stack(real).view(cur_batch_size, -1), or real = torch.stack(real).view(-1) (depending on the dimensions of your data, you do not need to specify cur_batch_size)

TensorDataset returns a tuple when the dataloader calls __getitem__.

See source code here: torch.utils.data.dataset — PyTorch 1.8.1 documentation