Python 如何获得TensorFlow中的对数概率?
我正在尝试将pytorch脚本转换为tensorflow,需要从分类分布中获取日志概率。但tensorflow计算的对数概率与pytorch的对数概率不同,即使使用相同的种子。这就是我到目前为止所做的Python 如何获得TensorFlow中的对数概率?,python,tensorflow,pytorch,tensorflow2.0,tensorflow-probability,Python,Tensorflow,Pytorch,Tensorflow2.0,Tensorflow Probability,我正在尝试将pytorch脚本转换为tensorflow,需要从分类分布中获取日志概率。但tensorflow计算的对数概率与pytorch的对数概率不同,即使使用相同的种子。这就是我到目前为止所做的 import torch from torch.distributions import Categorical import tensorflow as tf import tensorflow_probability as tfp torch.manual_seed(1) tf.rando
import torch
from torch.distributions import Categorical
import tensorflow as tf
import tensorflow_probability as tfp
torch.manual_seed(1)
tf.random.set_seed(1)
probs =[0.4,0.6]
m = Categorical(torch.tensor(probs))
action = m.sample()
n = tfp.distributions.Categorical(probs)
print("pytorch",m.log_prob(action))
print("Tensorflow", tf.math.log(n.prob(action.item())))
将日志作为默认参数。它们正在被标准化,由此产生的构建分布概率为[.45、.55]
您需要将tfp分布构建为:
tfp.distributions.Categorical(probs=probs)
tfp.distributions.Categorical(probs=probs)