I have a binary classification problem. However, I don't really care about fp
and fn
values. What I want to achieve is that the fp + tp
gets close to tp+fn
.
In other words, the sum all predicted labels as positive
should be close to the sum of all real positive
labels. To make my network go to this direction, I use a custom loss function, which aims to calculate the difference between fp + tp
and tp+fn
.
from keras import backend as K
import tensorflow as tf
def loss_func(y_true,pred):
y_true = tf.cast(y_true, tf.float32)
mseos = K.abs(K.sum(pred) - K.sum(y_true))
return mseos
model = Sequential()
model.add(LSTM(10, return_sequences=False, input_shape=(X_df.shape[1], X_df.shape[2])))
model.add(Dense(10, activation='relu'))
model.add(Dropout(0.1))
model.add(Dense(1, activation='sigmoid'))
opt = keras.optimizers.SGD(learning_rate=0.001)
model.compile(loss=loss_func, optimizer=opt, metrics=['accuracy'])
The problem with this code is: the pred
is not a binary tensor with 0
and 1
values, instead, it has float numbers between 0
and 1
.
If I try to round the pred
values (e.g. K.round
) or convert it to binary to count the number of 1
s, I get the following error:
ValueError: No gradients provided for any variable:
Any idea how I can fix that?
pred
? $\endgroup$pred
, I don't get any error. But the logic is different than what I want. Basicallypred
values are between 0 and 1 (likelihood of beingpositive
), I want to calculate the sum of positive labels. All the values more than 0.5 should be considered as 1. $\endgroup$