LSTM中的input_shape

时间:2019-09-21 14:00:03

标签: keras neural-network time-series lstm

我有以下代码段:

    X_train = np.reshape(X_train, (X_train.shape[0], 1, X_train.shape[1]))
    X_test = np.reshape(X_test, (X_test.shape[0], 1, X_test.shape[1]))


    model = Sequential()
    model.add(LSTM(200, activation='relu', input_shape=(X_train.shape[0], 1, X_train.shape[2]), return_sequences=False))
    model.compile(optimizer='adam', loss='mean_squared_error')
    model.fit(X_train, y_train, epochs=100, batch_size=32)
    y_pred = model.predict(X_test)

但是,出现以下错误:

ValueError: Input 0 is incompatible with layer lstm_1: expected ndim=3, found ndim=4

X_train和X_test的原始形状:

X_train: 1483, 13
X_test: 360, 13

并在重塑后变为:

X_train: 1483, 1, 13
X_test: 360, 1, 13

我知道这可能是重复的,但是网上的答案似乎都不适合我。

1 个答案:

答案 0 :(得分:0)

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <label for="extra_0" style="display:inline; margin-right: 10px;" class="boxextra"><input type="checkbox" data-multiple="1" id="extra_0" name="extra[]" value="rosii"> rosii</label> <label for="extra_1" style="display:inline; margin-right: 10px;" class="boxextra"><input type="checkbox" data-multiple="1" id="extra_1" name="extra[]" value="castraveti"> castraveti</label></td>是错误的。

LSTM应该具有2D输入形状(这意味着3D内部张量)。
升 -输入形状必须包含input_shape=(X_train.shape[0], 1, X_train.shape[2])
-这意味着内部形状将为(sequence_length, features_per_step)

然后,您的数据必须为3D,好的,但是(free_batch_size, sequence_length, features_per_step)应该为2D。
现在,input_shape对于循环层的工作是绝对必要的,如果您拥有sequence_length,这将毫无用处,除非您打算使用sequence_length = 1,它涉及到更为复杂的代码。