Tensorflow,预测值的概率?

时间:2017-05-19 22:11:57

标签: python tensorflow

我能获得预测值的概率吗?

我可以获得数据的准确性,但想以某种方式获取每个预测值的概率。

我的代码:

    training_data = np.vstack(training_data)
    training_target = np.vstack(training_target)
    test_data = np.vstack(test_data)
    test_target = np.vstack(test_target)

    feature_columns = tf.contrib.learn.infer_real_valued_columns_from_input(training_data)
    classifier = tf.contrib.learn.DNNClassifier(
        feature_columns=feature_columns,
        hidden_units=[10, 20, 10],
        n_classes=3,
        model_dir="/tmp/basic09",
        optimizer=tf.train.ProximalAdagradOptimizer(
            learning_rate=0.1,
            l1_regularization_strength=0.001
        )
    )

    def input_fn_train():
        x = tf.constant(training_data)
        y = tf.constant(training_target)
        return x, y

    def input_fn_test():
        x = tf.constant(test_data)
        y = tf.constant(test_target)
        return x, y

    def new_tests():
        return np.array(
            [
                [33.33, 44.44],
            ], dtype=np.float32)

    classifier.fit(input_fn=input_fn_train, steps=200)
    score = classifier.evaluate(input_fn=input_fn_test, steps=1)
    score_accuracy = score["accuracy"]
    score_loss = score["loss"]

    print("Score: ", score)
    print("Accuracy: ", score_accuracy)
    print("Loss: ", score_loss)

    predictions = list(classifier.predict_classes(input_fn=new_tests))
    print("Predictions: {}".format(predictions))

所以,我希望看到每个单一预测的概率百分比,不确定是否可能?

任何其他评论,建议都非常受欢迎,我是Tensorflow和ML的新人。

谢谢。

1 个答案:

答案 0 :(得分:8)

我会回答我自己的问题,也许有人会发现它很有用

所以,我需要使用函数predict_proba,它将返回包含概率的值数组。

    training_data = np.vstack(training_data)
    training_target = np.vstack(training_target)
    test_data = np.vstack(test_data)
    test_target = np.vstack(test_target)

    feature_columns = tf.contrib.learn.infer_real_valued_columns_from_input(training_data)
    classifier = tf.contrib.learn.DNNClassifier(
        feature_columns=feature_columns,
        hidden_units=[10, 20, 10],
        n_classes=3,
        model_dir="/tmp/basic09",
        optimizer=tf.train.ProximalAdagradOptimizer(
            learning_rate=0.1,
            l1_regularization_strength=0.001
        )
    )

    def input_fn_train():
        x = tf.constant(training_data)
        y = tf.constant(training_target)
        return x, y

    def input_fn_test():
        x = tf.constant(test_data)
        y = tf.constant(test_target)
        return x, y

    def new_tests():
        return np.array(
            [
                [33.33, 44.44],
            ], dtype=np.float32)

    classifier.fit(input_fn=input_fn_train, steps=200)
    score = classifier.evaluate(input_fn=input_fn_test, steps=1)
    score_accuracy = score["accuracy"]
    score_loss = score["loss"]

    print("Score: ", score)
    print("Accuracy: ", score_accuracy)
    print("Loss: ", score_loss)

    predictions = list(classifier.predict_proba(input_fn=new_tests))
    print("Predictions probability: ", predictions)