使用自定义指标获取 keras 序列模型中 sample_weight 的当前纪元数

问题描述

我正在使用 Keras、Tensorflow 2 顺序模型。我想计算一个 Revenue@k 作为我的指标。所以我写了一个自定义指标如下。我需要附加参数 _sample_weight 来给出产品的价格。

def revenue_1(_sample_weight):
    def revenue(y_true,y_pred):
        m = keras_metrics.Precision(top_k=1)
        # select the batch size of the sample weight (price)
        m.update_state(y_true,y_pred,sample_weight=_sample_weight)
        return m.result().numpy()
return revenue

调用这个函数如下:

self.model.compile(loss=self.loss,optimizer=self.optimizer_method,run_eagerly=True,metrics[custom_metrics.revenue_1(self.input_data['train_price'],self.model)])

问题是y_true和y_pred形状是batch的大小,而sample_weight是完整的数据大小。我不知道是否可以访问内部数据以使用当前批次对 sample_weight 进行切片。我该怎么做?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)