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

    # dev data
    test_record = tf.data.TFRecordDataset("./model/bert/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
    total_acc = 0.0
    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, acc = sess.run([model.loss, model.acc], feed_dict=feed_dict)
            total_loss += loss * batch_len
            total_acc += acc * batch_len
        except Exception as e:
            print(e)
            break

    return total_loss / data_nums, total_acc / data_nums
def evaluate ( sess , model ) : # dev data test_record = tf . data . TFRecordDataset ( "./model/bert/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 total_acc = 0.0 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 , acc = sess . run ( [ model . loss , model . acc ] , feed_dict = feed_dict ) total_loss += loss * batch_len total_acc += acc * batch_len except Exception as e : print ( e ) break return total_loss / data_nums , total_acc / data_nums