TensorFlow 1.14中使用tf.compat.v1.keras.utils.Progbar更新训练Loss和Accuracy时出现AttributeError问题的求助
Fix AttributeError When Updating Tensor-Based Metrics with tf.compat.v1.keras.utils.Progbar
Let's break down the issue and fix it step by step:
What's Causing the Error?
Two key mistakes are triggering that AttributeError:
- Incorrect average calculation for accuracy: You're calling
np.mean(batch_train_acc)instead of using the accumulatedtrain_meta_acclist. This means you're trying to compute the mean of a single Tensor object instead of a list of values. - Trying to process Tensor objects directly with NumPy: TensorFlow 1.x's Tensor objects can't be directly passed to NumPy functions like
np.mean()—you need to convert them to plain numeric values first. The error aboutDTypecomes from NumPy trying to handle the Tensor's internal type system incorrectly.
Solution Code
Here's the corrected version of your code, with explanations:
import numpy as np import tensorflow as tf train_progbar = tf.compat.v1.keras.utils.Progbar(train_data.steps) train_meta_loss = [] train_meta_acc = [] for i in range(train_data.steps): # Get batch metrics (Tensor objects) batch_train_loss, batch_train_acc = maml.train_on_batch( train_data.get_one_batch(), inner_optimizer, inner_step=1, outer_optimizer=outer_optimizer ) # Convert Tensors to NumPy-compatible values (TF 1.x compatible) batch_loss_val = tf.keras.backend.eval(batch_train_loss) batch_acc_val = tf.keras.backend.eval(batch_train_acc) # Add to accumulated lists train_meta_loss.append(batch_loss_val) train_meta_acc.append(batch_acc_val) # Calculate averages from the accumulated lists mean_train_meta_loss = np.mean(train_meta_loss) mean_train_meta_acc = np.mean(train_meta_acc) # Fixed: use the accumulated list # Update Progbar with numeric values train_progbar.update(i+1, [('loss', mean_train_meta_loss), ('accuracy', mean_train_meta_acc)])
Even Better: Let Progbar Handle Averages
Progbar is designed to automatically compute running averages for you, so you don't need to manually track and average the accumulated lists. This is more efficient and cleaner:
train_progbar = tf.compat.v1.keras.utils.Progbar(train_data.steps) train_meta_loss = [] train_meta_acc = [] for i in range(train_data.steps): batch_train_loss, batch_train_acc = maml.train_on_batch( train_data.get_one_batch(), inner_optimizer, inner_step=1, outer_optimizer=outer_optimizer ) batch_loss_val = tf.keras.backend.eval(batch_train_loss) batch_acc_val = tf.keras.backend.eval(batch_train_acc) # Save history if needed train_meta_loss.append(batch_loss_val) train_meta_acc.append(batch_acc_val) # Let Progbar calculate the running average automatically train_progbar.update(i+1, [('loss', batch_loss_val), ('accuracy', batch_acc_val)])
Key Notes for TF 1.14
- If you've enabled eager execution (via
tf.enable_eager_execution()), you can replacetf.keras.backend.eval(tensor)withtensor.numpy()for simpler syntax. - Always ensure you're working with numeric values (not Tensors) when passing data to NumPy functions or Progbar updates.
内容的提问来源于stack exchange,提问作者Ken
相关产品推荐
相关产品推荐

