我收到来自Keras的非常混乱的错误消息。我使用以下模型并传递给它输入形状(num_examples, n, 1)
。
def create_model():
model = Sequential()
model.add(LSTM(64, input_shape=(n,1), return_sequences=False))
model.add(Dense(units=n, activation='linear'))
return model
我收到此错误消息:ValueError: Error when checking target: expected dense_16 to have 2 dimensions, but got array with shape (11030, 50, 1)
。
但那怎么可能呢?如果我使用model.summary()
,它表明LSTM
输出具有这种形状:(None, 64)
。那么如何将形状为(11030, 50, 1)
的数组传递给Dense层呢?
此外,如果我尝试在LSTM和Dense之间添加model.add(Flatten())
,我会收到此错误:ValueError: Input 0 is incompatible with layer flatten_3: expected min_ndim=3, found ndim=2
。
所以,它将2D传递给Flatten,但它如何才能将3D传递给Dense呢?
问题不在于您的模型,而在于目标,即您提供的y标签。你有一个不匹配,因为你的模型输出(batch_size, n)
,你给(batch_size, 50, 1)
。
假设n=50
然后您需要将目标标签挤压到2维并移除1. y_train = y_train.squeeze()
应该解决形状不匹配。
这可能会有所帮助
import numpy as np
input_array = np.array(input_array).reshape(dim1, dim2)
其中dim1,dim2表示您需要的尺寸大小。