Use Earth Mover's Distance as loss function in Tensorflow


I want to compute Earth Mover’s Distance between two pointclouds as loss function in Tensorflow.

pointclouds1 = tf.placeholder(tf.float32, shape=(batch_size, num_point, 3))
pointclouds2 = tf.placeholder(tf.float32, shape=(batch_size, num_point, 3))
//'3' means xyz coordinate

def get_loss(pointclouds1, pointclouds2):
    loss = EMD.getEMD(pointclouds1,pointclouds2)
    return loss

Unfortunately,I get the error:

File "F:\pointclouds\utils\", line 71, in groundDistance
return np.linalg.norm(x1 - x2, norm)
File "C:\Users\xu\Anaconda3\lib\site-packages\numpy\linalg\", line 2257, in norm
raise ValueError("Improper number of dimensions to norm.")
ValueError: Improper number of dimensions to norm.

The is from

But I can use the file to operate numpy array directly:

We got: 160.542759771
C example got 160.54277

I guess it means I cannot operate Tensor directly, so what should I do?


Is the function EMD.getEMD actually doing the computation? It appears to be the case, and if my assumption is correct you’re misunderstanding tensorflow.

Tensorflow development progresses in two stages, first you build a graph of your operations, we usually separate all of this code into a build_graph() function. At this point no data is passed in, we are only defining the operations that we’ll do.

Second, you create a session, pass in the variable, and ask tensorflow to compute certain values, such as the loss. You’ll actually do computations in tensorflow using a call to[ops_to_compute], feed_dict={placeholder_1:input_1, placeholder_2:input_2, ...})

In order to use a custom loss function, you’ll need to define the loss function in tensorflow. If you ever use a numpy function in the definition of the loss function you know you’ve done it wrong. You have to define the loss function using tensorflow operations.

It’s usually quite trivial to do so. You’ll generally just look at the numpy operations that you have in your current code and re-create the same tensorflow operation.

Answered By – David Parks

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published