簡體   English   中英

輸入層和第一個 LSTM 層的 Keras 功能 API 問題

[英]Keras Functional API issue with Input layer and first LSTM layer

我正在嘗試創建一個功能 API,而不是順序 API。 我之前使用 Sequential API 構建了模型,並且效果很好。 它是一個 LSTM,我在處理從 Input 到 LSTM 層的 batch_size 時遇到了問題。 Sequential API 構建如下:

new_model = Sequential()
new_model.add(LSTM(n_neurons, batch_input_shape=(batch_size,train_X.shape[1], train_X.shape[2]), activation='tanh', stateful=True, return_sequences=True))
new_model.add(Dropout(0))
new_model.add(LSTM(n_neurons, batch_input_shape=(batch_size,train_X.shape[1], train_X.shape[2]), activation='tanh', stateful=True))
new_model.add(Dropout(0))
new_model.add(Dense(n_neurons1, activation='tanh'))
new_model.add(Dropout(0.1))
new_model.add(Dense(nm))
new_model.compile(loss='mse', optimizer=optimizer)

上面的代碼片段工作正常。 我試圖開始工作的功能 API 如下:

inp = Input(shape = (train_X.shape[1], train_X.shape[2]), batch_size = batch_size)
L1 = LSTM(n_neurons, batch_input_shape=(batch_size,train_X.shape[1], train_X.shape[2]), activation='tanh', stateful=True, return_sequences=True)(inp)
D1 = Dropout(0)(L1)
L2 = LSTM(n_neurons, batch_input_shape=(batch_size,train_X.shape[1], train_X.shape[2]), activation='tanh', stateful=True, return_sequences=True)(D1)
D2 = Dropout(0)(L2)
F1 = Dense(n_neurons1, activation='tanh')(D2)
D3 = Dropout(0.1)(F1)
out = Dense(nm)
new_model = Model(inp,out)
new_model.compile(loss='mse', optimizer=optimizer)

我收到一條錯誤消息,指出“Input() 得到了一個意外的關鍵字參數‘batch_size”,即使我知道 batch_size 是輸入層的參數。 然后,如果我擺脫了這個論點,我會收到第一個 LSTM 層的錯誤消息:

“如果 RNN 是有狀態的,它需要知道它的批量大小。指定輸入張量的批量大小:

  • 如果使用 Sequential 模型,請通過將batch_input_shape參數傳遞給第一層來指定批次大小。
  • 如果使用函數式 API,請通過將batch_shape參數傳遞給您的輸入層來指定批量大小。”

我已經嘗試過更新 tensorflow 但這並沒有解決 Input() 問題。 我從這里去哪里?

您描述了通過功能 API 傳遞batch_size參數並收到錯誤提示“將batch_shape參數傳遞給您的輸入層”。

如果您嘗試將輸入層中的batch_size = batch_size更改為

batch_shape = (batch_size,train_X.shape[1], train_X.shape[2])

這能解決嗎?

暫無
暫無

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

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