我正在尝试使用张量流后端将以下函数实现为Keras模型的自定义损失函数。但是我找不到一种方法来访问模型的输出并隔离其值,因此我可以使用它们来确定函数的得分。我还需要一种方法来获取训练过程中每次损失函数使用的样本数,以便使用Coords数组获取每个样本的实际值(纬度,经度)
def distance_error(y_actual, y_predicted):
R = 6378.1 #Radius of the Earth
brng_pred = y_predicted[1]*(math.pi/180) #Bearing is 90 degrees converted to radians.
d_pred = y_predicted[0] #Distance in km
lat1 = math.radians(Coord[i,0]) #Current lat point converted to radians
lon1 = math.radians(Coord[i,1]) #Current long point converted to radians
lat3 = math.asin( math.sin(lat1)*math.cos(d_pred/R) + math.cos(lat1)*math.sin(d_pred/R)*math.cos(brng_pred))
lon3 = lon1 + math.atan2(math.sin(brng_pred)*math.sin(d_pred/R)*math.cos(lat1), math.cos(d_pred/R)-math.sin(lat1)*math.sin(lat3))
true=(Coord[i+1,0],Coord[i+1,1])
mypred=(math.degrees(lat3),math.degrees(lon3))
testScore = geopy.distance.vincenty(true, mypred).km
return testScore
这样的损失函数可能吗?我搜索了一段时间并尝试过,但是无法访问输出值