2017-12-22 6 views
1

이 논문은 Tensorflow CR-CNN에서 구현하고 있습니다. 이 논문에서 사용 된 손실 함수는 Tensors와 실제 레이블의 실행 시간 값에 의존하는 용어를 사용합니다. 내가 아는 한 Tensorflow는 정적 계산 그래프를 생성 한 다음 세션에서 실행합니다. 이 논문에서 언급 한 예측 및 손실 함수를 구현하는 것이 어렵다는 것을 알았습니다. 둘 다 런타임에 동적으로 변경되기 때문입니다. 내 코드에서 tf.cond()를 사용했지만 그 결과로 '없음'이 그라디언트로 사용되었습니다. 따라서 내 네트워크는 전혀 훈련을받지 못합니다.Tensorflow의 쌍방향 랭킹 손실 기능

class_scores = tf.matmul(pooled, W_classes) 

n_correct = tf.Variable(0, trainable=True) 

for t in xrange(batch_size): 
    max_arg = tf.cast(tf.argmax(class_scores[t], 1), tf.int32) 
    #true_class = tf.constant(0) 
    true_class = tf.cast(tf.argmax(Y[t], 1), tf.int32) 

    pred_class = tf.Variable(0,trainable=True) 
    value = class_scores[t][max_arg] 
    tf.cond(value <= 0, lambda: tf.assign(pred_class, 0), lambda: tf.assign(pred_class, max_arg + 1)) 
    tf.cond(tf.equal(true_class, pred_class), lambda: tf.add(n_correct, 1), lambda: tf.add(n_correct, 0)) 

    #print(value) 

accuracy = tf.cast(n_correct, tf.float32)/tf.cast(batch_size, tf.float32) 

올바른 예측을 계산하지 않고 정확도를 계산합니다. 뿐만 아니라 손실 함수에 대한

비슷한 접근 방식 :

gamma = tf.constant(2.0) 
m_plus = tf.constant(2.5) 
m_minus = tf.constant(0.5) 

batch_loss = tf.Variable(0.0, trainable=True) 


for t in xrange(batch_size): 
    max_arg = tf.cast(tf.argmax(class_scores[t], 1), tf.int32) 
    true_class = tf.cast(tf.argmax(Y[t], 1), tf.int32) 

    top2_val, top2_i = tf.nn.top_k(class_scores[t], 2, sorted=True) 

    pred_class = tf.Variable(0, trainable=True) 
    true_score = tf.Variable(0.0, trainable=True) 
    neg_score = tf.Variable(0.0, trainable=True) 

    value = class_scores[t][max_arg] 

    tf.cond(value <= 0, lambda: tf.assign(pred_class, 0), lambda: tf.assign(pred_class, max_arg + 1)) 

    tf.cond(tf.equal(true_class, 0), lambda: tf.assign(true_score, 0), lambda: tf.assign(true_score, class_scores[t][true_class-1])) 

    tf.cond(tf.equal(true_class, 0), lambda: tf.assign(neg_score, value), lambda: tf.cond(tf.equal(true_class, pred_class), 
       lambda: tf.assign(neg_score, top2_val[1]), lambda: tf.assign(neg_score, value))) 

    example_loss = tf.Variable(0.0, trainable=True) 

    tf.cond(tf.equal(true_class, 0), lambda: tf.assign(example_loss, tf.log(1 + tf.exp(tf.multiply(gamma, m_minus + neg_score)))), 
        lambda: tf.assign(example_loss, tf.log(1 + tf.exp(tf.multiply(gamma, m_plus - true_score))) + tf.log(1 + tf.exp(tf.multiply(gamma, m_minus + neg_score))))) 
    batch_loss = tf.add(batch_loss, example_loss) 

    #print(neg_score) 

batch_loss = batch_loss/batch_size 
train_step = tf.train.GradientDescentOptimizer(learning_rate=lambda_t).minimize(batch_loss) 

그러나 네트워크가 훈련을 받고 있지 않습니다. 누군가가 tensorflow에서 이것을 수행하는 방법을 제안 할 수 있습니까?

답변

1

이 코드에는 몇 가지 문제가 있으며 작동하지 않는 것이 있습니다. 여기에 존재하지 않는 개념적 문제 (tf.cond 또는 tf.Variable이 문제를 해결하는 데 필요하지 않음)와 같이 tensorflow eager execution을 사용해 보시기 바랍니다.

이 코드 예제에서 tf.cond를 사용하는 방법의 문제점은 tf.cond가 본질적으로 기능한다는 것입니다 (tf.cond의 반환 값을 사용할 때만 실행되는 그래프에 ops가 추가됨). 따라서 코드를 tf.cond와 연계시켜 (아마도 tf.control_dependencies를 통해) 실행해야합니다.

그러나 훈련 예에서는 tf.Variables도 사용합니다. Tensorflow는 tf.Variable에 배정을 역행 할 수 없으므로 대신 tf.assign 및 친구들에 대한 호출을 변수의 새 값을 반환하고 Python에서 사용하도록 바꾸어야합니다.