使用TensorBoard回调触发Graph张量TypeError错误,寻求解决方案
问题描述
添加TensorBoard回调时触发以下错误,移除回调则无异常:
TypeError: An op outside of the function building code is being passed a "Graph" tensor.
错误提示说明:若函数构建代码中包含tf.init_scope,Graph张量可能会泄漏出函数构建上下文。示例失败代码:
def has_init_scope(): my_constant = tf.constant(1.) with tf.init_scope(): added = my_constant * 2
涉及的Graph张量名称为:output_4/kernel:0
可复现代码如下(已补全必要导入):
import tensorflow as tf from tensorflow import keras import numpy as np import datetime 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,)
解决方案
这个错误的核心是experimental_run_tf_function=False与TensorBoard的histogram_freq=1不兼容——禁用TF函数运行时,TensorBoard收集权重直方图的操作会非法访问Graph张量。
可选修复方案:
- 方案1(推荐):删除
experimental_run_tf_function=False,让模型默认使用TF函数执行:model_test_gt_layer_.compile(tf.keras.optimizers.SGD(), loss='mse') - 方案2:保留
experimental_run_tf_function=False,但关闭TensorBoard的直方图收集:tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=log_dir, histogram_freq=0) - 方案3:启用急切执行模式(取消注释代码中的
run_eagerly=True),但会牺牲部分训练速度:model_test_gt_layer_.compile(tf.keras.optimizers.SGD(), loss='mse', experimental_run_tf_function=False, run_eagerly=True,)
内容的提问来源于stack exchange,提问作者Mihai.Mehe
相关产品推荐
相关产品推荐

