Calculation of cosine similarity is giving error

Cosine similarity using mxnet scala api

I am getting an error in calculating the above. Need help !!

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]

Hi @amolpol98,

Shapes of demon and dt are different when you attempt the elemwise_div. You can either keep 2 dimensions when applying norm with norm(result1, keepdims=True) or add an axis to demon with .expand_dims(0). Are you performing this on batched data? If so, you’ll want to reformulate your cosine similarity. Check out this example.