I've got a loss function that fulfills my needs, but is only in PyTorch. I need to implement it into my TensorFlow code, but while most of it can trivially be "translated" I am stuck with a particular line:
y_hat[:, torch.arange(N), torch.arange(N)] = torch.finfo(y_hat.dtype).max # to be "1" after sigmoid
You can see the whole code in following and it is indeed pretty straight forward except for that line:
def get_loss(y_hat, y):
# No loss on diagonal
B, N, _ = y_hat.shape
y_hat[:, torch.arange(N), torch.arange(N)] = torch.finfo(y_hat.dtype).max # to be "1" after sigmoid
# calc loss
loss = F.binary_cross_entropy_with_logits(y_hat, y) # cross entropy
y_hat = torch.sigmoid(y_hat)
tp = (y_hat * y).sum(dim=(1, 2))
fn = ((1. - y_hat) * y).sum(dim=(1, 2))
fp = (y_hat * (1. - y)).sum(dim=(1, 2))
loss = loss - ((2 * tp) / (2 * tp + fp + fn + 1e-10)).sum() # fscore
return loss
So far I came up with following:
def get_loss(y_hat, y):
loss = tf.keras.losses.BinaryCrossentropy()(y_hat,y) # cross entropy (but no logits)
y_hat = tf.math.sigmoid(y_hat)
tp = tf.math.reduce_sum(tf.multiply(y_hat, y),[1,2])
fn = tf.math.reduce_sum((y - tf.multiply(y_hat, y)),[1,2])
fp = tf.math.reduce_sum((y_hat -tf.multiply(y_hat,y)),[1,2])
loss = loss - ((2 * tp) / tf.math.reduce_sum((2 * tp + fp + fn + 1e-10))) # fscore
return loss
so my questions boil down to:
- What does
torch.finfo()
do and how to express it in TensorFlow? - Does
y_hat.dtype
just return the data type?