tf.estimator.BoostedTreesEstimator 在使用涉及 tf.math.abs

问题描述

我提交了一个与此问题相同的 bug report,但我也发布了此问题,以防问题实际上是根本性的并且我误解了 Boosted Trees。

描述当前行为 在创建带有自定义头部的 BoostedTreesEstimator 以使用各种损失函数时,如果损失函数涉及 tf.math.abs(我只尝试过 tf.math.abs 和 tf.math.pow、tf. math.pow 按预期工作)。

描述预期行为 非零预测。请参阅示例并使用 custom_loss_mse 而不是 custom_loss_mae。在 [0,1] 中的统一数据示例中,mse 和 mae 都应返回接近 0.5 的预测。

重现问题的独立代码 Colab gist

import tensorflow as tf
import numpy as np

LABEL_DIM = 1
INPUT_Dims = [1]

features = [
    tf.feature_column.numeric_column(f"random_input_{i}",shape = (sh,),dtype=tf.float32
                                    ) 
    for i,sh in enumerate(INPUT_Dims)
]

def input_fn(num_samples = 1000):
    inputs = {f.key : np.random.rand(num_samples,s) for f,s in zip(features,INPUT_Dims)}
    targets = np.random.rand(num_samples,LABEL_DIM)
    ds = tf.data.Dataset.from_tensor_slices((inputs,targets))
    ds = ds.shuffle(num_samples).repeat(10).batch(10) 
    return ds

def less_input_fn():
    return input_fn(10)

def custom_loss_mse(labels,logits):
    return tf.math.pow(labels - logits,2)

def custom_loss_mae(labels,logits):
    return tf.math.abs(labels - logits)

est = tf.estimator.BoostedTreesEstimator(
    feature_columns = features,head = tf.estimator.RegressionHead(
        label_dimension = LABEL_DIM,#         loss_fn = custom_loss_mse,loss_fn = custom_loss_mae,n_batches_per_layer = 1,center_bias = True,)

est.train(input_fn)

list(est.predict(less_input_fn))

解决方法

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

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

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