首页 > 解决方案 > 使用 Keras 训练具有两个不同输出的相同模型

问题描述

我在 python 中有一个用 Keras 编码的简单 GRU 网络,如下所示:

gru1  = GRU(16, activation='tanh', return_sequences=True)(input)
dense  = TimeDistributed(Dense(16, activation='tanh'))(gru1)
output = TimeDistributed(Dense(1, activation="sigmoid"))(dense)

因为我的目的是分类,所以我使用了 sigmoid 激活作为输出。但我也需要使用相同的模型进行回归。我需要将输出激活更改为线性。但是,网络的其余部分仍然相同。所以在这种情况下,我将使用两个不同的网络来实现两个不同的目的。输入是相同的。但是输出是 sigmoid 的类和线性激活的值。

我的问题是,有没有办法只使用一个网络但最后得到两个不同的输出?谢谢。

标签: pythonkerasactivation-function

解决方案


是的,您可以使用功能 API 来设计多输出模型。您可以保留共享层和 2 个不同的输出,一个带有 sigmoid,另一个带有线性激活。

注意:不要input用作变量,它是 python 中的函数名。

from tensorflow.keras.layers import *
from tensorflow.keras.models import Model
seq_len = 100 # your sequence length
input_ = Input(shape=(seq_len,1))
gru1  = GRU(16, activation='tanh', return_sequences=True)(input_)
dense  = TimeDistributed(Dense(16, activation='tanh'))(gru1)
output1 = TimeDistributed(Dense(1, activation="sigmoid", name="out1"))(dense)
output2 = TimeDistributed(Dense(1, activation="linear", name="out2"))(dense)

model = Model(input_, [output1, output2])

model.summary()
Model: "model_1"
__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_3 (InputLayer)            [(None, 100, 1)]     0                                            
__________________________________________________________________________________________________
gru_2 (GRU)                     (None, 100, 16)      912         input_3[0][0]                    
__________________________________________________________________________________________________
time_distributed_3 (TimeDistrib (None, 100, 16)      272         gru_2[0][0]                      
__________________________________________________________________________________________________
time_distributed_4 (TimeDistrib (None, 100, 1)       17          time_distributed_3[0][0]         
__________________________________________________________________________________________________
time_distributed_5 (TimeDistrib (None, 100, 1)       17          time_distributed_3[0][0]         
==================================================================================================
Total params: 1,218
Trainable params: 1,218
Non-trainable params: 0

用两个损失函数编译:

losses = {
    "out1": "binary_crossentropy",
    "out2": "mse",
}

# initialize the optimizer and compile the model

model.compile(optimizer='adam', loss=losses, metrics=["accuracy", "mae"])

推荐阅读