张量流中的numpy.digitize的等效项 [英] Equivalent of numpy.digitize in tensorflow
问题描述
我正在研究内部使用numpy.digitize()
的自定义损失函数.对于数字化方法中使用的bins
值的一组参数,损耗被最小化.为了使用tensorflow
优化器,我想知道tensorflow
中是否存在digitize
的等效实现?如果没有,有没有一种好的方法来实施变通办法?
I am working on a customised loss function that uses numpy.digitize()
internally. The loss is minimised for a set of parameters that are the bins
values used in digitize method. In order to use the tensorflow
optimisers, I would like to know if there an equivalent implementation of digitize
in tensorflow
? if not is there a good way to implement a workaround?
这里是一个数字版本:
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)
推荐答案
与np.digitize
方法等效的方法在TensorFlow中称为bucketize
,引用了
The equivalent of np.digitize
method is called bucketize
in TensorFlow, quoting from this api doc:
根据边界"将输入"括起来.
Bucketizes 'input' based on 'boundaries'.
摘要
例如,如果输入为边界= [0,10,100]输入= [[-5,10000] [150,10] [5,100]]
For example, if the inputs are boundaries = [0, 10, 100] input = [[-5, 10000] [150, 10] [5, 100]]
然后输出将输出为[[0,3] [3,2] [1,3]]
then the output will be output = [[0, 3] [3, 2] [1, 3]]
参数:
scope:一个Scope对象 输入:任何具有Tensor形状的int或float类型的Tensor. 边界:浮点数的排序列表提供了铲斗的边界. 返回:
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:
输出:与输入"形状相同,每个输入值替换为存储区索引.
(numpy)等同于np.digitize.
Output: Same shape with 'input', each value of input replaced with bucket index.
(numpy) Equivalent to np.digitize.
我不确定为什么,但是此方法隐藏在TensorFlow中(请参见
I'm not sure why but, this method is hidden in TensorFlow (see the hidden_ops.txt file). So I wouldn't count on it even if you can import it by doing:
from tensorflow.python.ops import math_ops
math_ops._bucketize
这篇关于张量流中的numpy.digitize的等效项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!