大家好,我正在尝试建立模型来预测语音情感。由于音频的长度不同,特征矩阵的长度也不同,因此我的时间步长可变。我读了其他答案,我可以保留LSTM的输入形状,如下所示:model.add(LSTM(1, input_shape=(None, 31)))
接下来,我需要将输入的训练数据重塑成3D长度为(C0]的3D数组的列表,它是可变时步长(Tx,Features)的2D数组的列表]
我希望我正在做的背后的逻辑是正确的,但是无论如何我在重塑时都会出错。我确保类型正确,我不知道可能是什么问题,这是代码:
Model.fit()
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))
非常感谢,我的数据集很小(450个示例),还有关于该如何处理该模型的任何其他提示,由于我是新手,因此非常欢迎!
我也是新手,但从我发现的内容中:LSTM的输入是3-D [样本,时间步长,特征]有关更多信息,请访问此链接: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'>
我也认为你的问题在这一行x_train [e] = np.reshape(e,(1,e.shape [0],e.shape [1]))尺寸可能不正确