Guest User

Untitled

a guest
Dec 11th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. import torch
  2. import pandas as pd
  3. from torch.utils.data import Dataset, DataLoader
  4.  
  5. class ExampleDataset(Dataset):
  6. """Example Dataset"""
  7.  
  8. def __init__(self, csv_file):
  9. """
  10. csv_file (string): Path to the csv file containing data.
  11. """
  12. self.data_frame = pd.read_csv(csv_file)
  13.  
  14. def __len__(self):
  15. return len(self.data_frame)
  16.  
  17. def __getitem__(self, idx):
  18. return self.data_frame[idx]
  19.  
  20. # instantiates the dataset
  21. example_dataset = ExampleDataset('my_data_file.csv')
  22.  
  23. # batch size: number of samples returned per iteration
  24. # shuffle: Flag to shuffle the data before reading so you don't read always in the same order
  25. # num_workers: used to load the data in parallel
  26. example_data_loader = DataLoader(example_dataset, , batch_size=4, shuffle=True, num_workers=4)
  27.  
  28. # Loops over the data 4 samples at a time
  29. for batch_index, batch in enumerate(example_data_loader):
  30. print(batch_index, batch)
Add Comment
Please, Sign In to add comment