对于我的模型的损失函数,我想比较预测结果与真实值之间的距离,以及预测值与参考数据集中其他最近邻居之间的距离。我可以用np数组很好地计算这个值,但是TF不喜欢我在Tensor上使用这个。
def nearest_other_neighbour(y_true, y_pred):
losses=[]
res_df = pd.DataFrame(cdist(keras.backend.array(y_pred), keras.backend.array(y_true)))
for i in range(len(res_df.columns)) :
mycol = list(res_df[i])
dist_ref_self = mycol.pop(i)
dist_ref_min_other = min(mycol)
losses.append(dist_ref_self/dist_ref_min_other)
return losses
我找到了this post,它允许我只使用Tensor计算距离矩阵,但之后我仍然需要迭代距离矩阵。
我如何仅使用Tensor来执行这些计算?或者有没有一种方法可以将符号Tensor转换为损失函数中的np数组?
1条答案
按热度按时间2ekbmq321#
如果以后有人找这个:我和特遣部队一起完成了这一切
这需要使用datasets来管理输入:
train_dataset = tf.data.Dataset.from_tensor_slices((train_x, train_y)).batch(mybatch, drop_remainder=True)