2017-10-17 8 views
0

다중 레이블 분류 문제에 대해 주어진 임계 값에서 정확히 일치하는 비율을 표시하는 eval_metric_op 함수를 작성하려고합니다. 다음 함수는 제공된 임계 값을 기준으로 0 (정확히 일치하지 않음) 또는 1 (완전 일치)을 반환합니다.TensorFlow에 대해 "완전 일치"eval_metric_op을 만드는 방법은 무엇입니까?

def exact_match(y_true, y_logits, threshold): 
    y_pred = np.round(y_logits-threshold+0.5) 
    return int(np.array_equal(y_true, y_pred)) 

y_true = np.array([1,1,0]) 
y_logits = np.array([0.67, 0.9, 0.55]) 

print(exact_match(y_true, y_logits, 0.5)) 
print(exact_match(y_true, y_logits, 0.6)) 

0.5의 임계 값의 예측을 수득 [1,1,1] 함수는 0 0.6의 임계 값의 예측을 수득 반환되도록 정확하다 [1,1,0]는 매우 정확하다 함수는 1을 반환합니다.

나는이 함수를 tensorflow eval metric으로 바꾸고 싶습니다. 누구에게나 이것을 할 수있는 가장 좋은 방법을 조언 할 수 있습니까?

나는 아래 tensorflow 작전을 사용하여 동일한 논리로 얻을 수 있지만 사용자 정의 eval_metric_op에이를 수 있도록하는 방법을 완전히 확실하지 않다 : 위의 코드는 0의 exact_match_50가 발생합니다

import tensorflow as tf 

def exact_match_fn(y_true, y_logits, threshold): 
    #pred = tf.equal(tf.round(y_logits), tf.round(y_true)) 
    predictions = tf.to_float(tf.greater_equal(y_logits, threshold)) 
    pred_match = tf.equal(predictions, tf.round(y_true)) 
    exact_match = tf.reduce_min(tf.to_float(pred_match)) 
    return exact_match 

graph = tf.Graph() 
with graph.as_default(): 
    y_true = tf.constant([1,1,0], dtype=tf.float32) 
    y_logits = tf.constant([0.67,0.9,0.55], dtype=tf.float32) 
    exact_match_50 = exact_match_fn(y_true, y_logits, 0.5) 
    exact_match_60 = exact_match_fn(y_true, y_logits, 0.6) 

sess = tf.InteractiveSession(graph=graph) 
print(sess.run([exact_match_50, exact_match_60])) 

(에서 최소 1 예측 부정확) 및 1의 exact_match_60 (모든 라벨이 정확함).

단순히 tf.contrib.metrics.streaming_mean()을 사용하는 것만으로 충분합니까? 아니면 더 좋은 대안이 있습니까?

tf.contrib.metrics.streaming_mean(exact_match(y_true, y_logits, threshold)) 

답변

1

당신의 exact_match_fn의 출력은 평가를 위해 사용할 수있는 연산이다 :이로 구현하는 것입니다. 배치에 대한 평균값을 원할 경우 reduce_min을 변경하여 관련 축을 축소하면됩니다.

예. 당신 y_true/y_logits 각 모양 (batch_size, n)

def exact_match_fn(y_true, y_logits, threshold): 
    #pred = tf.equal(tf.round(y_logits), tf.round(y_true)) 
    predictions = tf.to_float(tf.greater_equal(y_logits, threshold)) 
    pred_match = tf.equal(predictions, tf.round(y_true)) 
    exact_match = tf.reduce_min(tf.to_float(pred_match), axis=1) 
    return exact_match 


def exact_match_prop_fn(*args): 
    return tf.reduce_mean(exact_match_fn(*args)) 

이있는 경우 이렇게하면 일괄 처리의 평균을 줄 것이다. 전체 데이터 세트에 대한 평균을 원한다면 일치 (또는 correcttotal 카운트)를 수집하고 세션/tensorflow 외부에서 평가할 것입니다. 그러나 streaming_mean은 확실하지 않습니다.

+0

예, 축 보정 = 1을 추가하는 것이 정확합니다 - 수정 해 주셔서 감사합니다! 나는 그 부분을 생각한다. 나는 내 실험에서 streaming_mean()이 전체 카운트를 수집하는 것을 처리한다고 생각한다. 그래서 여기에 누락 된 유일한 것이 될 것이다. – reese0106