Keras,张量流:变量的初始化器...来自控制流构造,循环或条件内部 [英] Keras, tensorflow: Initializer for variable... is from inside a control-flow construct, a loop or conditional

查看:353
本文介绍了Keras,张量流:变量的初始化器...来自控制流构造,循环或条件内部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在训练一个遮罩r-cnn模型,请参阅github上的这个代表: https://github.com/matterport/Mask_RCNN

I am training a mask r-cnn model refer to this rep on github: https://github.com/matterport/Mask_RCNN

我遇到一个似乎是使用Keras的问题,所以我来这里.

I meet a problem which seems to be an issue of using Keras, so I come here.

代码从感兴趣区域(rois)和特征图计算蒙版:

The code calculates masks from region of interest(rois) and feature map:

mrcnn_mask = build_fpn_mask_graph(rois, mrcnn_feature_maps,
    config.IMAGE_SHAPE,
    config.MASK_POOL_SIZE,
    config.NUM_CLASSES)

但是,有时rois可能全为零,在这种情况下,我想直接返回全零.所以,我像这样使用tf.cond:

However, sometimes rois maybe all zeros, in which case I want to return all zeros directly. So, I use tf.cond like this:

def ff_true():
    mrcnn_mask = build_fpn_mask_graph(rois, mrcnn_feature_maps,
       config.IMAGE_SHAPE,
       config.MASK_POOL_SIZE,
       config.NUM_CLASSES)

def ff_false():
    return tf.zeros_like(target_mask)

mrcnn_mask = KL.Lambda(lambda x: tf.cond(tf.equal(tf.reduce_mean(x), 0), 
    ff_true, ff_true)) (rois)

这会引发错误:

ValueError:变量lambda_5/cond/mrcnn_mask_conv1/kernel/的初始化程序来自控制流结构内部,例如循环或条件.在循环或条件内创建变量时,请使用lambda作为初始化程序.

ValueError: Initializer for variable lambda_5/cond/mrcnn_mask_conv1/kernel/ is from inside a control-flow construct, such as a loop or conditional. When creating a variable inside a loop or conditional, use a lambda as the initializer.

我用google搜索,但没有得到有用的信息. 这似乎是错误地使用keras/tensorflow的问题. 任何线索都将受到欢迎!

I google it but no useful info got. This seems to be an issue of falsely using keras/tensorflow. Any clue will be welcomed!

顺便说一句,如果我使用此代码,它将没有错误(但我不想提前计算出一个值):

BTW, if I use this code, it will be no error(but I don't want to calculate a in advance):

a = build_fpn_mask_graph(rois, mrcnn_feature_maps,
    config.IMAGE_SHAPE,
    config.MASK_POOL_SIZE,
    config.NUM_CLASSES)   
def ff_true():
    return a
def ff_false():
    return tf.zeros_like(target_mask)

mrcnn_mask = KL.Lambda(lambda x: tf.cond(tf.equal(tf.reduce_mean(x), 0), 
    ff_true, ff_true)) (rois)

推荐答案

该错误基本上就是该消息所说的内容.您不能在条件中包含变量初始值设定项.与普通编程语言的粗略类比是:

The error is basically what the message says. You cannot have a variable initializer inside a conditional. A crude analogy to normal programming languages is:

if my_condition:
    a = 1
print a    # can't do this. a might be uninitialized.

这是一个简单的示例,用于说明此问题和错误消息中建议的修复程序:

Here is a simple example to illustrate this issue and the fix suggested in the error message:

import tensorflow as tf

def cond(i, _):
  return i < 10

def body(i, _):
  zero = tf.zeros([], dtype=tf.int32)
  v = tf.Variable(initial_value=zero)
  return (i + 1, v.read_value())

def body_ok(i, _):
  zero = lambda: tf.zeros([], dtype=tf.int32)
  v = tf.Variable(initial_value=zero, dtype=tf.int32)
  return (i + 1, v.read_value())

tf.while_loop(cond, body, [0, 0])

这正在使用tf.while_loop,但为此目的与tf.cond相同.如果按原样运行此代码,则会收到相同的错误.如果将body替换为body_ok,一切都会好起来的.原因是,当初始化器是一个函数时,tensorflow可以将其放置在控制流上下文之外",以确保其始终运行.

This is using tf.while_loop but it is the same as tf.cond for this purposes. If you run this code as is, you will get the same error. If you replace body with body_ok everything will be fine. The reason is that when the initializer is a function, tensorflow can place it "outside of the control flow context" to make sure it always runs.

为澄清将来的读者可能的困惑,先计算a"的方法不是理想的,而是出于一个微妙的原因.首先,请记住,您在此处正在构建计算图(假设您未使用急于执行).因此,您实际上并不是在计算a.您只是在定义 的计算方式. Tensorflow运行时根据session.run()的参数决定在运行时需要计算的内容.因此,可能希望如果条件为false,则返回的a分支将不会执行(因为不需要).不幸的是,这不是TensorFlow运行时的工作方式.您可以在第一个答案的此处中找到更多详细信息,但简要地说, TensorFlow运行时将为任一分支执行所有依赖项,只有true_fn/false_fn内部的操作将有条件地执行.

To clarify a possible confusion for future readers, the approach to "compute a first" is not ideal but for a subtle reason. First, remember that what you are doing here is building a computation graph (assuming you are not using eager execution). So, you are not actually computing a. You are just defining how it can be computed. Tensorflow runtime decides what needs to be computed at runtime, depending on the arguments to session.run(). So, one might expect that the if the condition is false, the branch returning a will not be executed (since it is not needed). Unfortunately, this is not how TensorFlow runtime works. You can find more details in the first answer here, but briefly, TensorFlow runtime will execute all dependencies for either branch, only the operations inside the true_fn/false_fn will be executed conditionally.

这篇关于Keras,张量流:变量的初始化器...来自控制流构造,循环或条件内部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆