ValueError: All layers in a Sequential model should have a single output tensor. For multi-output layers, use the functional API

不知道什么问题,明明不是多输出的网络,出现错误ValueError: All layers in a Sequential model should have a single output tensor. For multi-output layers, use the functional API.

model = tf.keras.models.Sequential([
layers.Bidirectional(layers.LSTM(units=100,activation=‘tanh’, return_sequences=True, return_state=True, input_shape=(1, 50))),
layers.Bidirectional(layers.LSTM(units=100,activation=‘tanh’, return_sequences=True, return_state=True)),
layers.Bidirectional(layers.LSTM(units=100,activation=‘tanh’, return_sequences=True, return_state=False)),
layers.Dense(units=100,activation=‘tanh’),
layers.Dropout(0.1),
layers.Dense(units=100,activation=‘tanh’),
layers.Dense(50)
])
model.compile(optimizer=keras.optimizers.Adam(1e-4),
loss=tf.keras.losses.mean_squared_error,
metrics=[‘mse’])
num_epochs = 20#对完整训练数据迭代的次数
history = model.fit(train_input,train_output, epochs=num_epochs,validation_split=0.1)
model.evaluate(test_input,test_output)
@snowkylin

#改变训练集维度
train_input = train_input.reshape((above_number,1,sample_point))
train_output = train_output.reshape((above_number,1,sample_point))
#验证集
val_input = network_input[train_number:val_number].reshape((val_number - train_number,1,sample_point))
val_output = network_output[train_number:val_number].reshape((val_number - train_number,1,sample_point))
#测试集
test_input = network_input[val_number:test_number].reshape((test_number - val_number,1,sample_point))
test_output = network_output[val_number:test_number].reshape((test_number - val_number,1,sample_point))

这个API我没用过。我猜reture_sequencereturn_state只需要设一个为true,不然的话两个都返回了就 multi-output 了。

2 Likes

猜对了!太棒了