Python 计算张量流损失函数中的多指标和

Python 计算张量流损失函数中的多指标和,python,tensorflow,neural-network,keras,Python,Tensorflow,Neural Network,Keras,我正在用一个损失函数来优化神经网络,这个损失函数相当于c指数。我想使用的损失函数是链接中的latex方程 其中,y'是预测向量,y是一批大小为N的标签向量,\ sigma是sigmoid函数。我希望能够在TensorFlow中实现这一点,但我找不到一种表达两个索引和的方法 我尝试过将方程重新排列成不同的形式,可以用TensorFlow和Keras原语表示,但没有成功。我使用的是Keras,所以Keras或TensorFlow实现都是可用的 Python代码是 from itertools imp

我正在用一个损失函数来优化神经网络,这个损失函数相当于c指数。我想使用的损失函数是链接中的latex方程

其中,y'是预测向量,y是一批大小为N的标签向量,\ sigma是sigmoid函数。我希望能够在TensorFlow中实现这一点,但我找不到一种表达两个索引和的方法

我尝试过将方程重新排列成不同的形式,可以用TensorFlow和Keras原语表示,但没有成功。我使用的是Keras,所以Keras或TensorFlow实现都是可用的

Python代码是

from itertools import permutations, combinations
a = np.arange(4)
a = a*100

def loss_ci(y_true, y_pred):
summ = 0.
total=0
for i in range(len(y_true)):
    for j in range(i+1,len(y_true)):
        summ += 1/(1+np.exp(-(y_true[i]-y_true[j]) * (y_pred[i]-y_pred[j])))
        total+=1
return (summ)/total

print("y_true\t\ty_pred\t\tc-index\tloss")
for c in permutations(a,3):
for d in combinations(a,3):
    print(c, d, "\t{:.4f}".format(ci(c, d)), "\t{:.4f}".format(loss_ci(c, d)))

可使用张量流计算损失,如下代码所示:

from itertools import permutations, combinations
a = np.arange(4)
a = a*100

def loss_ci(y_true, y_pred):
summ = 0.
total=0
for i in range(len(y_true)):
    for j in range(i+1,len(y_true)):
        summ += 1/(1+np.exp(-(y_true[i]-y_true[j]) * (y_pred[i]-y_pred[j])))


return (summ)

def tf_loss_ci(y_true, y_pred):
  Y = tf.constant(y_true)
  _Y = tf.constant(y_pred)
  S = tf.sigmoid(tf.multiply((Y[tf.newaxis,:]-Y[:,tf.newaxis]),(_Y[tf.newaxis,:]-_Y[:,tf.newaxis])))
  S = tf.reduce_sum(tf.matrix_set_diag(S,tf.zeros_like(Y))) / 2
  sess = tf.InteractiveSession()
  tf.global_variables_initializer().run()
  return S.eval()

print("y_true\t\ty_pred\t\ttensorloss\tloss")
for c in permutations(a,3):
  for d in combinations(a,3):
    print(c, d, "\t{:.4f}".format(tf_loss_ci(np.asarray(c, np.float32), np.array(d, np.float32))), "\t{:.4f}".format(loss_ci(c, d)))

谢谢,但这不能实现乙状结肠功能。我已经编辑了这个问题,将损失函数的numpy版本包括在内。除以总数是没有必要的,但它显示了损失是如何等同于c指数的。。做出了改变