diff --git a/appendix-E/01_main-chapter-code/previous_chapters.py b/appendix-E/01_main-chapter-code/previous_chapters.py index 992e0968..5bf8adc2 100644 --- a/appendix-E/01_main-chapter-code/previous_chapters.py +++ b/appendix-E/01_main-chapter-code/previous_chapters.py @@ -451,6 +451,9 @@ def _longest_encoded_length(self): if encoded_length > max_length: max_length = encoded_length return max_length + # Note: A more pythonic version to implement this method + # is the following, which is also used in the next chapter: + # return max(len(encoded_text) for encoded_text in self.encoded_texts) @torch.no_grad() # Disable gradient tracking for efficiency diff --git a/ch06/01_main-chapter-code/ch06.ipynb b/ch06/01_main-chapter-code/ch06.ipynb index 9299460e..788069e1 100644 --- a/ch06/01_main-chapter-code/ch06.ipynb +++ b/ch06/01_main-chapter-code/ch06.ipynb @@ -777,7 +777,10 @@ " encoded_length = len(encoded_text)\n", " if encoded_length > max_length:\n", " max_length = encoded_length\n", - " return max_length" + " return max_length\n", + " # Note: A more pythonic version to implement this method\n", + " # is the following, which is also used in the next chapter:\n", + " # return max(len(encoded_text) for encoded_text in self.encoded_texts)" ] }, { diff --git a/ch06/01_main-chapter-code/gpt_class_finetune.py b/ch06/01_main-chapter-code/gpt_class_finetune.py index 1a8a24e1..dc025662 100644 --- a/ch06/01_main-chapter-code/gpt_class_finetune.py +++ b/ch06/01_main-chapter-code/gpt_class_finetune.py @@ -132,7 +132,9 @@ def _longest_encoded_length(self): if encoded_length > max_length: max_length = encoded_length return max_length - + # Note: A more pythonic version to implement this method + # is the following, which is also used in the next chapter: + # return max(len(encoded_text) for encoded_text in self.encoded_texts) def calc_accuracy_loader(data_loader, model, device, num_batches=None): model.eval() diff --git a/ch06/02_bonus_additional-experiments/additional_experiments.py b/ch06/02_bonus_additional-experiments/additional_experiments.py index a92f1be9..eb1ee25b 100644 --- a/ch06/02_bonus_additional-experiments/additional_experiments.py +++ b/ch06/02_bonus_additional-experiments/additional_experiments.py @@ -94,6 +94,9 @@ def _longest_encoded_length(self, tokenizer): if encoded_length > max_length: max_length = encoded_length return max_length + # Note: A more pythonic version to implement this method + # is the following, which is also used in the next chapter: + # return max(len(encoded_text) for encoded_text in self.encoded_texts) def download_and_unzip(url, zip_path, extract_to, new_file_path): diff --git a/ch06/03_bonus_imdb-classification/train_bert_hf_spam.py b/ch06/03_bonus_imdb-classification/train_bert_hf_spam.py index 7e0adc74..6ba5c8be 100644 --- a/ch06/03_bonus_imdb-classification/train_bert_hf_spam.py +++ b/ch06/03_bonus_imdb-classification/train_bert_hf_spam.py @@ -51,6 +51,9 @@ def _longest_encoded_length(self, tokenizer): if encoded_length > max_length: max_length = encoded_length return max_length + # Note: A more pythonic version to implement this method + # is the following, which is also used in the next chapter: + # return max(len(encoded_text) for encoded_text in self.encoded_texts) def download_and_unzip(url, zip_path, extract_to, new_file_path):