skka3134

skka3134

email
telegram

Machine Learning and Quantitative Investment: 3. Creating Datasets with PyTorch

  1. Install pytorch, pytorch is a python-first deep learning framework. Using pytorch can automatically combine factors into strategies. If GPU training is needed, only N cards are supported, so choosing CPU mode here is fine. https://pytorch.org/, torchvision is used for image processing, torchaudio is not needed so it is not installed.
sudo /home/skka3134/folder/bot/bin/python -m pip install torch

image
2. Set up the dataset, inherit from the Dateset class to form TimeSeriesDataset

from torch.utils.data import Dataset

class TimeSeriesDataset(Dataset):
    def __init__(self, X, y):
        self.X = X
        self.y = y

    def __len__(self):
        return len(self.X)

    def __getitem__(self, i):
        return self.X[i], self.y[i]
    
train_dataset = TimeSeriesDataset(X_train, y_train)
test_dataset = TimeSeriesDataset(X_test, y_test)
  1. Load the dataset
from torch.utils.data import DataLoader
batch_size = 16    #Read 16 data points per batch, if training with GPU, can increase, maybe to 128?
train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True) #shuffle=True means shuffle the data
test_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=False) #shuffle=False means do not shuffle the data
  1. Visualization processing
for _, batch in enumerate(train_loader):
    x_batch, y_batch = batch[0].to(device), batch[1].to(device)
    print(x_batch.shape, y_batch.shape)
    break
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.