def evaluate(data, model, name, nbest=None, print_tag_counts=False, tag_to_ablate=None):
    "\n\n    :param data:\n    :param model:\n    :param name:\n    :param nbest:\n    :param print_tag_counts:\n    :param tag_to_ablate: if this is set to a tag name, like 'B-ORG', then in the LSTM layer's forward() we ablate the\n    number of neurons specified by data.ablate_num\n    :return:\n    "
    ablate_list_for_tag = None
    if tag_to_ablate:
        data.ablate_tag = tag_to_ablate
        ablate_list_for_tag = data.ablate_list[tag_to_ablate]
    print('\nEVALUATE file: {}, set={}, \n\t ablate_num={} tag: {} \nablate_list_for_tag={}'.format(data.model_dir, name, data.current_ablate_ind, tag_to_ablate, ablate_list_for_tag))
    if (name == 'train'):
        instances = data.train_Ids
    elif (name == 'dev'):
        instances = data.dev_Ids
    elif (name == 'test'):
        instances = data.test_Ids
    elif (name == 'raw'):
        instances = data.raw_Ids
    else:
        print('Error: wrong evaluate name,', name)
        exit(1)
    right_token = 0
    whole_token = 0
    nbest_pred_results = []
    pred_scores = []
    pred_results = []
    gold_results = []
    model.eval()
    ' Get count of model parameters '
    batch_size = data.HP_batch_size
    start_time = time.time()
    train_num = len(instances)
    total_batch = ((train_num // batch_size) + 1)
    for batch_id in range(total_batch):
        start = (batch_id * batch_size)
        end = ((batch_id + 1) * batch_size)
        if (end > train_num):
            end = train_num
        instance = instances[start:end]
        if (not instance):
            continue
        (batch_word, batch_features, batch_wordlen, batch_wordrecover, batch_char, batch_charlen, batch_charrecover, batch_label, mask) = batchify_with_label(instance, data.HP_gpu, False, data.sentence_classification)
        if (nbest and (not data.sentence_classification)):
            (scores, nbest_tag_seq) = model.decode_nbest(batch_word, batch_features, batch_wordlen, batch_char, batch_charlen, batch_charrecover, mask, nbest)
            nbest_pred_result = recover_nbest_label(nbest_tag_seq, mask, data.label_alphabet, batch_wordrecover)
            nbest_pred_results += nbest_pred_result
            pred_scores += scores[batch_wordrecover].cpu().data.numpy().tolist()
            tag_seq = nbest_tag_seq[:, :, 0]
        else:
            tag_seq = model(batch_word, batch_features, batch_wordlen, batch_char, batch_charlen, batch_charrecover, mask)
        (pred_label, gold_label) = recover_label(tag_seq, batch_label, mask, data.label_alphabet, batch_wordrecover, data.sentence_classification)
        pred_results += pred_label
        gold_results += gold_label
    decode_time = (time.time() - start_time)
    speed = (len(instances) / decode_time)
    (acc, p, r, f) = get_ner_fmeasure(gold_results, pred_results, data.tagScheme, data=data)
    if (nbest and (not data.sentence_classification)):
        return (speed, acc, p, r, f, nbest_pred_results, pred_scores)
    ' Get per-tag sensitivity '
    sensitivity_matrices = []
    for tag in sorted(data.tag_counts):
        if print_tag_counts:
            if (tag == 0):
                print('Padding {}: {} instances.'.format('0', data.tag_counts[tag]))
            else:
                print('Tag {}: {} instances.'.format(data.label_alphabet.get_instance(tag), data.tag_counts[tag]))
        sensitivity_tag = get_sensitivity_matrix(tag)
        sensitivity_matrices.append(sensitivity_tag)
    sensitivity_combined = np.squeeze(np.stack([sensitivity_matrices]))
    return (speed, acc, p, r, f, pred_results, pred_scores, sensitivity_combined)
:param data:
:param model:
:param name:
:param nbest:
:param print_tag_counts:
:param tag_to_ablate: if this is set to a tag name, like 'B-ORG', then in the LSTM layer's forward() we ablate the
number of neurons specified by data.ablate_num
:return:
def evaluate(data, model, name, nbest=None, print_tag_counts=False, tag_to_ablate=None):
    "\n\n    :param data:\n    :param model:\n    :param name:\n    :param nbest:\n    :param print_tag_counts:\n    :param tag_to_ablate: if this is set to a tag name, like 'B-ORG', then in the LSTM layer's forward() we ablate the\n    number of neurons specified by data.ablate_num\n    :return:\n    "
    ablate_list_for_tag = None
    if tag_to_ablate:
        data.ablate_tag = tag_to_ablate
        ablate_list_for_tag = data.ablate_list[tag_to_ablate]
    print('\nEVALUATE file: {}, set={}, \n\t ablate_num={} tag: {} \nablate_list_for_tag={}'.format(data.model_dir, name, data.current_ablate_ind, tag_to_ablate, ablate_list_for_tag))
    if (name == 'train'):
        instances = data.train_Ids
    elif (name == 'dev'):
        instances = data.dev_Ids
    elif (name == 'test'):
        instances = data.test_Ids
    elif (name == 'raw'):
        instances = data.raw_Ids
    else:
        print('Error: wrong evaluate name,', name)
        exit(1)
    right_token = 0
    whole_token = 0
    nbest_pred_results = []
    pred_scores = []
    pred_results = []
    gold_results = []
    model.eval()
    ' Get count of model parameters '
    batch_size = data.HP_batch_size
    start_time = time.time()
    train_num = len(instances)
    total_batch = ((train_num // batch_size) + 1)
    for batch_id in range(total_batch):
        start = (batch_id * batch_size)
        end = ((batch_id + 1) * batch_size)
        if (end > train_num):
            end = train_num
        instance = instances[start:end]
        if (not instance):
            continue
        (batch_word, batch_features, batch_wordlen, batch_wordrecover, batch_char, batch_charlen, batch_charrecover, batch_label, mask) = batchify_with_label(instance, data.HP_gpu, False, data.sentence_classification)
        if (nbest and (not data.sentence_classification)):
            (scores, nbest_tag_seq) = model.decode_nbest(batch_word, batch_features, batch_wordlen, batch_char, batch_charlen, batch_charrecover, mask, nbest)
            nbest_pred_result = recover_nbest_label(nbest_tag_seq, mask, data.label_alphabet, batch_wordrecover)
            nbest_pred_results += nbest_pred_result
            pred_scores += scores[batch_wordrecover].cpu().data.numpy().tolist()
            tag_seq = nbest_tag_seq[:, :, 0]
        else:
            tag_seq = model(batch_word, batch_features, batch_wordlen, batch_char, batch_charlen, batch_charrecover, mask)
        (pred_label, gold_label) = recover_label(tag_seq, batch_label, mask, data.label_alphabet, batch_wordrecover, data.sentence_classification)
        pred_results += pred_label
        gold_results += gold_label
    decode_time = (time.time() - start_time)
    speed = (len(instances) / decode_time)
    (acc, p, r, f) = get_ner_fmeasure(gold_results, pred_results, data.tagScheme, data=data)
    if (nbest and (not data.sentence_classification)):
        return (speed, acc, p, r, f, nbest_pred_results, pred_scores)
    ' Get per-tag sensitivity '
    sensitivity_matrices = []
    for tag in sorted(data.tag_counts):
        if print_tag_counts:
            if (tag == 0):
                print('Padding {}: {} instances.'.format('0', data.tag_counts[tag]))
            else:
                print('Tag {}: {} instances.'.format(data.label_alphabet.get_instance(tag), data.tag_counts[tag]))
        sensitivity_tag = get_sensitivity_matrix(tag)
        sensitivity_matrices.append(sensitivity_tag)
    sensitivity_combined = np.squeeze(np.stack([sensitivity_matrices]))
    return (speed, acc, p, r, f, pred_results, pred_scores, sensitivity_combined)