我实现了一个生成器函数来生成一个热编码向量,但是生成器实际上抛出了错误
我使用生成器函数来生成一个热编码向量,因为后者将用作深度学习lstm模型的输入。我这样做是为了避免在非常大的数据集上创建一个热编码时出现过多的负载和内存故障。但是,我没有得到错误的生成器函数。我需要帮助找出哪里出了问题。你知道吗
前面的代码:
X = np.zeros((len(sequences), seq_length, vocab_size), dtype=np.bool)
y = np.zeros((len(sequences), vocab_size), dtype=np.bool)
for i, sentence in enumerate(sequences):
for t, word in enumerate(sentence):
X[i, t, vocab[word]] = 1
y[i, vocab[next_words[i]]] = 1
在这里
sequences = sentences generated from data set
seq_length = length of each sentence(this is constant)
vocab_size = number of unique words in dictionary
My program when run on the large data set produces,
sequences = 44073315
seq_length = 30
vocab_size = 124958
因此,当上面的代码直接用于后面的输入时,就会产生beloe错误。你知道吗
Traceback (most recent call last):
File "1.py", line 206, in <module>
X = np.zeros((len(sequences), seq_length, vocab_size), dtype=np.bool)
MemoryError
(my_env) [rjagannath1@login ~]$
因此,我尝试创建一个生成器函数(用于测试),如下所示-
def gen(batch_size, no_of_sequences, seq_length, vocab_size):
bs = batch_size
ns = no_of_sequences
X = np.zeros((batch_size, seq_length, vocab_size), dtype=np.bool)
y = np.zeros((batch_size, vocab_size), dtype=np.bool)
while(ns > bs):
for i, sentence in enumerate(sequences):
for t, word in enumerate(sentence):
X[i, t, vocab[word]] = 1
y[i, vocab[next_words[i]]] = 1
print(X.shape())
print(y.shape())
yield(X, y)
ns = ns - bs
for item in gen(1000, 44073315, 30, 124958):
print(item)
但我得到下面的错误-
File "path_of_file", line 247, in gen
X[i, t, vocab[word]] = 1
IndexError: index 1000 is out of bounds for axis 0 with size 1000
我在发电机功能上犯了什么错误?你知道吗
在生成器中修改如下:
基本上,您希望在大小为
batch_size
的窗口上运行,因此您正在通过sequences
生成一个运行切片,它似乎是您的整个数据集。你知道吗您还必须增加
batch_i
,将其放在yield
之后,因此添加batch_i+=1
相关问题 更多 >
编程相关推荐