Skip to main content
 首页 » 编程设计

python之将 tensorflow 1.x.x 模型加载到 tensorflow 2.x.x

2024年08月12日33傻小

我有一个使用 TF1 创建的 SavedModel 正在加载 TF2。

我收到了图中每个变量的警告,它是:

WARNING:tensorflow:Unable to create a python object for variable <tf.Variable 'Encoder_en/hidden_layers/tanh_layer_0/bias:0' shape=(512,) dtype=float32_ref> because it is a reference variable. It may not be visible to training APIs. If this is a problem, consider rebuilding the SavedModel after running tf.compat.v1.enable_resource_variables(). 

我想优先修复这个警告,或者只是抑制它!

到目前为止我已经尝试过:

# In my python app 
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' 
 
# In my Dockerfile 
ENV TF_CPP_MIN_LOG_LEVEL 2 

编辑:这个模型来自 tensorflow hub,因此我没有构建它。

请您参考如下方法:

TensorFlow 中的登录在较新的版本中发生了变化,TF_CPP_MIN_LOG_LEVEL 不再使用(参见问题 #26348 和 #31870)。尝试使用 tf.get_logger().setLevel('ERROR')。

import tensorflow as tf 
tf.get_logger().warning('test') 
# WARNING:tensorflow:test 
tf.get_logger().setLevel('ERROR') 
tf.get_logger().warning('test') 
# (silence)