【问题标题】:Keras + LSTM/RNN: trouble with dimensionality of `X`'s for new predictionsKeras + LSTM/RNN:新预测的“X”维数问题
【发布时间】:2017-07-29 17:42:45
【问题描述】:

总结

在模型已经正确生成后,我无法将我的输入数据的正确尺寸提供给预测

我收到以下错误:

ValueError: Error when checking : expected lstm_13_input to have shape (None, 40, 39) but got array with shape (1, 39, 39)

背景

  • 使用 Anaconda 作为我的虚拟环境
  • Keras 2.0.6 版
  • TensorFlow 版本 1.1.0

我正在创建一个非常接近 this tutorial 的示例。

代码

以下是相关的sn-ps代码:

from keras.models import Sequential
from keras.layers import Dense, Activation, Dropout
from keras.layers import LSTM,TimeDistributed,SimpleRNN
from keras.utils.data_utils import get_file
import numpy as np
from time import sleep
import random
import sys

...

X = np.zeros((len(sentences), maxlen, len(chars)), dtype=np.bool)
y = np.zeros((len(sentences),maxlen, len(chars)), dtype=np.bool) # y is also a sequence , or  a seq of 1 hot vectors
for i, sentence in enumerate(sentences):
    for t, char in enumerate(sentence):
        X[i, t, char_indices[char]] = 1

for i, sentence in enumerate(next_chars):
    for t, char in enumerate(sentence):
        y[i, t, char_indices[char]] = 1

...

model = Sequential()
model.add(LSTM(512, return_sequences=True, input_shape=(maxlen, len(chars))))  # original one
model.add(LSTM(512, return_sequences=True)) #- original
model.add(Dropout(0.2))
model.add(TimeDistributed(Dense(len(chars))))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', optimizer='rmsprop')

...

history=model.fit(X, y, batch_size=128, nb_epoch=1,verbose=0)

...

seed_string="brutus:\nbeing so moved, he will not spare"
x=np.zeros((1, len(seed_string), len(chars)))
for t, char in enumerate(seed_string):
    x[0, t, char_indices[char]] = 1.
preds = model.predict(x, verbose=0)[0]

错误

在最后一行出现错误,并出现以下错误:

ValueError: Error when checking : expected lstm_13_input to have shape (None, 40, 39) but got array with shape (1, 39, 39)

努力解决

我玩过 seed_string 和由它生成的 x 的维度,但无论我如何尝试调整它们,我总是有某种不匹配由于None 的这个要求(我认为)。需要明确的是,我从种子字符串中添加或删除了字符,因此它是40 字符。但是,当我将它设置为40 时,错误说我实际上有41,而当我将它设置为39 时,它说我有39,如上所示。还有其他事情——我不明白——在这里发生。

我查看了 Reshape's codeexample of how to use it,但由于 Keras 的 Reshape 是用于模型层,我什至不明白我是怎么做到的可以使用它来重塑预测的输入,而 Numpy 无法重塑创建 None 维度(至少据我所知)。

【问题讨论】:

    标签: python keras lstm dimensions rnn


    【解决方案1】:

    seed_string 的长度需要与maxlen 匹配。您可以使用pad_sequences 处理字符串比maxlen 更短或更长的情况。在你的情况下,你的字符串太长了。

    from keras.preprocessing.sequence import pad_sequences
    
    seed_string = "brutus:\nbeing so moved, he will not spare"
    
    x = np.zeros((1, len(seed_string), len(chars)))
    
    for t, char in enumerate(seed_string):
        x[0, t, char_indices[char]] = 1.
    
    x = pad_sequences(x, maxlen=maxlen)
    
    preds = model.predict(x, verbose=0)[0]
    

    【讨论】:

    • 嗨@Nicole 感谢您的建议,但正如我在原始问题中所说,我已经这样做了。为了清楚起见,我看到 4039(None, 40, 39)(1, 39, 39) 的大小不匹配。但是种子字符串的大小不是问题,尽管看起来确实如此。 (事实上​​,它会说它要么是 39,要么跳到 41,即使我只添加了 1 个字符)。这个问题似乎是别的,我不能说清楚。
    • 您是真的尝试使用 pad_sequences 还是只是尝试更改字符串的长度?
    • 我只是改变了长度。我不知道pad_sequence。我会试试的。但是,我确实已经将它们作为具有适当维数的 numpy 数组。 (例如,我缩短了字符串的长度,then 将字符串向量化为一个单热编码的 numpy 多维数组,具有正确的 # 维数。)
    • 我问,因为在我看来,您的代码中很可能存在错误,并且您为 x 传递的格式错误。我可以重现您的错误的唯一方法是使x 尺寸错误。如果您可以粘贴您正在执行的操作的完整复制品以确保字符串的长度正确,那将很有帮助。在传递给predict之前尝试打印x.shape。还可以尝试使用pad_sequences,因为它可以确保您拥有正确的时间步长维度,无论您在其他地方尝试操作其维度时做了什么。
    猜你喜欢
    • 2017-01-01
    • 1970-01-01
    • 2021-11-03
    • 2018-01-03
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 2021-04-21
    • 1970-01-01
    相关资源
    最近更新 更多