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
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.
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.
This is because exponential functions in softmax are monotonically increasing. z , the derivative of log-softmax cannot. As a result, softmax is numerically stable.
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.])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With