0
$\begingroup$

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 1s, I get the following error:

ValueError: No gradients provided for any variable:

Any idea how I can fix that?

$\endgroup$
3
  • $\begingroup$ And what happens if you just don't round pred? $\endgroup$
    – noe
    Dec 1, 2022 at 16:53
  • $\begingroup$ If I don't round the pred, I don't get any error. But the logic is different than what I want. Basically pred values are between 0 and 1 (likelihood of being positive), I want to calculate the sum of positive labels. All the values more than 0.5 should be considered as 1. $\endgroup$
    – Farzad
    Dec 1, 2022 at 21:39
  • $\begingroup$ Doesn't the maximum of the rounded version match that of the round-less version? Have you tried empirically to see if the results are Ok? $\endgroup$
    – noe
    Dec 1, 2022 at 23:37

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.