【发布时间】:2021-10-08 11:48:33
【问题描述】:
大家好
我需要实现 Aitchison 损失函数以在输入和输出数据集之间进行验证。数据是二维的(批次、特征)。暂时将批量昏暗构造为“无”尺寸维度
如果损失函数可以与 numpy 数组一起使用,则可以通过这种方式轻松完成
def loss_Aitch(yTrue, yPred):
yTrue_np = yTrue.numpy()
yPred_np = yPred.numpy()
sample_dist_mean = 0
for i in range(yTrue_np.shape[0]):
mult1 = 1.
mult2 = 1.
for j in range(yTrue_np.shape[1]):
mult1 *= yTrue_np[i, j]
mult2 *= yPred_np[i, j]
mult1 = np.sqrt(mult1)
mult2 = np.sqrt(mult2)
sample_dist = 0
for j in range(yTrue_np.shape[1]):
sample_dist += np.square( np.log( yTrue_np[i, j] / mult1) - np.log(yPred_np[i, j] / mult2 ) )
sample_dist = np.sqrt(sample_dist)
sample_dist_mean += sample_dist
sample_dist_mean /= yTrue_np.shape[0]
return sample_dist_mean
但由于张量是占位符,所以这是行不通的。
那么这个函数如何直接在张量上实现呢?
【问题讨论】:
标签: python tensorflow neural-network tensor loss-function