0

I want to write a custom loss function for comparing the generated 2D curves corresponding to y_prediction and y_true, and calculate the signed distance function of them. And for the loss, compare these signed distance functions together.
For curve generation, I used Where to find Python implementation of Chaikin's corner cutting algorithm?. All the functions are the same. For the main function, and conversion to Signed distance function, I write the following codes:

def Chaikin_curve(points):
    temp_coords= []
    Cartesian_coords_list= []
    for k in range (len(points)):
        temp_coords.append([points[k,0], points[k,1]])
    Cartesian_coords_list=temp_coords
    Cartesian_coords_list.append(temp_coords[0])
        
    obj = Object(Cartesian_coords_list)
    Smoothed_obj = obj.Smooth_by_Chaikin(number_of_refinements = 4)
    return(np.array(Smoothed_obj))  


def SDF_conversion(points):
    smoothed_obj= Chaikin_curve(points)
    polygon = Polygon(smoothed_obj)
    resolution= 32
    data = np.zeros((resolution, resolution))
    for x in range(resolution):
        for y in range(resolution):
            min_d= 10000
            for p in smoothed_obj:
                d = math.sqrt((p[0] - x)**2 + (p[1] - y)**2)
                if (d < min_d) :
                    min_d= d
                    
            point= Point(x, y)
            sign= polygon.contains(point)
            real_dist=0
            if not sign:
                real_dist = min_d
            if sign:
                real_dist = -1 * min_d
            
            data[x, y] = float(real_dist)
    
    return tf.convert_to_tensor(data)

and finally, my custom loss function is:

def custom_loss(y_true, y_pred):
    
    yy_true= tf.map_fn(SDF_conversion, y_true, dtype=tf.float64)
    yy_pred= tf.map_fn(SDF_conversion, y_pred, dtype=tf.float64)
    difference= tf.square(yy_true- yy_pred)
    return tf.reduce_mean(difference)

This loss function does not work and I got an error: "No gradients provided for any variable".

Do you know how can I fix this?

Saeedeh
  • 1
  • 1
  • 3
    Hi! Can you please share the code of your attempt? See how to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – ClaudiaR Aug 18 '22 at 11:10
  • there is no intuitive notion of "curve" of a 2D output, thus please either post the code you have produced, or a more fine explanation on what you want to achieve, or reference to a definition (eg wikipedia page) – Alberto Sinigaglia Aug 18 '22 at 11:34

1 Answers1

0

Assuming 'Polygon' is from shapely (or similar), I think your problem might lie in

sign= polygon.contains(point)

and its subsequent use in the loss function.

Tensorflow needs to be able to calculate the gradient of the loss wrt weights and biases, so the loss must change continuously as weights and biases vary.

It looks like this introduces a discountinuity into the loss function (since its result will flip from 0 to 1 as a point moves across the boundary), so tf will not be able to calculate gradients.

David Harris
  • 646
  • 3
  • 11