簡體   English   中英

層順序的輸入 0 與層不兼容:

[英]Input 0 of layer sequential is incompatible with the layer:

我正在嘗試預測一個基本的股票價格模型。 這是我的代碼

data = pd.read_csv("total_cases.csv")
x = data["date"]
world_cases = data["Turkey"].fillna(0)
time = np.arange(len(world_cases), dtype="float32")

split_time = 200
x_train = time[:split_time]
x_valid = time[split_time:]

y_train = world_cases[:split_time]
y_valid = world_cases[split_time:]
window_size = 20
batch_size = 32
shuffle_buffer_size=1000
train_data = tf.data.Dataset.from_tensor_slices((x_train, y_train))
valid_data = tf.data.Dataset.from_tensor_slices((x_valid, y_valid))
model = Sequential()
model.add(LSTM(16, return_sequences=True))

model.add(LSTM(16))

model.add(Dense(16, activation='relu'))
model.compile(optimizer='adam', loss='mae', metrics=['mae'])

r = model.fit(train_data, validation_data=valid_data, epochs=100)

運行模型時,出現錯誤

ValueError: Input 0 of layer sequential is incompatible with the layer: expected ndim=3, found ndim=0. Full shape received: []

編輯這是 csv 文件的一部分,world_cases -Turkey- 列

0           0.0
1           0.0
2           0.0
3           0.0
4           0.0
         ...
258    291162.0
259    292878.0
260    294620.0
261    296391.0
262    298039.0

我轉載了你的問題。

import tensorflow as tf
inputs = tf.random.normal([])
lstm = tf.keras.layers.LSTM(4)
output = lstm(inputs)
print(output.shape)

輸出

ValueError: Input 0 of layer lstm_1 is incompatible with the layer: expected ndim=3, found ndim=0. Full shape received: ()

問題在於輸入數據形狀

由於 Tensorflow.Keras LSTM 需要形狀 3D 的輸入。 按照此修改您的輸入

inputs: A 3D tensor with shape [batch, timesteps, feature] 
model.add(LSTM(16,return_sequences=False)).

工作示例代碼

import tensorflow as tf
inputs = tf.random.normal([32, 10, 8])
lstm = tf.keras.layers.LSTM(4)
output = lstm(inputs)
print(output.shape)

輸出

(32, 4)

暫無
暫無

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

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