我正在开发一个自定义的损失函数,它在内部使用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)
在TensorFlow中,
np.digitize
方法的等价物称为bucketize
,引自{a1}:我不知道为什么,但是,这个方法隐藏在TensorFlow中(参见hidden_ops.txt file)。因此,即使您可以通过以下操作导入,我也不会指望它:
相关问题 更多 >
编程相关推荐