微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?