【发布时间】:2017-03-26 16:57:10
【问题描述】:
我正在尝试实现一个损失函数,该函数试图最小化从预测的双变量高斯分布参数中获得地面真值 (x,y) 的负对数似然。我在张量流中实现这个 - 这是代码 -
def tf_2d_normal(self, x, y, mux, muy, sx, sy, rho):
'''
Function that implements the PDF of a 2D normal distribution
params:
x : input x points
y : input y points
mux : mean of the distribution in x
muy : mean of the distribution in y
sx : std dev of the distribution in x
sy : std dev of the distribution in y
rho : Correlation factor of the distribution
'''
# eq 3 in the paper
# and eq 24 & 25 in Graves (2013)
# Calculate (x - mux) and (y-muy)
normx = tf.sub(x, mux)
normy = tf.sub(y, muy)
# Calculate sx*sy
sxsy = tf.mul(sx, sy)
# Calculate the exponential factor
z = tf.square(tf.div(normx, sx)) + tf.square(tf.div(normy, sy)) - 2*tf.div(tf.mul(rho, tf.mul(normx, normy)), sxsy)
negRho = 1 - tf.square(rho)
# Numerator
result = tf.exp(tf.div(-z, 2*negRho))
# Normalization constant
denom = 2 * np.pi * tf.mul(sxsy, tf.sqrt(negRho))
# Final PDF calculation
result = -tf.log(tf.div(result, denom))
return result
当我进行训练时,我可以看到损失值在减少,但它远远低于 0。我可以理解这应该是因为,我们正在最小化“负面”可能性。即使损失值在减少,我也无法得到准确的结果。有人可以帮助验证我为损失函数编写的代码是否正确。
对于训练神经网络(特别是 RNN)来说,这种损失性质也是可取的吗?
谢谢
【问题讨论】:
标签: tensorflow statistics deep-learning lstm