gpt4 book ai didi

python - "Please wrap your loss computation in a zero argument ` lambda`."是什么意思?

转载 作者:行者123 更新时间:2023-12-01 21:54:55 25 4
gpt4 key购买 nike

我编写了一个将 L2 损失添加到主要损失函数的代码:

def add_l2(model, penalty=0.001):
for layer in model.layers:
if "conv" in layer.name:
model.add_loss(penalty * tf.reduce_sum(tf.square(layer.trainable_variables[0])))
return

## training
@tf.function
def train_one_step(model, x, y, optimizer):
with tf.GradientTape() as tape:
logits = model(x, training=True)
loss = _criterion(y_true=y, y_pred=logits)

add_l2(model, 0.001)
loss += sum(model.losses)

grads = tape.gradient(loss, model.trainable_variables)
optimizer.apply_gradients(zip(grads, model.trainable_variables))
return loss, logits

当我开始训练时,出现如下错误:

ValueError: Expected a symbolic Tensors or a callable for the loss value. Please wrap your loss computation in a zero argument lambda.

这个错误是什么意思?我该如何治疗?

最佳答案

您的损失引用了模型层之一的变量(layer.trainable_variables[0]),因此需要将您的损失包装在零参数 lambda 中以使其成为可调用的。model.add_loss(lambda: penalty * tf.reduce_sum(tf.square(layer.trainable_variables[0])

有关更多详细信息,请查看官方文档 Here

关于python - "Please wrap your loss computation in a zero argument ` lambda`."是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58387852/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com