关于这个特殊的错误,我见过很多问题。我相信我的问题是不同的,足以保证自己的职位。你知道吗
我的目标:我正在建立一个生成新闻标题的RNN。它将根据前面的单词来预测下一个单词。这段代码来自一个example,我正在尝试调整它以适应我的情况。我试图将数组分为X
和y
。你知道吗
问题:
我知道出现这个错误是因为数组被索引时好像它是一个二维数组,但实际上它是一个一维数组。在将sequences
转换为数组之前,它是一个列表列表,但并非所有嵌套列表的长度都相同,因此numPy会将其转换为1d数组。你知道吗
我的问题是:有没有一种简单或优雅的方法来填充sequences
,使所有的列表长度相同?我可以用空格来保持较短标题中相同的意思吗?为什么要将列表列表更改为数组?正如我之前所说,这是一个例子,我试图了解他们做了什么,为什么。你知道吗
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Pretreat Data Section
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
# integer encode sequences of words
# create the tokenizer
t = Tokenizer()
# fit the tokenizer on the headlines
t.fit_on_texts(headlines)
sequences = t.texts_to_sequences(headlines)
# vocabulary size
vocab_size = len(t.word_index) + 1
#separate into input and output
sequences = np.array(sequences)
X, y = sequences[:,:-1], sequences[:,-1] # fix this
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-87-eb7aab0c3a22> in <module>
18 #separate into input and output
19 sequences = np.array(sequences)
---> 20 X, y = sequences[:,:-1], sequences[:,-1] # fix this
21 y = to_categorical(y, num_classes=vocab_size)
22 seq_length = X.shape[1]
IndexError: too many indices for array
问题是本教程在一个页面上有几个部分,每个部分都有自己的
"Complete Example"
首先
"Complete Example"
从republic_clean.txt
读取文本,清除它并将其保存在republic_sequences.txt
中—它创建具有相同字数的序列。你知道吗第二个
"Complete Example"
从republic_sequences.txt
读取文本并将其与因为第一部分创建了具有相同字数的序列,所以这段代码可以正常工作。你知道吗
你好像跳过了第一部分。你必须回到第一部分学习如何清除文本,以及如何创建正确的文件,你可以在第二部分使用。你知道吗
编辑:如果不能用相同的字数生成序列,则可以在较短的序列中添加空格。代码可以工作,但我不知道它是否能创建更好的模型。你知道吗
结果
相关问题 更多 >
编程相关推荐