Skip to main content
 首页 » 编程设计

python之TensorFlow 2.0 中的基本函数最小化和变量跟踪

2024年11月24日31dflying

我正尝试在 TensorFlow 2.0 中执行最基本的函数最小化,与问题 Tensorflow 2.0: minimize a simple function 中的完全一样。 ,但是我无法使那里描述的解决方案起作用。这是我的尝试,大部分是复制粘贴的,但添加了一些似乎缺失的部分。

import tensorflow as tf 
 
x = tf.Variable(2, name='x', trainable=True, dtype=tf.float32) 
with tf.GradientTape() as t: 
    y = tf.math.square(x) 
 
# Is the tape that computes the gradients! 
trainable_variables = [x] 
 
#### Option 2 
# To use minimize you have to define your loss computation as a funcction 
def compute_loss(): 
    y = tf.math.square(x) 
    return y 
 
opt = tf.optimizers.Adam(learning_rate=0.001) 
train = opt.minimize(compute_loss, var_list=trainable_variables) 
 
print("x:", x) 
print("y:", y) 

输出:

x: <tf.Variable 'x:0' shape=() dtype=float32, numpy=1.999> 
y: tf.Tensor(4.0, shape=(), dtype=float32) 

所以它说最小值是 x=1.999,但显然这是错误的。所以发生了什么事?我想它只执行了一个 minimiser 循环之类的?如果是这样,那么“最小化”似乎是该函数的一个糟糕名称。这应该如何工作?

附带说明一下,我还需要知道在损失函数中计算的中间变量的值(该示例只有 y,但想象一下它需要几个步骤来计算 y 我想要所有这些数字)。我认为我也没有正确使用渐变带,它与损失函数中的计算有任何关系对我来说并不明显(我只是从另一个问题中复制了这些东西)。

请您参考如下方法:

您需要多次调用minimize,因为minimize 只执行一个优化步骤。

以下应该有效

import tensorflow as tf 
 
x = tf.Variable(2, name='x', trainable=True, dtype=tf.float32) 
 
# Is the tape that computes the gradients! 
trainable_variables = [x] 
 
# To use minimize you have to define your loss computation as a funcction 
class Model(): 
    def __init__(self): 
        self.y = 0 
 
    def compute_loss(self): 
        self.y = tf.math.square(x) 
        return self.y 
 
opt = tf.optimizers.Adam(learning_rate=0.01) 
model = Model() 
for i in range(1000): 
    train = opt.minimize(model.compute_loss, var_list=trainable_variables) 
 
print("x:", x) 
print("y:", model.y)