我想制作一个数据集NumPy
,然后想训练和测试一个简单的模型,比如“线性或逻辑”。
我正在努力学习Pytorch Lightning
。我找到了一个教程,我们可以使用 NumPy 数据集,并且可以在这里使用均匀分布。作为一个新人,我没有得到完整的想法,我该怎么做!
我的代码如下
import numpy as np
import pytorch_lightning as pl
from torch.utils.data import random_split, DataLoader, TensorDataset
import torch
from torch.autograd import Variable
from torchvision import transforms
np.random.seed(42)
device = 'cuda' if torch.cuda.is_available() else 'cpu'
class DataModuleClass(pl.LightningDataModule):
def __init__(self):
super().__init__()
self.constant = 2
self.batch_size = 10
self.transform = transforms.Compose([
transforms.ToTensor()
])
def prepare_data(self):
a = np.random.uniform(0, 500, 500)
b = np.random.normal(0, self.constant, len(x))
c = a + b
X = np.transpose(np.array([a, b]))
idx = np.arange(500)
np.random.shuffle(idx)
# Uses foirst 400 random indices for training
train_idx = idx[:400]
# Uses the remaining indices for validation
val_idx = idx[400:]
# Generate train and validation dataset
x_train, y_train = X[train_idx], y[train_idx]
x_val, y_val = X[val_idx], y[val_idx]
# Converting numpy array to Tensor
self.x_train_tensor = torch.from_numpy(x_train).float().to(device)
self.y_train_tensor = torch.from_numpy(y_train).float().to(device)
self.x_val_tensor = torch.from_numpy(x_val).float().to(device)
self.y_val_tensor = torch.from_numpy(y_val).float().to(device)
training_dataset = TensorDataset(self.x_train_tensor, self.y_train_tensor)
validation_dataset = TensorDataset(self.x_val_tensor, self.y_val_tensor)
return training_dataset, validation_dataset
def train_dataloader(self):
training_dataloader = prepare_data() # Most probably this is wrong way!!!
return DataLoader(self.training_dataloader)
def val_dataloader(self):
validation_dataloader = prepare_data() # Most probably this is wrong way!!!
return DataLoader(self.validation_dataloader)
# def test_dataloader(self):
obj = DataModuleClass()
print(obj.prepare_data())
这部分是根据给出的答案 [Here, I want to take a and b as features
and c
as label or target variable
.]
现在,如何将数据集传递到“训练和验证方法”中?