Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何获得TensorFlow中的对数概率?_Python_Tensorflow_Pytorch_Tensorflow2.0_Tensorflow Probability - Fatal编程技术网

Python 如何获得TensorFlow中的对数概率?

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

我正在尝试将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.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)