일치 지수 (c-index) 와 동일한 손실 함수를 사용하여 신경망을 최적화하는 실험을하고 있습니다 . 내가 사용하고 싶은 손실 함수는 (링크의 라텍스 방정식)
∑ {i = 0} ^ N ∑ {j = i} ^ N \ 시그마 ((y_i-y_j) (y'_i-y'_j))
여기서 y '는 예측 벡터이고 y는 배치 크기 N의 레이블 벡터이며 \ sigma는 시그 모이 드 함수입니다. 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)))
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다