gpt4 book ai didi

python - Tensorflow 2.0 : custom keras metric caused tf. 函数回溯警告

转载 作者:行者123 更新时间:2023-12-01 06:55:45 26 4
gpt4 key购买 nike

当我使用以下自定义指标(keras 样式)时:

from sklearn.metrics import classification_report, f1_score
from tensorflow.keras.callbacks import Callback

class Metrics(Callback):
def __init__(self, dev_data, classifier, dataloader):
self.best_f1_score = 0.0
self.dev_data = dev_data
self.classifier = classifier
self.predictor = Predictor(classifier, dataloader)
self.dataloader = dataloader

def on_epoch_end(self, epoch, logs=None):
print("start to evaluate....")
_, preds = self.predictor(self.dev_data)
y_trues, y_preds = [self.dataloader.label_vector(v["label"]) for v in self.dev_data], preds
f1 = f1_score(y_trues, y_preds, average="weighted")
print(classification_report(y_trues, y_preds,
target_names=self.dataloader.vocab.labels))
if f1 > self.best_f1_score:
self.best_f1_score = f1
self.classifier.save_model()
print("best metrics, save model...")

我收到以下警告:

W1106 10:49:14.171694 4745115072 def_function.py:474] 6 out of the last 11 calls to .distributed_function at 0x14a3f9d90> triggered tf.function retracing. Tracing is expensive and the excessive number of tracings is likely due to passing python objects instead of tensors. Also, tf.function has experimental_relax_shapes=True option that relaxes argument shapes that can avoid unnecessary retracing. Please refer to https://www.tensorflow.org/beta/tutorials/eager/tf_function#python_or_tensor_args and https://www.tensorflow.org/api_docs/python/tf/function for more details.

最佳答案

当 TF 函数因其参数的形状或数据类型(对于张量)甚至值(Python 或 np 对象或变量)发生变化而被回溯时,会出现此警告。

在一般情况下,修复方法是在传递给 Keras 或 TF 某处的自定义函数的定义之前使用 @tf.function(experimental_relax_shapes=True) 。这会尝试检测并避免不必要的回溯,但不能保证解决问题。

就您而言,我猜 Predictor 类是一个自定义类,因此请将 @tf.function(experimental_relax_shapes=True) 放在 Predictor.predict() 的定义之前。

关于python - Tensorflow 2.0 : custom keras metric caused tf. 函数回溯警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58814130/

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