Python 如何在Tensorflow中禁用回溯
我有一个用Hessian的Newton-Rapson方法优化函数的代码Python 如何在Tensorflow中禁用回溯,python,tensorflow,Python,Tensorflow,我有一个用Hessian的Newton-Rapson方法优化函数的代码 import tensorflow as tf var = tf.Variable([3., -1., 0., 1.], dtype=tf.float32, name="var") for i in range(10): with tf.GradientTape() as tape1: with tf.GradientTape() as tape2:
import tensorflow as tf
var = tf.Variable([3., -1., 0., 1.], dtype=tf.float32, name="var")
for i in range(10):
with tf.GradientTape() as tape1:
with tf.GradientTape() as tape2:
func1 = (var[0] + 10*var[1])**2 + (5 *(var[2] - var[3])**2) + (var[1] - (2 * var[2]))**4 + (10*(var[0] - var[3])**4)
g = tape1.gradient(func1, var)
gt = tf.reshape(g, [1, 4])
h = tape2.jacobian(g, var)
h_inv = tf.linalg.inv(h)
dot = tf.matmul( gt, h_inv )
var_next = var - dot
var = tf.Variable([var_next[0, 0], var_next[0, 1], var_next[0, 2], var_next[0, 3]])
print('jacobian at ', i , 'step: ', gt)
当我把它放到循环中时,我得到:
警告:tensorflow:最近6次调用中有6次调用触发了tf.function Retracting
我知道TF函数被回溯时会发生这种情况,因为它的参数在形状或数据类型(对于张量)甚至值(Python或np对象或变量)上都会发生变化。在我的例子中,参数更改是正常的。使用以下解决方法关闭警告
import logging, os
logging.disable(logging.WARNING)
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
import tensorflow as tf
var = tf.Variable([3., -1., 0., 1.], dtype=tf.float32, name="var")
for i in range(10):
with tf.GradientTape() as tape1:
with tf.GradientTape() as tape2:
func1 = (var[0] + 10*var[1])**2 + (5 *(var[2] - var[3])**2) + (var[1] - (2 * var[2]))**4 + (10*(var[0] - var[3])**4)
g = tape1.gradient(func1, var)
gt = tf.reshape(g, [1, 4])
h = tape2.jacobian(g, var)
h_inv = tf.linalg.inv(h)
dot = tf.matmul( gt, h_inv )
var_next = var - dot
var = tf.Variable([var_next[0, 0], var_next[0, 1], var_next[0, 2], var_next[0, 3]])
print('jacobian at ', i , 'step: ', gt)
输出
jacobian at 0 step: tf.Tensor([[ 306. -144.00002 -2. -310. ]], shape=(1, 4), dtype=float32)
jacobian at 1 step: tf.Tensor([[ 94.81535 -1.1852257 2.3704371 -94.815445 ]], shape=(1, 4), dtype=float32)
jacobian at 2 step: tf.Tensor([[ 28.09341 -0.35116196 0.7023215 -28.093409 ]], shape=(1, 4), dtype=float32)
jacobian at 3 step: tf.Tensor([[ 8.324021 -0.1040398 0.20810343 -8.324027 ]], shape=(1, 4), dtype=float32)
jacobian at 4 step: tf.Tensor([[ 2.4663827 -0.03082165 0.0616576 -2.466382 ]], shape=(1, 4), dtype=float32)
jacobian at 5 step: tf.Tensor([[ 0.73078156 -0.00913089 0.01827012 -0.7307824 ]], shape=(1, 4), dtype=float32)
jacobian at 6 step: tf.Tensor([[ 0.21652769 -0.00270531 0.0054124 -0.21652697]], shape=(1, 4), dtype=float32)
jacobian at 7 step: tf.Tensor([[ 0.06415595 -0.00080431 0.00160385 -0.06415619]], shape=(1, 4), dtype=float32)
jacobian at 8 step: tf.Tensor([[ 0.01900928 -0.00023716 0.00047522 -0.01900923]], shape=(1, 4), dtype=float32)
jacobian at 9 step: tf.Tensor([[ 5.6324019e-03 -7.0029724e-05 1.4065549e-04 -5.6322156e-03]], shape=(1, 4), dtype=float32)
我在Tensorflow 2.4.1中没有看到任何警告。您能试用最新的Tensorflow版本并确认吗?谢谢!