Keras multiple outputs

4,171 views
Skip to first unread message

shaha...@gmail.com

unread,
Dec 10, 2016, 4:51:22 PM12/10/16
to Keras-users
Hi,
I want to build a net for solving a classification problem, But I want to classify K outputs using a softmax classifier, each output can be one of M groups. How can I do that with Keras? 

clarification: 
If for example I have N data entries, I mean I want the classifier to give me K outputs, each output will be a vector of length M with 1 only in the group that the entry belongs to. 
This means my target set size will be NxKxM

Florian Golemo

unread,
Dec 11, 2016, 4:13:42 AM12/11/16
to Keras-users, shaha...@gmail.com
Hello,

I'd do that with the functional API. You create your network like any other network and then you just create several output layers, like so:

from keras.layers import Input, Dense
from keras.models import Model

inputs = Input(shape=(N,)) # N is the width of any input element, say you have 50000 data points, and each one is a vector of 3 elements, then N is 3

x = Dense(64, activation='relu')(inputs) # this is your network, let's say you have 2 hidden layers of 64 nodes each (don't know if that's enough for you)
x = Dense(64, activation='relu')(x)

output1 = Dense(M, activation='softmax')(x) # now you create an output layer for each of your K groups. And each output has M elements, out of which because of 'softmax' only 1 will be activated. (practically this is of course a distribution, but after sufficient training, this usually makes one element close to one and the other
elements close to zero) output2 = Dense(M, activation='softmax')(x)
output3 = Dense(M, activation='softmax')(x)
... #you have to fill in the remaining layers here, or better: use a for loop
outputK
= Dense(M, activation='softmax')(x)
model = Model(input=inputs, output=[
output1, output2, output3, ..., outputK])


model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])

model.fit(inputData, [outputData1, outputData2, outputData3, ... outputDataK], nb_epochs=10, batch_size=64)


Florian Golemo

unread,
Dec 11, 2016, 4:18:15 AM12/11/16
to Keras-users, shaha...@gmail.com
Just to be clear, I assume here, that your dataset actually looks like this:

IxN inputs and KxM outputs,

where
- I is the number of rows in your dataset and in each row:
  - N is the length of the input vector
  - K is the number of outputs
  - M is the length of the each output vector

shaha...@gmail.com

unread,
Dec 12, 2016, 12:44:00 PM12/12/16
to Keras-users, shaha...@gmail.com
Thank you very much for your quick and very helpful answer! 
This is exactly what I wanted and it works excellently.

The only problem I have is that now my metrics are the accuracy for each output separately. I want to use a callback to train and take the net after the epoch with the best validation accuracy and I can't do that, because now the validation metrics are calculated separately for each output. Is there a way to make the fit function calculate the average accuracy over all outputs? 

Thanks you very much!

viji.ven...@gmail.com

unread,
May 9, 2018, 5:06:11 AM5/9/18
to Keras-users
Hello, I have exactly the same question. Were you able to figure out how to calculate the average accuracy over all outputs and use that in your callbacks?

monikas...@gmail.com

unread,
Jan 28, 2019, 4:44:55 AM1/28/19
to Keras-users
Hi...what is the solution for getting a combined accuracy for multiple heads?

Sergio Vera Martínez

unread,
May 3, 2021, 2:07:19 PM5/3/21
to Keras-users
Hello everyone, 

I have the same problem. I get the total loss, as well as from each output, but not the total/mean/combined accuracy.

I would really appreciate it if someone finds an answer, and comments on this thread.

Many thanks in advance.

Best regards,

Sergi

El dia dilluns, 28 de gener de 2019 a les 10:44:55 UTC+1, monikas...@gmail.com va escriure:

Sergio Vera Martínez

unread,
May 3, 2021, 3:30:25 PM5/3/21
to Keras-users
Hello everyone,

Fortunately, I've found the solution. I've used a callback method to calculate the mean accuracy. Next, there are the links to the solution:


Best regards

El dia dilluns, 3 de maig de 2021 a les 20:07:19 UTC+2, Sergio Vera Martínez va escriure:
Reply all
Reply to author
Forward
0 new messages