"sample must be less than `1`"张量流分布中的错误



我目前正在使用tensorflow的beta分发类,并遇到以下问题:

我的代码片段:

self.dist = tf.distributions.Beta(concentration1=alpha, concentration0=beta, validate_args=True, allow_nan_stats=False)
...
def neglogp(self, x):
clipped_x = tf.clip_by_value(x, 0.0000000001, 0.9999999999)
return tf.reduce_sum(-self.dist.log_prob(clipped_x), axis=-1)

错误:

tensorflow.python.framework.errors_impl.InvalidArgumentError: assertion failed: [sample must be less than `1`.] [Condition x < y did not hold element-wise:] [x (output/clip_by_value_1:0) = ] [[0.900631309 0.142189667 0.749241889...]...] [y (model/Beta/log_prob/ones:0) = ] [1]
[[node model/Beta/log_prob/assert_less/Assert/AssertGuard/Assert (defined at src/BetaDistribution.py:55) ]]

所以我想我可以使用clip_by_value函数来确保x中的每个值都低于1,但我仍然会得到同样的错误。我无论如何都不是tensorflow专家,也许有人知道为什么我仍然会出现这个错误,以及为什么剪辑显然不起作用。或者错误在其他地方,我不确定。

发现我的错误:

0.9999999999对于float32张量来说太精确了,所以它将其四舍五入为1,剪裁使用1作为最大值。

>>> x = tf.Variable([0.9999999])
>>> x.numpy()
array([0.9999999], dtype=float32)
>>> x = tf.Variable([0.99999999])
>>> x.numpy()
array([1.], dtype=float32)

最新更新