Here, my_dataloader is returning lists instead of torch tensor. please help.

```
tensor_x = torch.tensor(Mat) #Mat is a 2d numpy array
my_dataset = TensorDataset(tensor_x)
my_dataloader = DataLoader(my_dataset,batch_size=8, shuffle = True)
```

Here, my_dataloader is returning lists instead of torch tensor. please help.

```
tensor_x = torch.tensor(Mat) #Mat is a 2d numpy array
my_dataset = TensorDataset(tensor_x)
my_dataloader = DataLoader(my_dataset,batch_size=8, shuffle = True)
```

Hello, have you tried:

```
for i, (data) in enumerate(my_dataloader):
print(data.type())
```

What is the output of this?

1 Like

If your data loader is returning a list of tensors, and you want a single tensor you could use `torch.stack`

to stack all elements within the list to a single Tensor.

```
list = [ torch.randn(5,5) for _ in range(10) ]
tensor = torch.stack(list)
tensor.shape() #returns torch.Size([10, 5, 5])
```

1 Like

Tried what you said. It gave this : `AttributeError: 'list' object has no attribute 'type'`

What should I do now? Please help

I want my dataloader to return a batch of tensors

Does your list object contain 2 tensors? One for the input and one for the target/label?

1 Like

Just inputs , no targets as it is unsupervised learning

But the object returned by your `Dataloader`

seems to be a `list`

, so perhaps it’s just a `list`

of a single Tensor? Or perhaps it’s return a batch of lists?

1 Like

Hi,

You should change the `collate_fn`

, something like this

```
import torch
from torch.utils.data import TensorDataset, DataLoader
def collate_fn(batch):
batch = torch.cat([sample[0].unsqueeze(0) for sample in batch], dim=0)
return batch
tensor_x = torch.tensor(Mat) #Mat is a 2d numpy array
dataset = TensorDataset(tensor_x)
loader = DataLoader(dataset, batch_size = 8, shuffle=True, collate_fn=collate_fn)
```

2 Likes