j6wj1997
j6wj1997

Reputation: 179

How to normalize tensor inside custom loss function in TensorFlow?

The custom loss function was written and must to show a deviation from truth direction in degrees. I have the with truth direction (x, y, z), and I try to predict direction use the degrees_mean_error function for optimizer, which is presented below:

def degrees_mean_error(y_true, y_pred):
    norm = sqrt(y_pred[:, 0] ** 2 + y_pred[:, 1] ** 2 + y_pred[:, 2])
    y_pred[:, 0] /= norm
    y_pred[:, 1] /= norm
    y_pred[:, 2] /= norm
    angles = y_pred[:, 0] * y_true[:, 0] + y_pred[:, 1] * y_true[:, 1] + y_pred[:, 2] * y_true[:, 2]
    return acos(angles) * 180 / np.pi

But, I have a problem, because the tensor isn't assigment. Can I normalize the tensor inside keras loss function? If you don’t do so, the error will be big, even nan, see the below output without normilize during training:

256/170926 [..............................] - ETA: 3:21 - loss:88.1727

512/170926 [..............................] - ETA: 2:25 - loss: 66.7276

768/170926 [..............................] - ETA: 2:07 - loss: nan

1024/170926 [..............................] - ETA: 1:58 - loss: nan

1280/170926 [..............................] - ETA: 1:53 - loss: nan

1536/170926 [..............................] - ETA: 1:50 - loss: nan

1792/170926 [..............................] - ETA: 1:47 - loss: nan

2048/170926 [..............................] - ETA: 1:45 - loss: nan

Upvotes: 2

Views: 400

Answers (1)

Youlfey
Youlfey

Reputation: 605

You can immediately find the deviation:

 angles = (y_pred[:, 0] / norm) * y_true[:, 0] + (y_pred[:, 1] / norm) * y_true[:, 1] + (y_pred[:, 2] / norm) * y_true[:, 2]

Upvotes: 1

Related Questions