How to ensure the dataset is shuffled for each epoch using Trainer and Datasets?
I am using theSeq2SeqTrainerand pass andatasets.arrow_dataset.Datasetastrain_datasetwhen initiating the object. Is the dataset by default shuffled per epoch? If not, how to make it shuffled?An example is from the official example:transformers/run_seq2seq.py at master · huggingface/transformers · GitHubThanks!
2021-03-07T19:52:48Z
Still needs help…
2021-03-08T10:02:15Z
The Seq2SeqTrainer (as well as the standard Trainer) uses a PyTorch Sampler to shuffle the dataset. At each epoch, it does shuffle the dataset and it also groups the samples of roughly the same length size. You can find the Sampler definitionhere.
2022-04-21T03:55:22Z
Hi, Is there a parameters that controls whether or not the data get reshuffled before each epoch? And whether or not it is grouped by length? Thanks!Additionally, if the training is aborted and I’m restarting from a checkpoint - does the checkpoint have information about the shuffling order for this given epoch and which datapoints still haven’t gone through this epoch already? Thanks!
2022-04-21T12:28:34Z
dashapyly:Is there a parameters that controls whether or not the data get reshuffled before each epoch?No, this is would be very bad practice so we don’t offer that option.dashapyly:And whether or not it is grouped by length?That would be thegroup_by_lengthargument.dashapyly:Additionally, if the training is aborted and I’m restarting from a checkpoint - does the checkpoint have information about the shuffling order for this given epoch and which datapoints still haven’t gone through this epoch already?Yes training will resume with the same shuffle, at the same point you were at the time of the save.
2022-04-21T13:45:20Z
thank you!
2023-02-21T03:19:43Z
Hi Sgugger, why is it a bad practice to reshuffle the dataset at every epoch?I thought reshuffle the dataset at every epoch can reduce overfitting and improve the generalization performance of the model. By shuffling the dataset, we ensure that the model is exposed to a different sequence of samples in each epoch, which can help to prevent it from memorizing the order of the training data and overfitting to specific patterns.Shuffling the dataset also helps to improve the diversity of the mini-batches during training, which can improve the robustness of the model and make it more resistant to outliers or noise in the data.
2023-02-21T08:31:22Z
Yes that’s why we don’t offer the option tonotreshuffle the data at each epoch for the training set.
2023-10-15T18:19:26Z
be careful with the word “NOT”
2024-08-12T19:25:40Z
sgugger:Yes that’s why we don’t offer the option tonotreshuffle the data at each epoch for the training set.what are you referring by “that”@sgugger? Thanks in advance.
2024-08-12T19:30:05Z
I assume that at the very least at the beginning of training, shuffle is true is the recommended approach? e.g., just once with something like this:jsonl_files = glob.glob(os.path.expanduser(train_path), recursive=True)
    train_dataset = load_dataset('json', data_files=jsonl_files, split='train')
    train_dataset = train_dataset.select(range(200))
    print(f'->{len(train_dataset)=}')
    _turns_str_2_desired_str = lambda examples: turns_str_2_desired_str(examples, tokenizer, 'turns')
    train_dataset = raw_ds_2_lm_ds_mask_eos_pad_toks(train_dataset, tokenizer, max_length, raw_str_2_desired_str=_turns_str_2_desired_str)
    train_dataset = train_dataset.shuffle(seed=42)@sguggerDo you agree?
2024-08-12T19:30:28Z
nyust-eb210:Shuffling the dataset also helps to improve the diversity of the mini-batches during training, which can improve the robustness of the model and make it more resistant to outliers or noise in the data.This is a good comment.