简体   繁体   English

更新每批的损失值以获得平均时期损失

[英]Update loss value each batch to get an averaged epoch loss

I want to create an operation similar to the one you can obtain using tf.metrics and their update_op value. 我想创建一个类似于使用tf.metrics及其update_op值可以获得的操作。 When you execute this in tf: 在tf中执行此命令时:

acc, update_op = tf.metrics.accuracy(tf.argmax(probs, 1), labels, name="accuracy")

The update_op value is updated in each call. 在每个调用中都会更新update_op值。

So I want to do the same with the loss. 所以我想对损失做同样的事情。 I have tried the following code: update_loss = tf.Variable(0., name="loss") 我尝试了以下代码:update_loss = tf.Variable(0。,name =“ loss”)

loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=model.logits, labels=labels))
update_loss.assign(update_loss + loss)

But always I run: 但是我总是跑:

init_vars = [tf.local_variables_initializer(), 

tf.global_variables_initializer()]
with tf.Session() as sess:
    loss_val = sess.run(update_loss)

I get a value of 0. Any idea? 我的值为0。有什么想法吗?

EDIT: 编辑:

I must point that the value of tensor loss is not zero during the execution 我必须指出在执行过程中张量损失的值不为零

Ok, I have discovered a plausible solution which can work but it's not really solving my doubt... based on this post (5.2 Tensorflow - Batch accuracy ) 好的,我发现了一个可行的解决方案,可以解决这个问题,但并不能真正解决我的疑问……基于此帖子 (5.2 Tensorflow-批处理精度)

It consists on creating a function that uses the last obtained loss value and pass it to a function through feed_dict that updates a placeholder with the cumulative value: 它包括创建一个使用最后获得的损失值的函数,并通过feed_dict将其传递给函数,该函数使用累计值更新占位符:

session.run(tf_metric_update, feed_dict=feed_dict)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Keras 的自定义批量训练中获得每个时期的损失? - How to get the loss for each epoch in custom batch training in Keras? 我如何在每个时期而不是每个批次中获得损失? - How do I get a loss per epoch and not per batch? 每个时期的输出平均损失:奇怪的结果 - Output average loss on each epoch : weird results 为什么我的损失函数随着每个 epoch 增加? - Why is my loss function increasing with each epoch? Keras 极高的损失,不会随着每个 epoch 减少 - Keras extremely high loss, not decreasing with each epoch 每个 epoch 之后的验证准确率和损失是相同的 - Validation accuracy and loss is the same after each epoch 如何在训练期间修改每个时期的损失函数内的变量? - How to modify a variable inside the loss function in each epoch during training? 在 Python H2O 中检索每个时期的损失 - Retrieve loss for each epoch in Python H2O 为训练和测试数据集绘制每个时期的损失和准确度 - Plot loss and accuracy over each epoch for both training and test datasets Pytorch 闪电打印精度和每个 epoch 结束时的损失 - Pytorch lightning print accuracy and loss at the end of each epoch
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM