小编Ily*_*lya的帖子

Keras中的自定义损失函数,如何处理占位符

我正在尝试在 TF/Keras 中生成自定义损失函数,如果它在会话中运行并传递常量,则损失函数会起作用,但是,它在编译成 Keras 时停止工作。

成本函数(感谢 Lior 将其转换为 TF)

def ginicTF(actual,pred):

    n = int(actual.get_shape()[-1])

    inds =  K.reverse(tf.nn.top_k(pred,n)[1],axes=[0]) 
    a_s = K.gather(actual,inds) 
    a_c = K.cumsum(a_s)
    giniSum = K.sum(a_c)/K.sum(a_s) - (n+1)/2.0

    return giniSum / n

def gini_normalizedTF(a,p):
    return -ginicTF(a, p) / ginicTF(a, a)

#Test the cost function

sess = tf.InteractiveSession()

p = [0.9, 0.3, 0.8, 0.75, 0.65, 0.6, 0.78, 0.7, 0.05, 0.4, 0.4, 0.05, 0.5, 0.1, 0.1]
a = [1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, …
Run Code Online (Sandbox Code Playgroud)

python-3.x keras gini tensorflow loss-function

5
推荐指数
0
解决办法
1086
查看次数

标签 统计

gini ×1

keras ×1

loss-function ×1

python-3.x ×1

tensorflow ×1