【问题标题】:Reshape model input LSTM重塑模型输入 LSTM
【发布时间】:2020-04-30 11:52:01
【问题描述】:

大家好,我正在尝试建立一个模型来预测语音中的情绪。 由于音频有不同的长度,特征矩阵也有不同的长度,因此我有一个可变的时间步长。 我阅读了其他答案,我可以将 LSTM 的输入形状保留如下:model.add(LSTM(1, input_shape=(None, 31)))

接下来,我需要将我的输入训练数据(它是可变时间步长(Tx,Features)的 2D 数组列表)重塑为 Model.fit() 的长度(1,Tx,Features)3D 数组列表

我希望我所做的事情背后的逻辑是正确的,但无论如何我在重塑时会出错。我确保类型正确,我不知道可能是什么问题,代码如下:

def rnn(x_train, x_test, y_train, y_test):

    for e in x_train:
        print(type(e))
        print(type(e.shape[0]))
        print(type(e.shape[1]))
        x_train[e]=np.reshape(e,(1,e.shape[0],e.shape[1]))

    model = Sequential()

    model.add(LSTM(1, input_shape=(None, 31)))
    model.add(Dropout(0.2))

    model.add(Dense(32, activation='relu'))
    model.add(Dropout(0.2))

    model.add(Dense(5, activation='softmax'))

    opt = tf.keras.optimizers.Adam(lr=1e-3, decay=1e-5)

    model.compile(loss='sparse_categorical_crossentropy', optimizer=opt, metrics=['accuracy'])

    model.fit(x_train, y_train, epochs=3, validation_data=(x_test, y_test))
Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.3.3\plugins\python-ce\helpers\pydev\pydevd.py", line 1434, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.3.3\plugins\python-ce\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/Users/mp95/PycharmProjects/Thesis/Main.py", line 16, in <module>
    rnn(x_train, x_test, y_train, y_test)
  File "C:\Users\mp95\PycharmProjects\Thesis\models.py", line 15, in rnn
    x_train[e]=np.reshape(e,(1,e.shape[0],e.shape[1]))
TypeError: only integer scalar arrays can be converted to a scalar index
<class 'numpy.ndarray'>
<class 'int'>
<class 'int'>

非常感谢,我的数据集很小(450 个示例),还有关于我应该如何处理模型的任何其他提示,非常欢迎,因为我是这些东西的新手!

【问题讨论】:

    标签: python tensorflow keras lstm recurrent-neural-network


    【解决方案1】:

    我对此也很陌生,但根据我的发现: LSTM 的输入是 3-D [样本、时间步长、特征] 欲了解更多信息,请访问此链接: https://stats.stackexchange.com/questions/264546/difference-between-samples-time-steps-and-features-in-neural-network 我也认为你的问题出在这一行 x_train[e]=np.reshape(e,(1,e.shape[0],e.shape[1])) 可能尺寸不对

    【讨论】: