Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

torch dataloader for large csv file - incremental loading

I am trying to write a custom torch data loader so that large CSV files can be loaded incrementally (by chunks).

I have a rough idea of how to do that. However, I keep getting some PyTorch error that I do not know how to solve.


import numpy as np
import pandas as pd
import torch
from torch.utils.data import Dataset, DataLoader

# Create dummy csv data
nb_samples = 110
a = np.arange(nb_samples)
df = pd.DataFrame(a, columns=['data'])
df.to_csv('data.csv', index=False)


# Create Dataset
class CSVDataset(Dataset):
    def __init__(self, path, chunksize, nb_samples):
        self.path = path
        self.chunksize = chunksize
        self.len = nb_samples / self.chunksize

    def __getitem__(self, index):
        x = next(
            pd.read_csv(
                self.path,
                skiprows=index * self.chunksize + 1,  #+1, since we skip the header
                chunksize=self.chunksize,
                names=['data']))
        x = torch.from_numpy(x.data.values)
        return x

    def __len__(self):
        return self.len


dataset = CSVDataset('data.csv', chunksize=10, nb_samples=nb_samples)
loader = DataLoader(dataset, batch_size=10, num_workers=1, shuffle=False)

for batch_idx, data in enumerate(loader):
    print('batch: {}\tdata: {}'.format(batch_idx, data))

I get 'float' object cannot be interpreted as an integer error

like image 752
Petr Avatar asked Oct 27 '25 10:10

Petr


1 Answers

The error is caused by this line:

self.len = nb_samples / self.chunksize

When dividing using / the result is always a float. But you can only return an integer in the __len__() function. Therefore you have to round self.len and/or convert it to an integer. For example by simply doing this:

self.len = nb_samples // self.chunksize

the double slash (//) rounds down and converts to integer.

Edit: You acutally CAN return a float in __len__() but when calling len(dataset) the error will occur. So I guess len(dataset) is called somewhere inside the DataLoader class.

like image 60
Theodor Peifer Avatar answered Oct 29 '25 09:10

Theodor Peifer