You are making a text predictor language model that takes a "inputsize" section of text…
- User
You are making a text predictor language model that takes a "input_size" section of text and predicts "output_size" continuation characters. Given a pytorch tensor of shape [raw_values_amount] that acts as the dataset, create a DataLoader that extracts batch_size randomly selected sections of the dataset.
- Assistant
Here is a possible implementation of the DataLoader:
import torch from torch.utils.data import Dataset, DataLoader class TextPredictorDataset(Dataset): def __init__(self, raw_values, input_size, output_size): self.raw_values = raw_values self.input_size = input_size self.output_size = output_size def __len__(self): return len(self.raw_values) - self.input_size - self.output_size + 1 def __getitem__(self, idx): input_seq = self.raw_values[idx:idx+self.input_size] output_seq = self.raw_values[idx+self.input_size:idx+self.input_size+self.output_size] return input_seq, output_seq def collate_fn(batch): inputs = [torch.tensor(item[0]) for item in batch] outputs = [torch.tensor(item[1]) for item in batch] return torch.stack(inputs, dim=0), torch.stack(outputs, dim=0) def create_dataloader(raw_values, input_size, output_size, batch_size): dataset = TextPredictorDataset(raw_values, input_size, output_size) dataloader = DataLoader(dataset, batch_size=batch_size, shuffle=True, collate_fn=collate_fn) return dataloader - User
Ok, now simplify the code. The class shouldn't be required to achieve the goal.
- Assistant
Sure, here's a simplified version that doesn't use a separate dataset class:
import torch from torch.utils.data import DataLoader def create_dataloader(raw_values, input_size, output_size, batch_size): dataset = [(raw_values[i:i+input_size], raw_values[i+input_size:i+input_size+output_size]) for i in range(len(raw_values) - input_size - output_size + 1)] dataloader = DataLoader(dataset, batch_size=batch_size, shuffle=True) return dataloaderThis version creates a list of input/output sequences on the fly, instead of precomputing them in a dataset class. The rest of the code is similar to the previous version.
Here's an example of how to use this version of the DataLoader:
raw_values_amount = 1000 raw_values = torch.randint(100, (raw_values_amount,)) input_size = 10 output_size = 5 batch_size = 16 dataloader = create_dataloader(raw_values, input_size, output_size, batch_size) for inputs, outputs in dataloader: print(inputs.shape, outputs.shape) # should print [batch_size, input_size] and [batch_size, output_size]This achieves the same result as before but with simpler code.
- User
Ok, now make use of the "collate_fn" argument to DataLoader with a lambda function.
- Assistant
Sure, here's how to use a
lambdafunction as thecollate_fnargument:import torch from torch.utils.data import DataLoader def create_dataloader(raw_values, input_size, output_size, batch_size): dataset = [(raw_values[i:i+input_size], raw_values[i+input_size:i+input_size+output_size]) for i in range(len(raw_values) - input_size - output_size + 1)] dataloader = DataLoader(dataset, batch_size=batch_size, shuffle=True, collate_fn=lambda batch: tuple(torch.stack(samples, dim=0) for samples in zip(*batch))) return dataloaderThe
collate_fnargument is now alambdafunction that zips the batches of input and output sequences, stacks them into tensors along the first dimension, and returns a tuple of the input and output tensors.Here's an example of how to use this version of the DataLoader:
raw_values_amount = 1000 raw_values = torch.randint(100, (raw_values_amount,)) input_size = 10 output_size = 5 batch_size = 16 dataloader = create_dataloader(raw_values, input_size, output_size, batch_size) for inputs, outputs in dataloader: print(inputs.shape, outputs.shape) # should print [batch_size, input_size] and [batch_size, output_size] - User
The "dataset" variables becomes huge if it prepares explicitely the batches. Rewrite the code such that the batches are extracted dynamically during the training loop.
- Assistant
Sure, here's a version that extracts the batches dynamically during the training loop:
import torch from torch.utils.data import DataLoader def create_dataloader(raw_values, input_size, output_size, batch_size): dataloader = DataLoader(raw_values, batch_size=batch_size, shuffle=True) return dataloader def generator(raw_values, input_size, output_size): for batch in raw_values: for i in range(len(batch) - input_size - output_size + 1): input_seq = batch[i:i+input_size] output_seq = batch[i+input_size:i+input_size+output_size] yield input_seq, output_seq input_size = 10 output_size = 5 batch_size = 16 raw_values_amount = 1000 raw_values = [torch.randint(100, (1000,)) for _ in range(raw_values_amount // 1000)] dataloader = create_dataloader(raw_values, input_size+output_size, 1, batch_size) for inputs, outputs in dataloader: inputs = inputs.view(-1, input_size+output_size+1)[:, :-1] inputs = [inputs[i:i+batch_size] for i in range(0, len(inputs), batch_size)] outputs = outputs.view(-1, input_size+output_size+1)[:, input_size:] outputs = [outputs[i:i+batch_size] for i in range(0, len(outputs), batch_size)] for batch_inputs, batch_outputs in zip(inputs, outputs): print(batch_inputs.shape, batch_outputs.shape) # should print [batch_size, input_size] and [batch_size, output_size] # train(model, batch_inputs, batch_outputs)