I would like to add a custom metric to model with Keras, I'm debugging my working code and I don't find a method to do the operations I need.
The problem could be described as a multi classification trough logistic multinomial regression. The custom metric I would like to implement is this:
(1/Number_of_Classes)*(TruePositivesClass1/TotalElementsClass1 + TruePositivesClass2/TotalElementsClass2 + ... + TruePositivesClassN/TotalElementsClassN)
Where Number_of_Classes must be calculate from batch, i.e something like np.unique(y_true).count()
and
and every summation item would be something like
len(np.where(y_true==class_i,1,0) == np.where(y_pred==class_i,1,0) )/np.where(y_true==class_i,1,0).sum()
In terms of confusion matrix (in the minimal form of 2 variables)
True False
True 15 3
False 12 1
The formula would be 0.5*(15)/(15+12) + 0.5*(1/(1+3))=0.4027
The code could be something like
def custom_metric(y_true,y_pred):
total_classes = Unique(y_true) #How calculate total unique elements?
summation = 0
for _ in unique_value_on_target:
# calculates Number of y_predict that are _
true_predics_of_class = Count(y_predict,_)
# calculates total number of items of class _ in batch y_true
true_values = Count(y_true,_)
value = true_predicts/true_values
summation + = value
return summation
My preprocessed data is a numpy array like x=[v1,v2,v3,v4,...,vn]
, and my
objetive column is a nompy array y=[1, 0, 1, 0, 1, 0, 0, 1 ,..., 0, 1]
then, they are converted to tensors:
x_train = tf.convert_to_tensor(x)
y_train = tf.convert_to_tensor(tf.keras.utils.to_categorical(y))
Then, they are converted to tensorflow dataset objects:
train_ds = tf.data.Dataset.zip((tf.data.Dataset.from_tensor_slices(x_train),
tf.data.Dataset.from_tensor_slices(y_train)))
Later, I take a iterator:
train_itr = iter(
train_ds.shuffle(len(y_train) * 5, reshuffle_each_iteration=True).batch(len(y_train)))
and last, I take one element of iterator and train
x_train, y_train = train_itr.get_next()
model.fit(x=x_train, y=y_train, batch_size=batch_size, epochs=epochs,
callbacks=[custom_callback], validation_data=test_itr.get_next())
So, since objects are dataset iterators, I can't find functions to operate them as I would like, in order to get the custom metric described.