我在尝试在TensorFlow中实现自己的损失函数时,总是得到None
梯度。为了重现这个问题,我已经将程序简化为一个最小的例子。我定义了一个非常简单的模型:
import tensorflow as tfmodel = tf.keras.Sequential( [ tf.keras.Input(shape=(3,), name="input"), tf.keras.layers.Dense(64, activation="relu", name="layer2"), tf.keras.layers.Dense(3, activation="softmax", name="output"), ])
然后定义了一个非常简单(但可能无用)的损失函数:
def dummy_loss(x): return tf.reduce_sum(x)def train(model, inputs, learning_rate): outputs = model(inputs) with tf.GradientTape() as t: current_loss = dummy_loss(outputs) temp = t.gradient(current_loss, model.trainable_weights)train(model, tf.random.normal((10, 3)), learning_rate=0.001)
但是t.gradient(current_loss, model.trainable_weights)
只返回一个None
值的列表,即[None, None, None, None]
。为什么会这样?我做错了什么?可能是我对TensorFlow的工作原理有误解吗?
回答:
你需要在GradientTape
的上下文中运行(即前向传递)计算图或模型,这样模型中的所有操作才能被记录下来:
with tf.GradientTape() as t: outputs = model(inputs) # 这行应该在上下文管理器内 current_loss = dummy_loss(outputs)