keras 张力板错误:函数构建代码之外的操作正在传递“Graph”Tensor

t5fffqht  于 2023-01-17  发布在  其他
关注(0)|答案(1)|浏览(128)

以下代码复制了我一直遇到的Tensorboard错误。完整错误:

TypeError: An op outside of the function building code is being passed a "Graph" tensor.

通过在函数构建代码中包含tf. init_scope,可以使GraphTensor泄漏到函数构建上下文之外。例如,以下函数将失败:

def has_init_scope():
    my_constant = tf.constant(1.)
    with tf.init_scope():
      added = my_constant * 2

图形Tensor的名称为:输出_4/内核:0

    • 使用tensorboard回调我得到了错误,没有它我没有。贝娄,回调被注解掉了。任何帮助都是非常感谢的。
    • 代码:**
input_gt_boxes = keras.layers.Input(
        shape=[None, 4], name="input_gt_boxes", dtype=tf.float32)

output_ = keras.layers.Dense(1, name='output')(input_gt_boxes)

model_test_gt_layer_ = tf.keras.models.Model([input_gt_boxes],
                                            [output_],
                                            name="m")

model_test_gt_layer_.compile(tf.keras.optimizers.SGD(), loss='mse', \
                           experimental_run_tf_function=False,
                           #run_eagerly=True,
                           )
model_test_gt_layer_.summary()

log_dir = "logs/fit/" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=log_dir, histogram_freq=1)

a = np.concatenate([np.expand_dims(np.arange(4).reshape(1,4),axis=0) for _ in range(100)], axis=0)
o= np.concatenate([np.zeros(100).reshape(100,1) for _ in range(1)], axis=0)

model_test_gt_layer_.fit(a, o, \
                        epochs=5, \
                        callbacks=[
                        #tensorboard_callback, 
                                  ], \
                        verbose=1,\
                        use_multiprocessing= False,)
aydmsdu9

aydmsdu91#

必须启用紧急执行:tf.compat.v1.enable_eager_execution(),并在.compile语句中设置run_eagerly=True

import tensorflow as tf
tf.compat.v1.enable_eager_execution()

print("Eagerly: ", tf.executing_eagerly())
input_gt_boxes = keras.layers.Input(
        shape=[None, 4], name="input_gt_boxes", dtype=tf.float32)

output_ = keras.layers.Dense(1, name='output')(input_gt_boxes)

model_test_gt_layer_ = tf.keras.models.Model([input_gt_boxes],
                                            [output_],
                                            name="m")

model_test_gt_layer_.compile(tf.keras.optimizers.SGD(), loss='mse', \
                           experimental_run_tf_function=False,
                           run_eagerly=True,
                           )
model_test_gt_layer_.summary()

log_dir = "logs/fit/" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=log_dir, histogram_freq=1)

a = np.concatenate([np.expand_dims(np.arange(4).reshape(1,4),axis=0) for _ in range(100)], axis=0)
o= np.concatenate([np.zeros(100).reshape(100,1) for _ in range(1)], axis=0)

model_test_gt_layer_.fit(a, o, \
                        epochs=5, \
                        callbacks=[
                        tensorboard_callback, 
                                  ], \
                        verbose=1,\
                        use_multiprocessing= False,)

相关问题