How to get mini-batches in pytorch in a clean and efficient way?

前端 未结 6 1439
北恋
北恋 2021-01-30 08:48

I was trying to do a simple thing which was train a linear model with Stochastic Gradient Descent (SGD) using torch:

import numpy as np

import torch
from torch.         


        
6条回答
  •  深忆病人
    2021-01-30 09:33

    An alternative could be using pd.DataFrame.sample

    train = pd.read_csv(TrainSetPath)
    test = pd.read_csv(TestSetPath)
    
    # use df.sample() to shuffle the data frame 
    train = train.sample(frac=1)
    test = test.sample(frac=1)
    
    for i in range(epochs):
            for j in range(batch_per_epoch):
                train_batch = train.sample(n=BatchSize, axis='index',replace=True)
                y_train = train_batch['Target']
                X_train = train_batch.drop(['Target'], axis=1)
                
                # convert data frames to tensors and send them to GPU (if used)
                X_train = torch.tensor(np.mat(X_train)).float().to(device)
                y_train = torch.tensor(np.mat(y_train)).float().to(device)
    

提交回复
热议问题