问题描述
推理的时候报WARNING:tf.function has experimental_relax_shapes=True
例如:数据传输过程中数据不时出现丢失的情况,偶尔会丢失一部分数据
APP 中接收数据代码:
WARNING:tensorflow:3 out of the last 3 calls to triggered tf.function retracing. Tracing is expensive and the excessive number of tracings is likely due to passing python objects instead of tensors. Also, tf.function has experimental_relax_shapes=True option that relaxes argument shapes that can avoid unnecessary retracing
tf.function本质上就是一个函数修饰器,它能够帮助将用户定义的python风格的函数代码转化成高效的tensorflow计算图
@tf.function
def fun1():
a = tf.constant([[1,1],[1.,1.]])
x = tf.constant([[1.,0.],[0.,1.]])
y = tf.matmul(a, x)
return y
fun1()
1.该函数被执行和跟踪(tracing). Eager模型被关闭禁用,所有的tf.constant方法都被当做tf.Operation来构建Graph
2. AutoGraph被用于检测代码中是否存在能够被转换为graph的等价操作
原因分析:
如上提示,tf.function使用了静态图的模式,部分代码有使用numpy()函数转换导致
解决方案:
增加以下代码解决:tf.compat.v1.disable_eager_execution() ,即需要在tensorflow2.x下面关闭eager mode模式