P/N:表示模型预测的结果积极或消极。
T/F:表示模型预测的结果正确或错误。
所以
TP:表示模型预测为positive是准确的
TN:表示模型预测为negtive是正确的
FP:表示模型预测为positive是错误的
FN:表示模型预测为negtive是错误的
准确率(accuracy)=(TP + TN)/(TP+TN+FP+FN),表示模型预测准确的概率
查准率-精准率(Precision) = TP/(TP+FP),表示预测为positve的结果中预测正确所占的比例
查全率-召回率(recall)=TP/(TP+FN),表示预测postive的结果占应该预测正确的数量的比例
关于对应的代码实现:
import configparser
import tensorflow as tf
from utils.path_utils import get_full_path
from utils.read_batch_data import get_data_by_batch_size
class ModelTest:
def __init__(self,
batch_size=500,
sentence_vocb_length=15,
model_path ="",
fill_vocab='TTTTTT'
):
self.fill_vocab = fill_vocab
self.sentence_vocb_length=sentence_vocb_length
self.batch_size =batch_size
self.model = tf.keras.models.load_model(model_path)
self.fasenegtives = tf.keras.metrics.FalseNegatives()
self.trueNegatives = tf.keras.metrics.TrueNegatives()
self.falsePositives =tf.keras.metrics.FalsePositives()
self.truePositives =tf.keras.metrics.TruePositives()
# 返回结果的顺序性指标
def test(self):
# ========================== Create dataset =======================
for x, y in get_data_by_batch_size("data/train_data/validate_data.txt", self.batch_size,
self.sentence_vocb_length, self.fill_vocab):
test_x = x
test_y = y
y_predict_test = self.model(test_x)
result1 = tf.reshape(tf.cast(y_predict_test>0.6,dtype=tf.int32),shape=(len(test_y),))
result2 = tf.reshape(tf.cast(test_y,dtype=tf.int32),shape=(len(test_y),))
self.fasenegtives.update_state(result2,result1)
self.trueNegatives.update_state(result2,result1)
self.falsePositives.update_state(result2,result1)
self.truePositives.update_state(result2,result1)
if __name__ == '__main__':
# =============================== GPU ==============================
gpu = tf.config.experimental.list_physical_devices(device_type='GPU')
print("gpu message:{}".format(gpu))
# If you have GPU, and the value is GPU serial number.
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
config = configparser.ConfigParser()
config.read(get_full_path('conf/config.txt'), encoding="utf-8")
batch_size = 1000
sentence_vocb_length = config.getint("lstm_model", "sentence_vocab_lenth")
train_instance = ModelTest(model_path=get_full_path(config.get("lstm_model","model_path")),batch_size=batch_size,sentence_vocb_length = sentence_vocb_length)
train_instance.test()
print("fasenegtives",train_instance.fasenegtives.result().numpy())
print("trueNegatives",train_instance.trueNegatives.result().numpy())
print("falsePositives",train_instance.falsePositives.result().numpy())
print("truePositives",train_instance.truePositives.result().numpy())
print("准确率(accuracy):",(train_instance.truePositives.result().numpy() + train_instance.trueNegatives.result().numpy())/
(train_instance.truePositives.result().numpy() + train_instance.trueNegatives.result().numpy() +
train_instance.fasenegtives.result().numpy()+
train_instance.falsePositives.result().numpy()))
print("查准率-精准率(precision):",train_instance.truePositives.result().numpy()/(train_instance.truePositives.result().numpy()+
train_instance.falsePositives.result().numpy()))
print("查全率-召回率(recall):",train_instance.truePositives.result().numpy()/(train_instance.truePositives.result().numpy()+
train_instance.fasenegtives.result().numpy()))
|