【问题标题】:tensorflow 2.0: An op outside of the function building code is being passedtensorflow 2.0:正在传递函数构建代码之外的操作
【发布时间】:2019-12-08 05:45:16
【问题描述】:

我收到一个错误:

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。这样做的正确方法是什么?

【问题讨论】:

    标签: python tensorflow tensorflow2.0


    【解决方案1】:

    更新:所以这肯定来自一些渐变胶带层中包含的 self.loss。这样做的正确方法是什么?

    我认为正确的做法是通过

    self.add_loss(<your loss tensor>)
    

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

    (编辑抱歉,我没注意你发帖的日期,所以我想这已经不是很有用了哈哈)

    【讨论】:

    • 酷,会尽量记住测试。我认为在 tf2 的前几个版本中可能存在一些错误
    【解决方案2】:

    几分钟前刚遇到同样的问题,在我的情况下,我想修改我的损失函数类中的状态,这是我在你的情况下的解决方法。

    顺便说一句 @simon 给了我如何正确评估这一点的灵感。所以对他的支持!

    看来你应该为你在训练时要改变的属性创建一个tf.Variable。请注意,您对 self.output_dimself.num_masked 等其他属性没有任何问题。

    试试这个:

    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
            )
    
        ###CHANGE HERE
        self.loss = tf.Variable(0.0)
    
    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()
    
        ### CHANGE HERE
        self.loss.assign(tf.reduce_mean(nvp.log_prob(*inputs)))
        # return nvp.bijector.forward(*inputs)
        return nvp.bijector.inverse(*inputs)
    

    查看这个answer on github issue,类似的问题!

    【讨论】:

      猜你喜欢
      • 2021-09-27
      • 2020-06-18
      • 2020-12-24
      • 2020-02-22
      • 2020-09-30
      • 1970-01-01
      • 2020-06-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多