繁体   English   中英

在tensorflow中使用metric.Mean()

[英]Using metric.Mean() in tensorflow

我正在研究Google Colabs中的tensorflow教程,并且已按照以下链接中的说明运行了所有内容:

https://www.tensorflow.org/tutorials/eager/custom_training_walkthrough

我正在运行以下代码:

## Note: Rerunning this cell uses the same model variables

# keep results for plotting
train_loss_results = []
train_accuracy_results = []

num_epochs = 201

for epoch in range(num_epochs):
  epoch_loss_avg = tf.metrics.Mean()
  epoch_accuracy = tf.metrics.Accuracy()

  # Training loop - using batches of 32
  for x, y in train_dataset:
    # Optimize the model
    loss_value, grads = grad(model, x, y)
    optimizer.apply_gradients(zip(grads, model.variables),
                              global_step)

    # Track progress
    epoch_loss_avg(loss_value)  # add current batch loss
    # compare predicted label to actual label
    epoch_accuracy(tf.argmax(model(x), axis=1, output_type=tf.int32), y)

  # end epoch
  train_loss_results.append(epoch_loss_avg.result())
  train_accuracy_results.append(epoch_accuracy.result())

  if epoch % 50 == 0:
    print("Epoch {:03d}: Loss: {:.3f}, Accuracy: {:.3%}".format(epoch,
                                                                epoch_loss_avg.result(),
                                                                epoch_accuracy.result()))

但是当我运行它时,出现以下错误:

AttributeError: module 'tensorflow._api.v1.metrics' has no attribute 'Mean'

据我了解,他们试图在代码中执行的操作是将tf.metrics.Mean()函数分配给epoch_loss_avg,然后将其进一步应用到epoch_loss_avg(loss_value)中。 所以我想自从编写本教程以来,Tensorflow中的某些内容可能已经发生了变化,因此我尝试将其重写如下:

## Note: Rerunning this cell uses the same model variables

# Keep results for plotting
train_loss_results = []
train_accuracy_result = []

num_epochs = 201

for epoch in range(num_epochs):
  #epoch_loss_avg = tf.metrics.Mean()
  #epoch_accuracy = tf.metrics.Accuracy()

  # Training loop - using batches of 32
  for x, y in train_dataset:
    # Optimize the model
    loss_value, grads = grad(model, x, y)
    optimizer.apply_gradients(zip(grads, model.variables),
                             global_step)

    # Track progress
    mean_temp = tf.metrics.mean(loss_value) # Add current batch loss
    # Compare the predicted label to actual label
    acc_temp = tf.metrics.accuracy(tf.argmax(model(x), axis = 1, output_type = tf.int32), y)

  # End epoch
  train_loss_results.append(mean_temp)
  train_accuracy_results.append(acc_temp)

  if epoch % 50 == 0:
    print("Epoch {:03d}: Loss: {:,3f}, Accuracy: {:.3f}".format(epoch,
                                                               epoch_loss_avg.result(),
                                                               epoch_accuracy.result()))

该函数直接运行的地方,但是现在我又收到了一条错误消息:

RuntimeError: tf.metrics.mean is not supported when eager execution is enabled.

所以我的问题是,是否有另一种写方法来获得相同的结果,我对正在发生的事情的解释正确,如果不是,我正在解释什么?

谢谢

为了使用Eager Execution,您需要将tf.metrics.Meantf.metrics.Accuracy更改为:

epoch_loss_avg = tf.contrib.eager.metrics.Mean()
epoch_accuracy = tf.contrib.eager.metrics.Accuracy()

还有tf.Variable to:

global_step = tf.contrib.eager.Variable(0)

据我了解,他们试图在代码中执行的操作是将tf.metrics.Mean()函数分配给epoch_loss_avg,然后将其进一步应用到epoch_loss_avg(loss_value)中。

是的,在epoch_loss_avg = tf.metrics.Mean()他们创建了计算平均值的操作,然后在epoch_loss_avg(loss_value)行中累积了批次的损失。 因此,在纪元结束时,考虑到数据集中的所有批次,我们将产生平均损失,这将导致纪元的损失(行epoch_loss_avg.result() )。

关于第二个错误:如您所见,如果启用了急切执行,则tf.metrics.mean会引发RuntimeError 您需要改用tf.contrib.eager.metrics

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM