Is it possible for using 3rd party lib when define loss function?

Hello There?

I want to use 3rd party lib when define loss function , because pytorch can not fulfill all requirments for users ,pseudo code as following:

from shapely.geometry import Polygon  # 3rd party pacakage

p1=Polygon(polygon_from_model_inference)
p2=Polygon(polygon_ground_truth)
intersection_area=p1.intersection(p2).area
iou=intersection_area/(p1.area+p2.area-intersection_area)
loss= iou

Whether pytoch will automatically trace the gradient of “loss” or not ? Thank you in advance

Yes, it’s possible, but Autograd won’t be able to track these operations, so you would need to write the backward pass manually via a custom autograd.Function as described here.