![](/img/trans.png)
[英]Tensorflow LSTM Error (ValueError: Shapes must be equal rank, but are 2 and 1 )
[英]Keras LSTM TensorFlow Error: 'Shapes must be equal rank, but are 1 and 0'
我正在嘗試創建Keras LSTM(請注意,我是Keras中的LSTM和RNN的新手)。 該神經網絡應該接受4116個值的輸入,並輸出4116個值。 這將執行288個時間步。 我有27個這樣的時間步長(我意識到這可能會導致過擬合;我有一個更大的數據集,但首先想僅使用27個訓練示例來測試我的代碼)。
訓練數據存儲在兩個numpy數組x
和y
。 這些變量的形狀為(27, 288, 4116)
。
我的代碼:
datapoints = data.get.monthPoints(2, year)
x, y = datapoints[:-1], datapoints[1:]
del datapoints
input_shape = x.shape[1:]
output_shape = y.shape[1:]
checkpoint = ModelCheckpoint('model/files/alpha.h5', monitor='val_loss', verbose=1, save_best_only=True, mode='auto', period=1)
early = EarlyStopping(monitor='val_loss', min_delta=0, patience=1, verbose=1, mode='auto')
model = Sequential()
model.add(LSTM(5488, activation='relu', input_shape=input_shape))
model.add(RepeatVector(output_shape))
model.add(LSTM(5488, activation='relu', return_sequences=True))
model.add(TimeDistributed(Dense(output_shape)))
model.compile(loss='mse', optimizer='adam')
model.fit(x, y, epochs=100, batch_size=8, callbacks = [checkpoint, early])
運行程序時,出現以下錯誤:
tensorflow.python.framework.errors_impl.InvalidArgumentError: Shapes must be equal rank, but are 1 and 0
From merging shape 1 with other shapes. for 'repeat_vector/stack_1' (op: 'Pack') with input shapes: [], [2], []
和
During handling of the above exception, another exception occurred:
ValueError: Shapes must be equal rank, but are 1 and 0
From merging shape 1 with other shapes. for 'repeat_vector/stack_1' (op: 'Pack') with input shapes: [], [2], []
我還看到了其他類似的問題,例如this和this ,但是他們沒有提供解決我的問題的解決方案,或者解決方案不清楚。
我猜我的問題與我錯誤地構建網絡或錯誤格式化數據有關。
任何見解,我將不勝感激。
謝謝。
您可能希望重復第一LSTM層的輸出,其重復次數與模型輸出序列中的時間步長相同(即y
)。 因此,應為:
model.add(RepeatVector(y.shape[1]))
您的代碼中有兩個問題。 首先,在RepeatVector
您要通過傳遞y.shape [1:]發送一個列表。 在RepeatVector
您應該發送一個整數。
第二個問題是在TimeDistributed
。 發送您希望重復第二維的次數。 因此,您的代碼應為:
repeat_vector_units = x.shape[1]
output_units = x.shape[2]
model = Sequential()
model.add(LSTM(5488, activation='relu', input_shape=input_shape))
model.add(RepeatVector(repeat_vector_units)) ### Change here
model.add(LSTM(5488, activation='relu', return_sequences=True))
model.add(TimeDistributed(Dense(output_units))) #### Change here
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.