【问题标题】:Use scipy.stats.entropy in Tensorflow Graph在 TensorFlow Graph 中使用 scipy.stats.entropy
【发布时间】:2018-11-29 20:20:21
【问题描述】:

我的目标是计算以下代码 sn-p 中称为 p_distb 和 q_distb 的两个概率密度函数之间的距离。我尝试利用 Jensen-Shannon-divergence 来实现这一点,因为它是对称且有界的,必要的 entropy 函数是从 scipy.stats 导入的。

当我尝试运行图表时,出现以下错误:

如果 len(qk) != len(pk): TypeError: len() of unsized object

显然,scipy.stats.entropy 无法处理 tensorflow 张量,即使这些张量是向下兼容的,并且应该像 numpy 数组一样工作。

有没有人可以解决这个问题?

非常感谢!!

from scipy.stats import entropy
import tensorflow as tf
import numpy as np

graph = tf.Graph()
with graph.as_default():

i_dim = 8
j_dim = 8

input_dim = 201

weights = tf.Variable(tf.random_normal(shape=[i_dim*j_dim, input_dim]))

input_vector = tf.Variable(tf.random_normal(shape=[input_dim,1]))

min_codebook_dist = []

for index in range(i_dim*j_dim):

    p_distb = tf.div(weights[index,:],tf.reduce_sum(weights[index,:]))
    p_distb = tf.reshape(p_distb, shape=[input_dim,])

    q_distb = tf.div(input_vector,tf.reduce_sum(input_vector))
    q_distb = tf.reshape(input_vector, shape=[input_dim,])

    m_distb = tf.div(tf.add(p_distb,q_distb),2)

    dist_pq = np.sqrt((entropy(p_distb[:], m_distb[:]) + entropy(q_distb[:], m_distb[:])) / 2)

    min_codebook_dist.append(dist_pq)

sess = tf.InteractiveSession()
init_op = tf.initialize_all_variables()
sess.run(init_op)

【问题讨论】:

    标签: numpy tensorflow scipy tensor entropy


    【解决方案1】:

    TensorFlow 张量不向下兼容 numpy 数组。您可以尝试将对 scipy 的调用包装在 tf.py_func 中以调用 scipy。

    【讨论】:

      猜你喜欢
      • 2014-12-31
      • 2020-08-15
      • 1970-01-01
      • 2019-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-19
      • 2022-06-17
      相关资源
      最近更新 更多