So basically, I want to code this line of code but one that is acceptable by tensorflow’s modile.complie()
total_CB_Loss = []
for a in range(0,len(targets)):
for y in range(0,3):
if targets[a][y] == 1:
CB_Loss = ((-1) * CB[a] * ((1 - inputs[a,y]) ** gamma) * (log(inputs[a,y])))
total_CB_Loss.append(CB_Loss)
elif targets[a][y] == 0:
CB_Loss2 = ((-1) * CB[a] * ((inputs[a,y]) ** gamma) * (log(1 - inputs[a,y])))
total_CB_Loss.append(CB_Loss2)
So I did a little bit of digging and saw that I need to use a masking technique (Custom loss function in Keras/Tensorflow with if statement). So my code looks a little bit like this:
total_CB_Loss = []
for a in range(0,len(targets)):
for y in range(0,3):
mask = tf.reduce_all(tf.equal(targets[a][y],1))
mask = K.cast(mask,'float32')
CB_Loss = (-1) * CB[a] * (((1-inputs[a][y])**gamma)*mask)* ((tf.math.log(inputs[a][y]))*mask)*(((inputs[a][y]) ** gamma)*(tf.math.log(mask))) * (((tf.math.log(1 - inputs[a][y]))*(tf.math.log(mask))))
total_CB_Loss.append(CB_Loss)
I understand that masking is “Masking is a way to tell sequence-processing layers that certain timesteps in an input are missing, and thus should be skipped when processing the data.”(https://www.tensorflow.org/guide/keras/understanding_masking_and_padding).
But the output I get for the loss value after model.fit() is nan. I understand this happens due to log(0) is math error.
I anticipated that if the value is not there (math error), then it will be skipped as stated above.
How to make so that the intended if-else works for my case while using mask concept?
I did not try any other tensorflow concept beside masking as it works for a few of my codes.