Reputation: 3179
I have the following Keras model, whose output has 3 classes (0, 1, 2):
model = Sequential()
model.add(LSTM(100, input_shape=(n_time_steps,n_features)))
model.add(Dropout(0.5))
model.add(Dense(100, activation='relu'))
model.add(Dense(n_classes, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
I'm interested in evaluating the model for precision of classes 1 and 2, not 0, i.e. the model is good if it has the smallest number of false positives in classes 1 and 2, while I don't really care about class 0.
How can I write such metrics in Keras?
Upvotes: 0
Views: 200
Reputation: 86620
Create any function taking the ground truth and the predicted results and calculate your metrics:
def false_positives(y_true, y_pred):
negatives = 1 - y_true
y_pred = K.cast(K.greater(y_pred, 0.5), K.floatx()) #round to 0 or 1
#if you don't round y_pred, it might even serve as a loss function
falsePositives = y_pred * negatives
falsePositives1 = falsePositives[:,1]
falsePositives2 = falsePositives[:,2]
return something
Use metrics = [false_positives]
, or metrics=['accuracy', false_positives_1, false_positives_2]
, etc.
Upvotes: 1