From 87aab925d2a5285f67d95f56aebfa9114798189f Mon Sep 17 00:00:00 2001 From: Chang Luo <33987852+luochang212@users.noreply.github.com> Date: Thu, 9 May 2024 02:49:22 +0800 Subject: [PATCH] fix offset --- .../language-models-and-dataset.md | 6 +++--- .../language-models-and-dataset_origin.md | 6 +++--- d2l/mxnet.py | 2 +- d2l/paddle.py | 2 +- d2l/tensorflow.py | 2 +- d2l/torch.py | 2 +- 6 files changed, 10 insertions(+), 10 deletions(-) diff --git a/chapter_recurrent-neural-networks/language-models-and-dataset.md b/chapter_recurrent-neural-networks/language-models-and-dataset.md index 0b44c1ae0..ed66337c2 100644 --- a/chapter_recurrent-neural-networks/language-models-and-dataset.md +++ b/chapter_recurrent-neural-networks/language-models-and-dataset.md @@ -334,7 +334,7 @@ for X, Y in seq_data_iter_random(my_seq, batch_size=2, num_steps=5): def seq_data_iter_sequential(corpus, batch_size, num_steps): #@save """使用顺序分区生成一个小批量子序列""" # 从随机偏移量开始划分序列 - offset = random.randint(0, num_steps) + offset = random.randint(0, num_steps - 1) num_tokens = ((len(corpus) - offset - 1) // batch_size) * batch_size Xs = d2l.tensor(corpus[offset: offset + num_tokens]) Ys = d2l.tensor(corpus[offset + 1: offset + 1 + num_tokens]) @@ -351,7 +351,7 @@ def seq_data_iter_sequential(corpus, batch_size, num_steps): #@save def seq_data_iter_sequential(corpus, batch_size, num_steps): #@save """使用顺序分区生成一个小批量子序列""" # 从随机偏移量开始划分序列 - offset = random.randint(0, num_steps) + offset = random.randint(0, num_steps - 1) num_tokens = ((len(corpus) - offset - 1) // batch_size) * batch_size Xs = d2l.tensor(corpus[offset: offset + num_tokens]) Ys = d2l.tensor(corpus[offset + 1: offset + 1 + num_tokens]) @@ -369,7 +369,7 @@ def seq_data_iter_sequential(corpus, batch_size, num_steps): #@save def seq_data_iter_sequential(corpus, batch_size, num_steps): #@save """使用顺序分区生成一个小批量子序列""" # 从随机偏移量开始划分序列 - offset = random.randint(0, num_steps) + offset = random.randint(0, num_steps - 1) num_tokens = ((len(corpus) - offset - 1) // batch_size) * batch_size Xs = d2l.tensor(corpus[offset: offset + num_tokens]) Ys = d2l.tensor(corpus[offset + 1: offset + 1 + num_tokens]) diff --git a/chapter_recurrent-neural-networks/language-models-and-dataset_origin.md b/chapter_recurrent-neural-networks/language-models-and-dataset_origin.md index 3941261c3..45ba311ae 100644 --- a/chapter_recurrent-neural-networks/language-models-and-dataset_origin.md +++ b/chapter_recurrent-neural-networks/language-models-and-dataset_origin.md @@ -283,7 +283,7 @@ in each subsequence. def seq_data_iter_random(corpus, batch_size, num_steps): #@save """Generate a minibatch of subsequences using random sampling.""" # Start with a random offset to partition a sequence - corpus = corpus[random.randint(0, num_steps):] + corpus = corpus[random.randint(0, num_steps - 1):] # Subtract 1 since we need to account for labels num_subseqs = (len(corpus) - 1) // num_steps # The starting indices for subsequences of length `num_steps` @@ -333,7 +333,7 @@ This strategy preserves the order of split subsequences when iterating over mini def seq_data_iter_sequential(corpus, batch_size, num_steps): #@save """Generate a minibatch of subsequences using sequential partitioning.""" # Start with a random offset to partition a sequence - offset = random.randint(0, num_steps) + offset = random.randint(0, num_steps - 1) num_tokens = ((len(corpus) - offset - 1) // batch_size) * batch_size Xs = d2l.tensor(corpus[offset: offset + num_tokens]) Ys = d2l.tensor(corpus[offset + 1: offset + 1 + num_tokens]) @@ -350,7 +350,7 @@ def seq_data_iter_sequential(corpus, batch_size, num_steps): #@save def seq_data_iter_sequential(corpus, batch_size, num_steps): #@save """Generate a minibatch of subsequences using sequential partitioning.""" # Start with a random offset to partition a sequence - offset = random.randint(0, num_steps) + offset = random.randint(0, num_steps - 1) num_tokens = ((len(corpus) - offset - 1) // batch_size) * batch_size Xs = d2l.tensor(corpus[offset: offset + num_tokens]) Ys = d2l.tensor(corpus[offset + 1: offset + 1 + num_tokens]) diff --git a/d2l/mxnet.py b/d2l/mxnet.py index 25fc6eaf3..79726ea87 100644 --- a/d2l/mxnet.py +++ b/d2l/mxnet.py @@ -609,7 +609,7 @@ def seq_data_iter_sequential(corpus, batch_size, num_steps): Defined in :numref:`sec_language_model`""" # 从随机偏移量开始划分序列 - offset = random.randint(0, num_steps) + offset = random.randint(0, num_steps - 1) num_tokens = ((len(corpus) - offset - 1) // batch_size) * batch_size Xs = d2l.tensor(corpus[offset: offset + num_tokens]) Ys = d2l.tensor(corpus[offset + 1: offset + 1 + num_tokens]) diff --git a/d2l/paddle.py b/d2l/paddle.py index 6c5813aeb..ccb816ce6 100644 --- a/d2l/paddle.py +++ b/d2l/paddle.py @@ -670,7 +670,7 @@ def seq_data_iter_sequential(corpus, batch_size, num_steps): Defined in :numref:`sec_language_model`""" # 从随机偏移量开始划分序列 - offset = random.randint(0, num_steps) + offset = random.randint(0, num_steps - 1) num_tokens = ((len(corpus) - offset - 1) // batch_size) * batch_size Xs = d2l.tensor(corpus[offset: offset + num_tokens]) Ys = d2l.tensor(corpus[offset + 1: offset + 1 + num_tokens]) diff --git a/d2l/tensorflow.py b/d2l/tensorflow.py index 85705dceb..b4c0f59f2 100644 --- a/d2l/tensorflow.py +++ b/d2l/tensorflow.py @@ -629,7 +629,7 @@ def seq_data_iter_sequential(corpus, batch_size, num_steps): Defined in :numref:`sec_language_model`""" # 从随机偏移量开始划分序列 - offset = random.randint(0, num_steps) + offset = random.randint(0, num_steps - 1) num_tokens = ((len(corpus) - offset - 1) // batch_size) * batch_size Xs = d2l.tensor(corpus[offset: offset + num_tokens]) Ys = d2l.tensor(corpus[offset + 1: offset + 1 + num_tokens]) diff --git a/d2l/torch.py b/d2l/torch.py index 77e5f3577..d73edd5d0 100644 --- a/d2l/torch.py +++ b/d2l/torch.py @@ -657,7 +657,7 @@ def seq_data_iter_sequential(corpus, batch_size, num_steps): Defined in :numref:`sec_language_model`""" # 从随机偏移量开始划分序列 - offset = random.randint(0, num_steps) + offset = random.randint(0, num_steps - 1) num_tokens = ((len(corpus) - offset - 1) // batch_size) * batch_size Xs = d2l.tensor(corpus[offset: offset + num_tokens]) Ys = d2l.tensor(corpus[offset + 1: offset + 1 + num_tokens])