【问题标题】:Training the same model with two different outputs with Keras使用 Keras 训练具有两个不同输出的相同模型
【发布时间】:2020-04-23 15:07:09
【问题描述】:

我有一个在 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 的类和线性激活的值。

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

【问题讨论】:

    标签: python keras activation-function


    【解决方案1】:

    是的,您可以使用函数式 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"])
    

    【讨论】:

    • 感谢您对扎比尔的评论。这里还有一点。我需要用不同的损失函数训练这两个模型。 MSE 用于线性激活,Binary CE 用于 sigmoid 激活。这种结构可以吗?
    • 当然,您可以查看我更新的答案。如果有帮助,请不要犹豫投票/接受。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-25
    • 1970-01-01
    • 2021-07-03
    • 1970-01-01
    • 2018-08-15
    相关资源
    最近更新 更多