我正在尝试创建功能API,而不是顺序API。我之前使用顺序API构建了该模型,并且效果很好。这是一个LSTM,我在batch_size从Input到LSTM层时遇到了麻烦。顺序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)
即使我知道batch_size是Input层的参数,我也会收到一条错误消息,提示“ Input()得到了意外的关键字参数'batch_size”。然后,如果我放弃了该参数,则第一个LSTM层会报错:
“如果RNN是有状态的,则需要知道其批处理大小。指定输入张量的批处理大小:
batch_input_shape
参数传递到第一层来指定批处理大小。batch_shape
参数来指定批处理大小。“ 我已经尝试过更新tensorflow,但这不能解决Input()问题。我从这里去哪里?
答案 0 :(得分:1)
您描述了通过功能性API传递batch_size
参数并收到错误提示“将batch_shape
参数传递给输入层”。
如果您尝试将输入层中的batch_size = batch_size
更改为
batch_shape = (batch_size,train_X.shape[1], train_X.shape[2])
能解决吗?