Tensorflow:命名变量

时间:2018-01-19 19:21:48

标签: python tensorflow naming-conventions

我想知道在哪里放置以下功能以及如何命名变量" r"和" p":

def fit_mme(sample_data, name=None):
    """
        Calculates the Maximum-of-Momentum Estimator of NB(r, p) for a given NxM matrix `sample_data`

        :param sample_data: `(N, M)` matrix with `M` distributions containing `N` observed values each
        :return: estimated values of `r` and `p`
        """
    with tf.name_scope(name, "MME"):
        mean = tf.reduce_mean(sample_data, axis=0, name="mean")
        variance = tf.reduce_mean(tf.square(sample_data - mean),
                                  axis=0,
                                  name="variance")
        nan = tf.fill(tf.shape(variance), math.nan, name="NaN_constant")

        r_by_mean = tf.where(tf.less(mean, variance),
                             mean / (variance - mean),
                             nan)
        r = r_by_mean * mean
        p = 1 / (r_by_mean + 1)
        return r, p

正如你所看到的,我已经命名为#34;意思是"," variance"和" nan",但我如何命名变量" r"和" p"?

另外,我应该将此方法打包成" tf.contrib.distributions.NegativeBinomial"的子类。或者我应该为它创建一些utils.py文件?

1 个答案:

答案 0 :(得分:1)

你可以这样做:

r = tf.multiply(r_by_mean, mean, name = 'r')

p = tf.divide(1, tf.add(r_by_mean, 1), name = 'p')

或者您可以(如Name the output of an expression in Tensorflow中所述):

r = r_by_mean * mean
r = tf.identity(r, name = 'r')

p = 1 / (r_by_mean + 1)
p = tf.identity(p, name = 'p')
  

另外,我应该将此方法打包到“tf.contrib.distributions.NegativeBinomial”的子类中,还是应该为它创建一些utils.py文件?

我不会把它放到任何'官方'命名空间,比如tf,但是使用你自己的文件,很明显这是你/你的项目而不是Tensorflow添加的功能(除非你需要修复/覆盖一些标准张量流功能)。