我正在尝试运行一个接受样本权重的自定义函数。我正在遵循这份文档https://www.tensorflow.org/api_docs/python/tf/keras/losses/Loss。
然而,当我尝试使用以下损失函数时:
class deltaE(Loss):
def __call__(self, y_true, y_pred, sample_weight):
errors = tf_get_deltaE2000(y_true * tf_Xtrain_labels_max, y_pred * tf_Xtrain_labels_max)
errors *= sample_weight
return tf.math.reduce_mean(errors, axis=-1)
loss_deltaE = deltaE()
我在使用 Model.fit
方法时遇到了这个错误。
TypeError: in user code:
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:571 train_function *
outputs = self.distribute_strategy.run(
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:951 run **
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2290 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2649 _call_for_each_replica
return fn(*args, **kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:543 train_step **
self.compiled_metrics.update_state(y, y_pred, sample_weight)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/compile_utils.py:411 update_state
metric_obj.update_state(y_t, y_p)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/utils/metrics_utils.py:90 decorated
update_op = update_state_fn(*args, **kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/metrics.py:603 update_state
matches = self._fn(y_true, y_pred, **self._fn_kwargs)
TypeError: __call__() missing 1 required positional argument: 'sample_weight'
我正在使用一个生成器,它生成长度为 3 的元组,正如所需。我已经检查过了。这个工作很顺利。
成本函数也很好用。当我使用下面的代码时,模型会顺利地训练。
def loss_deltaE(y_true, y_pred):
errors = tf_get_deltaE2000(y_true * tf_Xtrain_labels_max, y_pred * tf_Xtrain_labels_max)
return tf.math.reduce_mean(errors, axis=-1)
如果有人有什么线索,我会非常感激。提前致谢!