(sess, model)
return total_loss / data_nums, f1_score, confuse_matrix, y_pred, y_target
评估 val data 的准确率和损失
评估 val data 的准确率和损失
评估 val data 的准确率和损失
def evaluate(sess, model):
    """
    评估 val data 的准确率和损失
    """

    # dev data
    test_record = tf.data.TFRecordDataset("./model/bert2/eval.tf_record")
    test_data = read_data(test_record, train_batch_size, False, 3)
    test_iterator = test_data.make_one_shot_iterator()
    test_batch = test_iterator.get_next()

    data_nums = 0
    total_loss = 0.0
    y_pred = []
    y_target = []
    while True:
        try:
            features = sess.run(test_batch)
            feed_dict = {model.input_ids: features["input_ids"],
                         model.input_mask: features["input_mask"],
                         model.segment_ids: features["segment_ids"],
                         model.labels: features["label_ids"],
                         model.is_training: False,
                         model.learning_rate: learning_rate}

            batch_len = len(features["input_ids"])
            data_nums += batch_len
            # print(data_nums)
            loss, logits = sess.run([model.loss, model.logits], feed_dict=feed_dict)
            total_loss += loss * batch_len
            y_batch_pred = get_logits_label(logits)
            y_batch_target = get_target_label(features["label_ids"])
            y_pred.extend(y_batch_pred)
            y_target.extend(y_batch_target)
        except Exception as e:
            break

    confuse_matrix = compute_confuse_matrix(y_target, y_pred)
    f1_micro, f1_macro = compute_micro_macro(confuse_matrix)
    print(f1_micro, f1_macro)
    f1_score = (f1_micro + f1_macro) / 2.0

    return total_loss / data_nums, f1_score, confuse_matrix, y_pred, y_target
def evaluate ( sess , model ) : # dev data test_record = tf . data . TFRecordDataset ( "./model/bert2/eval.tf_record" ) test_data = read_data ( test_record , train_batch_size , False , 3 ) test_iterator = test_data . make_one_shot_iterator ( ) test_batch = test_iterator . get_next ( ) data_nums = 0 total_loss = 0.0 y_pred = [ ] y_target = [ ] while True : try : features = sess . run ( test_batch ) feed_dict = { model . input_ids : features [ "input_ids" ] , model . input_mask : features [ "input_mask" ] , model . segment_ids : features [ "segment_ids" ] , model . labels : features [ "label_ids" ] , model . is_training : False , model . learning_rate : learning_rate } batch_len = len ( features [ "input_ids" ] ) data_nums += batch_len # print(data_nums) loss , logits = sess . run ( [ model . loss , model . logits ] , feed_dict = feed_dict ) total_loss += loss * batch_len y_batch_pred = get_logits_label ( logits ) y_batch_target = get_target_label ( features [ "label_ids" ] ) y_pred . extend ( y_batch_pred ) y_target . extend ( y_batch_target ) except Exception as e : break confuse_matrix = compute_confuse_matrix ( y_target , y_pred ) f1_micro , f1_macro = compute_micro_macro ( confuse_matrix ) print ( f1_micro , f1_macro ) f1_score = ( f1_micro + f1_macro ) / 2.0 return total_loss / data_nums , f1_score , confuse_matrix , y_pred , y_target