Jordan
Jordan

Reputation: 25

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:

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.

Here is my Model ArchitectureModel 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.

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. In this case, I need to change the activation function and the loss function or binarise my label's data.

Upvotes: 1

Views: 3989

Answers (1)

Jordan
Jordan

Reputation: 25

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.

Upvotes: 1

Related Questions