def acc_hook(scores, y_data, other_id):
    true_and_prediction = []
    corr = 0.
    total = 0.
    for score, true_label in zip(scores, y_data):
        predicted = []
        true_tag = []
        for i in range(len(score)):
            t1 = true_label[i]
            if t1 != 0:
                true_tag.append(i)
            t2 = np.argmax(score[i])
            if t2 != 0:
                predicted.append(i)
            if t1 != 0:
                total += 1
                t2 = np.argmax(score[i][1:]) + 1
                if t1 == t2:
                    corr += 1.0
        if predicted == []: predicted = [other_id]
        if true_tag == []: true_tag = [other_id]
        true_and_prediction.append((true_tag, predicted))
    stct = strict(true_and_prediction)
    macro = loose_macro(true_and_prediction)
    micro = loose_micro(true_and_prediction)
    report = []
    if total != 0:
        report.append("Accuracy:%f %d" % (corr / total, total))
    report.append("     strict (p,r,f1):%f %f %f:" % stct)
    report.append("loose macro (p,r,f1):%f %f %f" % macro)
    report.append("loose micro (p,r,f1):%f %f %f" % micro)
    report.append("%f\t%f\t%f" % (stct[-1], macro[-1], micro[-1]))
    return stct[2], report, [t[1] for t in true_and_prediction]
Exemple #2
0
def acc_hook(scores,
             y_data,
             gaussian=False,
             delta=0,
             topK=1,
             use_path=False,
             label_hierarchy=None):
    true_and_prediction = get_true_and_prediction(scores, y_data, gaussian,
                                                  delta, topK, use_path,
                                                  label_hierarchy)
    print("     strict (p,r,f1):", strict(true_and_prediction))
    print("loose macro (p,r,f1):", loose_macro(true_and_prediction))
    print("loose micro (p,r,f1):", loose_micro(true_and_prediction))
Exemple #3
0
def acc_hook(scores, y_data):
    true_and_prediction = []
    for score, true_label in zip(scores, y_data):
        predicted_tag = []
        true_tag = []
        for label_id, label_score in enumerate(list(true_label)):
            if label_score > 0:
                true_tag.append(label_id)
        lid, ls = max(enumerate(list(score)), key=lambda x: x[1])
        predicted_tag.append(lid)
        for label_id, label_score in enumerate(list(score)):
            if label_score > 0.5:
                if label_id != lid:
                    predicted_tag.append(label_id)
        true_and_prediction.append((true_tag, predicted_tag))

    print("     strict (p,r,f1):", strict(true_and_prediction))
    print("loose macro (p,r,f1):", loose_macro(true_and_prediction))
    print("loose micro (p,r,f1):", loose_micro(true_and_prediction))
Exemple #4
0
def acc_hook(scores,Y,th=3000):
    true_and_prediction = []
    num_of_samples = Y.shape[0]
    
    for score,true_label in zip(scores,Y):
        predicted_tag = []                                                                                                                                                                                 
        true_tag = []
        for label_id,label_score in enumerate(list(true_label)):
            if label_score > 0:
                true_tag.append(label_id)
        lid,ls = max(enumerate(list(score)),key=lambda x: x[1])
        predicted_tag.append(lid)
        for label_id,label_score in enumerate(list(score)):
            if label_score > th:
                if label_id != lid:
                    predicted_tag.append(label_id)
        true_and_prediction.append((true_tag, predicted_tag))
    
    print("     strict (p,r,f1):",strict(true_and_prediction))
    print("loose macro (p,r,f1):",loose_macro(true_and_prediction))
    print("loose micro (p,r,f1):",loose_micro(true_and_prediction))
Exemple #5
0
def acc_hook(batcher,output_model,x_context,x_target,feature,y,keep_prob_context,keep_prob_target,context_length,sess):
    true_and_prediction = []
    [x_context_data, x_target_mean_data, y_data,feature_data] = batcher.next()
    num_of_samples = y_data.shape[0]
    feed = {y:y_data,keep_prob_context:[1.],keep_prob_target:[1.],feature:feature_data}
    for i in range(context_length*2+1):
        feed[x_context[i]] = x_context_data[:,i,:]
    feed[x_target] = x_target_mean_data

    scores = sess.run(output_model, feed_dict = feed)
    """
    for score,true_label in zip(scores,y_data):
        predicted_tag = []
        true_tag = []
        for label_id,label_score in enumerate(list(true_label)):
            if label_score > 0:
                true_tag.append(label_id)
        for label_id,label_score in enumerate(list(score)):
            if label_score > 0.5:
                predicted_tag.append(label_id)
        true_and_prediction.append((true_tag, predicted_tag))
    """    
    for score,true_label in zip(scores,y_data):
        predicted_tag = []                                                                                                                                                                                 
        true_tag = []
        for label_id,label_score in enumerate(list(true_label)):
            if label_score > 0:
                true_tag.append(label_id)
        lid,ls = max(enumerate(list(score)),key=lambda x: x[1])
        predicted_tag.append(lid)
        for label_id,label_score in enumerate(list(score)):
            if label_score > 0.5:
                if label_id != lid:
                    predicted_tag.append(label_id)
        true_and_prediction.append((true_tag, predicted_tag))
    
    print("     strict (p,r,f1):",strict(true_and_prediction))
    print("loose macro (p,r,f1):",loose_macro(true_and_prediction))
    print("loose micro (p,r,f1):",loose_micro(true_and_prediction))
Exemple #6
0
            is_leaf=is_only_leaf(gt_labels[0])
            if is_leaf != 0:
                continue
        predict_quant_embd,id_list = model.forward(context.to(device), lens.to(device),ids,left_right)
        predict_quant_embd=predict_quant_embd.data.cpu().numpy()
        mask_matrix=np.multiply(quantam_concept_embds, predict_quant_embd)
        normalizing_constant=np.sum(np.abs(predict_quant_embd)**2,axis=-1)
        score=(np.sum(np.abs(mask_matrix) ** 2, axis=-1))/normalizing_constant

        predicted_labels_id,best_level_1_node,best_level_1_score,best_level_2_node,best_level_2_score=predict_labels(score,threshold_level_wise,constant_to_divide)
        predicted_labels=[total_node_list[i] for i in predicted_labels_id]
        temp_dict = {}
        temp_dict['gt_label'] = gt_labels[0]
        temp_dict['predicted_label'] = predicted_labels
        temp_dict['best_level_1_node']=best_level_1_node
        temp_dict['best_level_2_node'] = best_level_2_node
        temp_dict['best_level_1_score']=best_level_1_score
        temp_dict['best_level_2_score']=best_level_2_score


        total_gt_label_len.append(len(gt_labels[0]))
        total_predicted_label_len.append(len(predicted_labels))


        p,r,f=evaluate.loose_macro([(gt_labels_id, predicted_labels_id)])
        p1,r1,f1=evaluate.strict([(gt_labels_id, predicted_labels_id)])
        true_and_prediction.append((gt_labels_id, predicted_labels_id))

print("strict (p,r,f1):",evaluate.strict(true_and_prediction))
print("loose micro (p,r,f1):",evaluate.loose_micro(true_and_prediction))
print("loose macro (p,r,f1):",evaluate.loose_macro(true_and_prediction))
Exemple #7
0
def acc_hook(scores, y_data):
    true_and_prediction = get_true_and_prediction(scores, y_data)
    print("     strict (p,r,f1):",strict(true_and_prediction))
    print("loose macro (p,r,f1):",loose_macro(true_and_prediction))
    print("loose micro (p,r,f1):",loose_micro(true_and_prediction))