Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple softmax classifications (Keras)

I am trying to construct a CNN that will output 2 labels, where each label has 12 possibilities; the input is an image.

In other words, my desired output is A (one out of 12 options) and B (one out of 12 different options).

With each label separately, the go-to is of course softmax, but I couldn't find whether it is possible to do 'multiple softmax' labels like that.

Clearly, I can output 24 neurons, using sigmoid outputs and then normalize manually, but then the network will not intrinsically couple the 12 options within each group.

I'm writing pseudo-pseudo-code of what I would have wanted, if it existed:

model = Sequential()
model.add(Convolution2D(64, kernel_size=3,activation='relu', input_shape=image_shape))
model.add(Flatten())
model.add(Dense(256,activation='relu'))
# pseudo-pseudo-code
model.add(Dense((12,12),activation=('softmax','softmax'))) # <- here is where I would have liked 2 softmax outputs with 12 neurons in each

Any solutions will be welcome (I'm using Keras but will be happy for solutions using any other package, or even code something myself).

Edit: I could also generate 144 outputs representing all combinations, but I am not sure whether this would be a good solution - any comment on that is also very welcome

like image 924
Tacratis Avatar asked Feb 26 '19 20:02

Tacratis


People also ask

Can softmax be used for multiple classification problems?

The softmax function is used as the activation function in the output layer of neural network models that predict a multinomial probability distribution. That is, softmax is used as the activation function for multi-class classification problems where class membership is required on more than two class labels.

Why is softmax used for multiclass classification?

Softmax extends this idea into a multi-class world. That is, Softmax assigns decimal probabilities to each class in a multi-class problem. Those decimal probabilities must add up to 1.0. This additional constraint helps training converge more quickly than it otherwise would.

Is softmax monotonically increasing?

This is because exponential functions in softmax are monotonically increasing. z , the derivative of log-softmax cannot. As a result, softmax is numerically stable.


1 Answers

I would use the functional API with 2 outputs. Guide in https://keras.io/getting-started/functional-api-guide/#multi-input-and-multi-output-models

something on the lines of:

input = Input(input_shape=image_shape)
cnv = Convolution2d(...)(input)
hidden = Dense(256, ...)(Flatten()(cnv))
output1 = Dense(12, activation='softmax')(hidden)
output2 = Dense(12, activation='softmax')(hidden)
model = keras.models.Model(inputs=input, outputs=[output1, output2])
model.compile(optimizer='sgd', loss=['binary_crossentropy', 'binary_crossentropy'],
              loss_weights=[1., 1.])
like image 51
Pedro Marques Avatar answered Oct 13 '22 18:10

Pedro Marques