Skip to content

Commit 8cfa52b

Browse files
authored
More pythonic way to find the longest sequence (#512)
* More pythonic way to find the longest sequence * pep8 fix
1 parent 0e14c76 commit 8cfa52b

File tree

5 files changed

+16
-1
lines changed

5 files changed

+16
-1
lines changed

appendix-E/01_main-chapter-code/previous_chapters.py

+3
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,9 @@ def _longest_encoded_length(self):
451451
if encoded_length > max_length:
452452
max_length = encoded_length
453453
return max_length
454+
# Note: A more pythonic version to implement this method
455+
# is the following, which is also used in the next chapter:
456+
# return max(len(encoded_text) for encoded_text in self.encoded_texts)
454457

455458

456459
@torch.no_grad() # Disable gradient tracking for efficiency

ch06/01_main-chapter-code/ch06.ipynb

+4-1
Original file line numberDiff line numberDiff line change
@@ -777,7 +777,10 @@
777777
" encoded_length = len(encoded_text)\n",
778778
" if encoded_length > max_length:\n",
779779
" max_length = encoded_length\n",
780-
" return max_length"
780+
" return max_length\n",
781+
" # Note: A more pythonic version to implement this method\n",
782+
" # is the following, which is also used in the next chapter:\n",
783+
" # return max(len(encoded_text) for encoded_text in self.encoded_texts)"
781784
]
782785
},
783786
{

ch06/01_main-chapter-code/gpt_class_finetune.py

+3
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ def _longest_encoded_length(self):
132132
if encoded_length > max_length:
133133
max_length = encoded_length
134134
return max_length
135+
# Note: A more pythonic version to implement this method
136+
# is the following, which is also used in the next chapter:
137+
# return max(len(encoded_text) for encoded_text in self.encoded_texts)
135138

136139

137140
def calc_accuracy_loader(data_loader, model, device, num_batches=None):

ch06/02_bonus_additional-experiments/additional_experiments.py

+3
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ def _longest_encoded_length(self, tokenizer):
9494
if encoded_length > max_length:
9595
max_length = encoded_length
9696
return max_length
97+
# Note: A more pythonic version to implement this method
98+
# is the following, which is also used in the next chapter:
99+
# return max(len(encoded_text) for encoded_text in self.encoded_texts)
97100

98101

99102
def download_and_unzip(url, zip_path, extract_to, new_file_path):

ch06/03_bonus_imdb-classification/train_bert_hf_spam.py

+3
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ def _longest_encoded_length(self, tokenizer):
5151
if encoded_length > max_length:
5252
max_length = encoded_length
5353
return max_length
54+
# Note: A more pythonic version to implement this method
55+
# is the following, which is also used in the next chapter:
56+
# return max(len(encoded_text) for encoded_text in self.encoded_texts)
5457

5558

5659
def download_and_unzip(url, zip_path, extract_to, new_file_path):

0 commit comments

Comments
 (0)