我正在使用tensorflow分发API进行采样,以下是我正在使用的示例代码,但我发现概率大于1,然后记录概率小于0.我尝试了CPU和GPU,两者都产生了这个奇怪的结果。张量流为1.3。
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import tensorflow as tf
from sklearn.datasets import load_boston
from sklearn.preprocessing import scale
from matplotlib import pyplot as plt
import numpy as np
learning_rate = 0.01
total_features, total_prices = load_boston(True)
# Keep 300 samples for training
train_features = scale(total_features[:300])
train_prices = total_prices[:300]
x = tf.placeholder(tf.float32, [None, 13])
l1 = tf.layers.dense(inputs=x, units=20, activation=tf.nn.elu)
l2 = tf.layers.dense(inputs=l1, units=20, activation=tf.nn.elu)
mu = tf.squeeze(tf.layers.dense(inputs=l2, units=1))
sigma = tf.squeeze(tf.layers.dense(inputs=l2, units=1))
sigma = tf.nn.softplus(sigma) + 1e-5
normal_dist = tf.contrib.distributions.Normal(mu, sigma)
samples = tf.squeeze(normal_dist._sample_n(1))
log_prob = -normal_dist.log_prob(samples)
prob = normal_dist.prob(samples)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
avg_cost = 0.0
feed_dict = {x: train_features}
p = sess.run(prob, feed_dict)
lp = sess.run(log_prob, feed_dict)
p是我的概率输出 和lp是对数概率
谢谢!
答案 0 :(得分:2)
函数.prob和.log_prob是正态分布的PDF和日志PDF:https://en.wikipedia.org/wiki/Probability_density_function。请注意,PDF不必评估为0到1之间的值;它在一个范围内的积分(与CDF有关)必须在0和1之间。
考虑mu = 0
和sigma = 1e-4
的情况。如果我们使用正态分布的PDF:https://en.wikipedia.org/wiki/Normal_distribution,那么PDF(0)〜= 4000!但是,如果我们要整合PDF并获得CDF(或直接使用CDF),那么我们将始终获得0到1之间的值。