我有10个数组。每个代表一个数据点(输入)。阵列的形状为(16,3)、(34,3)等。。由于LSTM需要3dim数据,我对这10个数组中的每一个都进行了整形。例如:如果它是(16,3),那么现在它是(1,16,3)。我试着让((1,16,3),(1,34,3),等等…)成为我的数组形状,换句话说,一个numpy数组中有10个数组,每个数组的形状是(1,something,3)。当我将所有10个数组作为一个数组输入数据时,会出现以下错误:
Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 1 array(s), but instead got the following list of 10 arrays.
但是如果我给其中一个数组添加一个标签,它就会工作并且过度拟合(这是应该的)。 如果batch\u size=1,程序不应该从这10个样本中选取一个进行训练吗?你知道吗
这是我的密码:
import os
import numpy as np
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
data = []
directory = 'realData'
for filename in os.listdir(directory):
data.append(np.load('realData/' + filename))
for i in range(len(data)):
data[i] = data[i].reshape(1,data[i].shape[0],3)
sad = np.array([[0]] * 2)
okay = np.array([[1]] * 3)
happy = np.array([[2]] * 2)
perfect = np.array([[3]] * 3)
labels = np.concatenate([sad,okay,happy,perfect],axis=0)
model = Sequential()
model.add(LSTM(32, input_shape=(None,3)))
model.add(Dense(1))
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'])
print('Train...')
model.fit(data, labels,
batch_size=1,
epochs=15,
validation_data=(data, labels))
score, acc = model.evaluate(data, labels, batch_size=1)
print('Test score:', score)
print('Test accuracy:', acc)
您的模型需要一个numpy数组作为输入,其中第一个维度是批处理维度。相反,您将为它提供一个numpy数组列表。可以使用
data = np.array(data)
将数组列表转换为单个数组。你知道吗训练时的LSTM输入需要Numpy数组。在这种情况下,可以将每个数组填充到批处理/输入中的最大长度,然后将它们转换为Numpy数组。你知道吗
因此最终数组的形状为(input\ size,maxlength,features\ size)。在这种情况下,如果在输入中有10个数组,则final\u arr将有一个形状(10,max\u lenth,3)。您可以将其用作LSTM的输入。你知道吗
相关问题 更多 >
编程相关推荐