Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

split custom pytorch dataset

import torch
import numpy as np
from torchvision import datasets
from torchvision import transforms
from torch.utils.data.sampler import SubsetRandomSampler

class CustomDatasetFromCSV(Dataset):
    def __init__(self, csv_path, transform=None):
        ....


dataset = CustomDatasetFromCSV(my_path)
batch_size = 16
validation_split = .2
shuffle_dataset = True
random_seed= 42

# Creating data indices for training and validation splits:
dataset_size = len(dataset)
indices = list(range(dataset_size))
split = int(np.floor(validation_split * dataset_size))
if shuffle_dataset :
    np.random.seed(random_seed)
    np.random.shuffle(indices)
train_indices, val_indices = indices[split:], indices[:split]

# Creating PT data samplers and loaders:
train_sampler = SubsetRandomSampler(train_indices)
valid_sampler = SubsetRandomSampler(val_indices)

train_loader = torch.utils.data.DataLoader(dataset, batch_size=batch_size, 
                                           sampler=train_sampler)
validation_loader = torch.utils.data.DataLoader(dataset, batch_size=batch_size,
                                                sampler=valid_sampler)

# Usage Example:
num_epochs = 10
for epoch in range(num_epochs):
    # Train:   
    for batch_index, (faces, labels) in enumerate(train_loader):
        # ...
Comment

PREVIOUS NEXT
Code Example
Python :: what is mustafa nickname 
Python :: how to wait for loading icon to disappear from the page using selenium python 
Python :: pysimplegui start value 
Python :: how to set date and time rows in order python pandas 
Python :: reverse order of dataframe rows 
Python :: object function in python 
Python :: class python __call__ 
Python :: python json change line 
Python :: How can i restrict letters after a number in an input in Python 
Python :: how to specify a key to be as a break fomction python 
Python :: change state enabled tkinter 
Python :: python use negation with maskedarray 
Python :: empty array numpy python 
Python :: discord chatterbot python 
Python :: subprocess the system cannot find the file specifie 
Python :: Flatten List in Python With Itertools 
Python :: bst deleting 
Python :: python kubernetes client find pod with name 
Python :: add button to python gui file 
Python :: how to stop python for some time in python 
Python :: convert iso 8601 to milliseconds python 
Python :: rgb to grayscale python 
Python :: rotate existing labels python 
Python :: sum of fraction numbers in python 
Python :: parser.add_argument array python 
Python :: if a specific column name is present drop tyhe column 
Python :: python remove specific character from string 
Python :: Acticating virtual environment 
Python :: python casting float to int 
Python :: if statement python 
ADD CONTENT
Topic
Content
Source link
Name
2+4 =