简体   繁体   English

Keras多输出模型

[英]Keras Multiple outputs model

Edit: I have part of the answer, see the end of this post 编辑:我有部分答案,请参阅这篇文章的结尾

After making two different models to predict the score of a mastermind player, I am now trying to make a single model with two outputs: 在制作了两个不同的模型来预测主宰者的得分之后,我现在尝试制作一个具有两个输出的模型:

  • rcrp : number of pegs that have the Right Color in the Right Place rcrp:在正确位置具有正确颜色的钉子数
  • rcwp : number of pegs that have the Right Color in the Wrong Place. rcwp:在错误的位置具有正确颜色的桩号。

The entry contains both the player's proposal and the secret to find encoded in a binary table. 该条目包含玩家的提议和在二进制表中找到编码的秘密。 6 colors * 4 pins = 24 bits for the secret and 24 bits for the proposal. 6种颜色* 4个引脚=秘密使用24位,建议使用24位。

Here is my Model Architecture 这是我的模型架构 模型架构 .

Here is my main code: 这是我的主要代码:

main_input = Input(shape=(input_layer_size, ), name='main_input')
x = Dense(hidden_layer_size, activation="relu")(main_input)
for i in range(nb_hidden_layer):
    x = Dense(hidden_layer_size, activation="relu")(x)
rcrp_out = Dense(1, activation='sigmoid', name='rcrp_out')(x)
rcwp_out = Dense(1, activation='sigmoid', name='rcwp_out')(x)

model_rpwp = Model(inputs=main_input, outputs=[rcrp_out, rcwp_out])
model_rpwp.compile(optimizer='rmsprop', loss=['binary_crossentropy', 'binary_crossentropy'], metrics=['accuracy'])

Here is a sample of the training data: 这是训练数据的示例:

print(rpwp_feature)
[[0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 1]
 [0 0 0 ... 0 1 0]
 ...
 [1 0 0 ... 0 0 0]
 [1 0 0 ... 0 0 1]
 [1 0 0 ... 0 0 0]]

print(rcrp_label)
[3 0 1 ... 0 1 4]

print(rcwp_label)
[0 3 2 ... 4 2 0]

There is probably something I don't understand because my model doesn't learn anything and always predicts 0 for both outputs. 我可能不了解某些内容,因为我的模型没有学到任何东西,总是对两个输出都预测为0。

I've tried multiple loss functions and architectures, but nothing works. 我尝试了多种损失函数和体系结构,但没有任何效果。 My input and output data are formed as I expect. 我的输入和输出数据按预期形成。

Can you help me understand what I'm doing wrong? 您能帮我了解我在做什么错吗?

Edit: I have part of the answer. 编辑:我有部分答案。 The Sigmoid activation function of rcrp_out and rcwp_out returns a float between 0 and 1 so that it will never be a natural number. rcrp_outrcwp_out的Sigmoid激活函数返回0到1之间的浮点数,因此它永远不会是自然数。 In this case, I need to change the activation function and the loss function or binarise my label's data. 在这种情况下,我需要更改激活函数和丢失函数或对标签数据进行二值化处理。

I have binarised my label data with these functions. 我已经使用这些功能对标签数据进行了二值化处理。

def binarise_number(number, max_number=None):
    if max_number is None:
        return [int(x) for x in format(number, "0b")]
    n_number = format(number, "0>%db" % len(binarise_number(max_number, None)))
    return [int(x) for x in n_number]

def revert_binarise_number(n_number):
    str_number = '0b' + ''.join(str(int(x)) for x in n_number)
    number = int(str_number, base=2)
    return number

My data are now like this: 我的数据现在是这样的:

print(rcrp_label)
[[0 1 0]
 [0 1 0]
 [0 1 0]
 ...
 [0 0 0]
 [0 1 0]
 [1 0 0]]

print(rcwp_label)
[[0 0 1]
 [0 0 0]
 [0 0 0]
 ...
 [0 1 0]
 [0 0 0]
 [0 0 0]]

It now works as expected. 现在可以正常工作了。

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

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