0

I'm trying to define a loss function on the following shape: (NUM_OF_STROKES, STROKE_LEN, 2) For example, let say NUM_OF_STROKES=1, STROKE_LEN=4, it can be like: [[[x1,y1], [x2,y2], [x3,y3], [x4,y4]]]

I want that my loss function will be the ditances between two corresponding points (sum of all the distances). For example:

p1 = [[[a1,b1], [a2,b2], [a3,b3], [a4,b4]]]
p2 = [[[c1,d1], [c2,d2], [c3,d3], [c4,d4]]]
loss = sqrt((a1-c1)^2 + (b1-d1)^2) + ... + sqrt((a4-c4)^2 + (b4-d4)^2)

In numpy, I can do: np.sum(np.linalg.norm(np.array(p1) - np.array(p2), axis=1))

But I dont know how to do that in tensrflow I'm working with tensorflow 2 , keras.

Shahar182
  • 7
  • 2

1 Answers1

0

I think what you are looking for is:

tf.keras.backend.sum(tf.sqrt(tf.keras.backend.sum(tf.square(labels - predictions), axis=3)))

and axis = 3 for the batch