相当于数字化以十为单位

2024-06-12 08:29:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在开发一个自定义的损失函数,它在内部使用numpy.digitize()。对于一组参数,即数字化方法中使用的bins值,损失最小。为了使用tensorflow优化程序,我想知道在tensorflow中是否有digitize的等效实现?如果没有,是否有一个好的方法来实施一个变通办法?在

这里有一个新版本:

def fom_func(b, n):
    np.where((b > 0) & (n > 0), np.sqrt(2*(n*np.log(np.divide(n,b)) + b - n)),0) 
def loss(param, X, y):
    param = np.sort(np.asarray(param))
    nbins = param.shape[0]
    score = 0
    y_pred = np.digitize(X, param)
    for c in np.arange(nbins):
        b = np.where((y==0) & (y_pred==c), 1, 0).sum()
        n = np.where((y_pred==c), 1, 0).sum()
        score += fom_func(b,n)**2
    return -np.sqrt(score)

Tags: 方法paramtensorflowdefnpsqrtwherefunc
1条回答
网友
1楼 · 发布于 2024-06-12 08:29:29

在TensorFlow中,np.digitize方法的等价物称为bucketize,引自{a1}:

Bucketizes 'input' based on 'boundaries'.

Summary

For example, if the inputs are boundaries = [0, 10, 100] input = [[-5, 10000] [150, 10] [5, 100]]

then the output will be output = [[0, 3] [3, 2] [1, 3]]

Arguments:

scope: A Scope object input: Any shape of Tensor contains with int or float type. boundaries: A sorted list of floats gives the boundary of the buckets. Returns:

Output: Same shape with 'input', each value of input replaced with bucket index.
(numpy) Equivalent to np.digitize.

我不知道为什么,但是,这个方法隐藏在TensorFlow中(参见hidden_ops.txt file)。因此,即使您可以通过以下操作导入,我也不会指望它:

from tensorflow.python.ops import math_ops
math_ops._bucketize

相关问题 更多 >