如何解决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 举报,一经查实,本站将立刻删除。