How to use numpy dataset in Pytorch Lightning
I want to make a dataset using NumPy and then want to train and test a simple model like 'linear, or logistic`.
I am trying to learn Pytorch Lightning. I have found a tutorial that we can use the NumPy dataset and can use uniform distribution here. As a newcomer, I am not getting the full idea, how can I do that!
My code is given below
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())  

This part is done based on the answer given [Here, I want to take a and b as features and c as label or target variable.]
Now, how, can I pass the dataset into the `training and validation method?

You can get the data from prepare_data() or setup() both using the following code.
def prepare_data(self):
    a = np.random.uniform(0, 500, 500)
    b = np.random.normal(0, self.constant, len(a))

    c = a + b
    X = np.transpose(np.array([a, b]))

    # Converting numpy array to Tensor
    self.x_train_tensor = torch.from_numpy(X).float().to(device)
    self.y_train_tensor = torch.from_numpy(c).float().to(device)

    training_dataset = TensorDataset(self.x_train_tensor, self.y_train_tensor)

    self.training_dataset = training_dataset

def setup(self):
    data = self.training_dataset
    self.train_data, self.val_data = random_split(data, [400, 100])

def train_dataloader(self):
    return DataLoader(self.train_data)

def val_dataloader(self):
    return DataLoader(self.val_data)

You can split the dataset using random_split().
