简体   繁体   English

输入层和第一个 LSTM 层的 Keras 功能 API 问题

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

I am trying to create a Functional API as opposed to a Sequential API.我正在尝试创建一个功能 API,而不是顺序 API。 I have built the model previously using the Sequential API, and it worked just fine.我之前使用 Sequential API 构建了模型,并且效果很好。 It is an LSTM, and I am having trouble with the batch_size going from the Input to the LSTM layer.它是一个 LSTM,我在处理从 Input 到 LSTM 层的 batch_size 时遇到了问题。 The Sequential API was built as follows: 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)

The above snippet works fine.上面的代码片段工作正常。 The Functional API I am trying to get to work is as follows:我试图开始工作的功能 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)

I get an error saying "Input() got an unexpected keyword argument 'batch_size", even though I know batch_size is an argument for the Input layer.我收到一条错误消息,指出“Input() 得到了一个意外的关键字参数‘batch_size”,即使我知道 batch_size 是输入层的参数。 Then, if I get rid of the argument, I get an error with the first LSTM layer saying:然后,如果我摆脱了这个论点,我会收到第一个 LSTM 层的错误消息:

"If a RNN is stateful, it needs to know its batch size. Specify the batch size of your input tensors: “如果 RNN 是有状态的,它需要知道它的批量大小。指定输入张量的批量大小:

  • If using a Sequential model, specify the batch size by passing a batch_input_shape argument to your first layer.如果使用 Sequential 模型,请通过将batch_input_shape参数传递给第一层来指定批次大小。
  • If using the functional API, specify the batch size by passing a batch_shape argument to your Input layer."如果使用函数式 API,请通过将batch_shape参数传递给您的输入层来指定批量大小。”

I have already tried updating tensorflow but that did not fix the Input() issue.我已经尝试过更新 tensorflow 但这并没有解决 Input() 问题。 Where do I go from here?我从这里去哪里?

You describe passing a batch_size parameter via the functional API and getting an error suggesting "passing a batch_shape argument to your Input layer."您描述了通过功能 API 传递batch_size参数并收到错误提示“将batch_shape参数传递给您的输入层”。

If you try changing batch_size = batch_size in your input layer to如果您尝试将输入层中的batch_size = batch_size更改为

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

does that solve it?这能解决吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM