How do i create Confusion matrix of predicted and ground truth labels with Tensorflow?

前端 未结 3 2059
天命终不由人
天命终不由人 2020-12-15 01:58

I have implemented a Nueral Network model for a classification with the help of using TensorFlow. But, i don\'t know how can i able to draw confusion matrix by using predic

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-15 02:29

    For the moment, I use this solution to obtain the confusion matrix :

    # load the data
    (train_x, train_y), (dev_x, dev_y), (test_x, test_y) = dataLoader.load()
    
    # build the classifier
    classifier = tf.estimator.DNNClassifier(...)
    
    # train the classifier
    classifier.train(input_fn=lambda:train_input_fn(), steps=1000)
    
    # evaluate and prediction on the test set
    test_evaluate = classifier.evaluate(input_fn=lambda:eval_input_fn())
    test_predict = classifier.predict(input_fn = lambda:eval_input_fn())
    
    # parse the prediction to retrieve the predicted labels
    predictions = []
    
    for i in list(test_predict):
        predictions.append(i['class_ids'][0])
    
    # build the prediction matrix
    matrix = tf.confusion_matrix(test_y, predictions)
    
    #display the prediction matrix
    with tf.Session():
        print(str(tf.Tensor.eval(matrix)))
    

    But I am not convince by my loop to retrieve the predicted labels...there should be a better Python way to do this...(or a TensorFlow way...)

提交回复
热议问题