How can I combine two outputs to form a custom metric in TensorFlow?

Viewed 137

I would like to implement a metric in TensorFlow based on the combined results of two outputs.

My model takes in a 100-character string and returns two outputs (called flavour and form) based on this string. These outputs are both softmax probabilities that are compared with a one-hot encoded vectors (standard classification). The code for the model is:

inputs = Input(shape=(None,))
input_embeddings = Embedding(vocab_size, embedding_size, mask_zero=True)(inputs)

shared_lstm = Bidirectional(LSTM(units, return_sequences=True, dropout=0.2))(input_embeddings)

fl_lstm = Bidirectional(LSTM(units, dropout=0.2))(shared_lstm)
fl_dense = Dense(flavour_size, activation='softmax', name='flavour')(fl_lstm)

fo_lstm = Bidirectional(LSTM(units, dropout=0.2))(shared_lstm)
fo_dense = Dense(form_size, activation='softmax', name='form')(fo_lstm)

split_shared_model = Model(inputs=inputs, outputs=[fl_dense, fo_dense])

Here is a flow diagram of the model's architecture:

enter image description here

Currently, I am compiling and fitting as follows:

split_shared_model.compile(optimizer='adam', loss=CategoricalCrossentropy(), 
                  metrics=['accuracy'])

split_shared_model.fit(X_train, [fl_train, fo_train],
                       batch_size=32,
                       epochs=10)

Each of the individual outputs (flavour and form) reach score around 96% accuracy on the test set. However, I would like to create a metric that combines these two predictions and assess them together. Something that might look a little bit like:

def combined_accuracy(fl_true, fo_true, fl_pred, fo_pred):
    correct_fl = K.equal(fl_true, K.round(fl_pred))
    correct_fo = K.equal(fo_true, K.round(fo_pred))
    combined = tf.logical_and(correct_fl, correct_fo)
    return K.mean(combined)

I've looked at the Keras documentation (https://keras.io/api/metrics/#creating-custom-metrics), but it seems as if the custom metric is applied to each output individually.

What can I try next?

0 Answers
Related