I am using Keras to train my model and want to use angular distance between my predictions and hypothesis as my metric. The closest metric I have found so far is cosine proximity, not angular distance though.
The problem I have with cosine proximity (or cosine distance) is that the cosine for small numbers are very similar.
Looking at Wikipedia, it is possible to calculate the angular distance using cosine proximity:
So I am wondering if using a custom metric based on cosine proximity is a good idea and if so how is that implemented.
So I think I have the answer to my own question. What I did is I first calculated the cosine proximity by simply using the Keras's source code. Then I calculated the Arc Cosine of the previous result:
def angular_distance(y_true, y_pred):
y_true = K.l2_normalize(y_true, axis=-1)
y_pred = K.l2_normalize(y_pred, axis=-1)
cosine = K.sum(y_true * y_pred, axis=-1)
return 2*tf.math.acos(cosine)/np.pi
Then I passed the new function as a custom metric to model's compilation script:
model.compile(loss='categorical_crossentropy', optimizer=Adam(0.001),
metrics=[angular_distance])