tensorflow 2.0:正在传递函数构建代码之外的操作

时间:2019-07-30 18:53:23

标签: python tensorflow tensorflow2.0

我遇到错误:

TypeError: An op outside of the function building code is being passed
a "Graph" tensor. It is possible to have Graph tensors
leak out of the function building context by including a
tf.init_scope in your function building code.
For example, the following function will fail:
  @tf.function
  def has_init_scope():
    my_constant = tf.constant(1.)
    with tf.init_scope():
      added = my_constant * 2

使用如下所示的NVP层:

import tensorflow_probability as tfp
tfb = tfp.bijectors
tfd = tfp.distributions
class NVPLayer(tf.keras.models.Model):

    def __init__(self, *, output_dim, num_masked, **kwargs):
        super().__init__(**kwargs)
        self.output_dim = output_dim
        self.num_masked = num_masked
        self.shift_and_log_scale_fn = tfb.real_nvp_default_template(
            hidden_layers=[2], # HERE HERE ADJUST THIS
            activation=None, # linear
            )
        self.loss = None

    def get_nvp(self):
        nvp = tfd.TransformedDistribution(
            distribution=tfd.MultivariateNormalDiag(loc=[0.] * self.output_dim),
            bijector=tfb.RealNVP(
                num_masked=self.num_masked,
                shift_and_log_scale_fn=self.shift_and_log_scale_fn)
            )
        return nvp

    def call(self, *inputs):
        nvp = self.get_nvp()
        self.loss = tf.reduce_mean(nvp.log_prob(*inputs)) # how else to do this?
        # return nvp.bijector.forward(*inputs)
        return nvp.bijector.inverse(*inputs)

我没有在任何地方打tf.init_scope。训练像这样的图层的简单版本似乎起作用。

我将尝试获得更详细的跟踪,但是我怀疑这与非急切模式的东西有关。

更新:因此,这肯定来自self.loss包含在某些渐变磁带层中。正确的方法是什么?

1 个答案:

答案 0 :(得分:1)

更新:因此,这肯定来自某些渐变磁带层中的self.loss。正确的方法是什么?

我认为正确的方法是

self.add_loss(<your loss tensor>)

({https://www.tensorflow.org/api_docs/python/tf/keras/layers/Layer#add_loss了解更多信息)

(编辑,抱歉,我没有注意您的帖子日期,所以我想这已经不再有用了)