Cosine similarity loss is correct or not?

The following code is for 1- cosine similarity loss, is it correct? Any advice will be appreciated, thanks.

1-cosine similarity loss

    head_feat = mx.symbol.reshape(head_feat, shape=(-1, ), name='head_feat_reshape')
    head_feat_vis = mx.symbol.reshape(head_feat_vis, shape=(-1,), name='head_feat_vis_reshape')
    dot =, head_feat_vis, name='dot')
    head_feat_l2 = mx.symbol.norm(head_feat, name='head_feat_l2')
    head_feat_vis_l2 = mx.symbol.norm(head_feat_vis, name='head_feat_vis_l2')
    head_feat_dot =, head_feat_vis_l2, name='head_feat_dot')
    cos_sim = mx.symbol.elemwise_div(dot, head_feat_dot, name='cosine similarity')
    constant1 = mx.symbol.ones(shape=(1, ), name='constant_1')
    cos_sim = mx.symbol.elemwise_sub(constant1, cos_sim, '1_cosine _imilarity')
    cos_loss = X.loss(cos_sim,
        grad_scale=1.0 / batch_roi * scale_loss_shift,


this looks fine to me. are you getting any errors?

Thanks for your reply. I did not get any errors. Thanks

How to do the same thing in scala?

println(result1.shape) # Shape(2047, 1)
println(result2.shape) # Shape(2047, 1)
val dt =, result2) # dt is of shape (1, 1)
println(“dot computed”)
val r1n = NDArray.norm(result1)
val r2n = NDArray.norm(result2)
println(“norm calculated”)
val denom =, r2n) # denom is of shape (1)
println(“denom calculated”)

val cosine_similarity = NDArray.elemwise_div(denom, dt) # This is giving error
Error: Incompatible attr in node at 1-th input: expected [1], got [1,1]