簡體   English   中英

ValueError:檢查目標時出錯:預期dense_3有2維,但得到形狀為(500、10、14)的數組

[英]ValueError: Error when checking target: expected dense_3 to have 2 dimensions, but got array with shape (500, 10, 14)

Keras:2.1.6,python 3.6,張量流 1.8.0

我正在嘗試訓練一個具有兩個 LSTM 層和 3 個密集層的序列模型。 我事先做了一些數據准備,並以 LSTM 層要求的格式設置我的數據,即(n_samples, n_timesteps, n_features) 我的數據有 14 個特征,實際上是一個由 5000 個步驟組成的長序列,因此我將其分解為 500 個樣本到 10 個時間步長。 完成后,我從下面的模型開始,但很快就遇到了最后一層的輸入形狀錯誤。 我嘗試使用 Sequential 和 Functional API 都產生相同的錯誤。

import keras 
from keras import callbacks
import tensorflow as tf
from keras.models import Model
from keras.layers import Input, Dense
from keras.layers import LSTM


X_input = Input(X_train.shape[1:]);

## First LSTM Layer
X = LSTM(10, return_sequences=True, input_shape = (10,14), name = 'LSTM_1')(X_input);

## Second LSTM Layer
X = LSTM(10)(X);

## First Dense Layer
X = Dense(10, activation='relu', name = 'dense_1')(X)

## Second Dense Layer
X = Dense(5, activation='relu', name = 'dense_2')(X)

## Final Dense Layer
X = Dense(1, activation='relu', name = 'dense_3')(X)

    ##The model object
model = Model(inputs = X_input, outputs = X, name='LSTMModel')

model.compile(optimizer = "Adam" , loss = "mean_squared_error", metrics = ['mean_squared_error','cosine', 'mae']);

Model.fit(x = X_train, y = Y_train, epochs = 300, callbacks=[tensorboard], validation_data=(X_eval,Y_eval));

我的數據是形狀(500,10,14)

>>> X_train.shape
(500,10,14)

我的模型摘要如下所示:

model.summary()
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
input_1 (InputLayer)         (None, 10, 14)            0
_________________________________________________________________
LSTM_1 (LSTM)                (None, 10, 10)            1000
_________________________________________________________________
LSTM_2 (LSTM)                (None, 10)                840
_________________________________________________________________
dense_1 (Dense)              (None, 10)                110
_________________________________________________________________
dense_2 (Dense)              (None, 5)                 55
_________________________________________________________________
dense_3 (Dense)              (None, 1)                 6
=================================================================
Total params: 2,011
Trainable params: 2,011
Non-trainable params: 0
_________________________________________________________________

雖然,我仍然收到錯誤:

ValueError: Error when checking target: expected dense_3 to have 2 dimensions, but got array with shape (500, 10, 14)

我的標簽形狀如下:

X_train = np.reshape(Train_data_scaled.values,(500,10,14));
Y_train = np.reshape(Train_labels_scaled.values,(500,10,1));
X_eval = np.reshape(Validation_data_scaled.values,(10,10,14));
Y_eval = np.reshape(Validation_labels_scaled.values,(10,10,1));

添加 RepeatVector 層后,我發現這里的另一個問題是相同的堆棧跟蹤。

_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
input_1 (InputLayer)         (None, 10, 14)            0
_________________________________________________________________
LSTM_1 (LSTM)                (None, 10)                1000
_________________________________________________________________
repeat_vector_1 (RepeatVecto (None, 10, 10)            0
_________________________________________________________________
LSTM_2 (LSTM)                (None, 10, 10)            840
_________________________________________________________________
dense_1 (Dense)              (None, 10, 10)            110
_________________________________________________________________
dense_2 (Dense)              (None, 10, 5)             55
_________________________________________________________________
dense_3 (Dense)              (None, 10, 1)             6
=================================================================
Total params: 2,011
Trainable params: 2,011
Non-trainable params: 0
_________________________________________________________________
Traceback (most recent call last):
  File ".\lstm.py", line 76, in <module>
    tf.app.run()
  File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\platform\app.py", line 126, in run
    _sys.exit(main(argv))
  File ".\lstm.py", line 67, in main
    Hist =  Model.fit(x = X_train, y = Y_train, epochs = 300,batch_size=10, callbacks=[tensorboard], validation_data=(X_eval,Y_eval));
  File "C:\Program Files\Python36\lib\site-packages\keras\engine\training.py", line 1630, in fit
    batch_size=batch_size)
  File "C:\Program Files\Python36\lib\site-packages\keras\engine\training.py", line 1480, in _standardize_user_data
    exception_prefix='target')
  File "C:\Program Files\Python36\lib\site-packages\keras\engine\training.py", line 123, in _standardize_input_data
    str(data_shape))
ValueError: Error when checking target: expected dense_3 to have shape (10, 1) but got array with shape (10, 14)

由於您想預測未來 10 天故事的價值,您需要將第二個 LSTM 層的return_sequences參數設置為True以使整個模型的輸出形狀(None, 10, 1)

## Second LSTM Layer
X = LSTM(10, return_sequences=True)(X)

此外,預測未來d天的值的更通用解決方案是在第一個 LSTM 層之后立即使用RepeatVector層。 這次你需要將第一個 LSTM 層的return_sequences參數設置為False

d = 5  # how many days in the future you want to predict?

## First LSTM Layer
X = LSTM(10, input_shape = (10,14), name = 'LSTM_1')(X_input);

X = RepeatVector(d)(X)

## Second LSTM Layer
X = LSTM(10, return_sequences=True)(X)

就好像第一個 LSTM 層對輸入數據進行編碼,而第二個 LSTM 層根據該編碼表示預測未來值。 另外,不用說標簽數組的形狀(即y_train )也必須是(n_samples, d, n_feats)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM