Tensorflow: extracting predictions from "Layer Module"

时间:2017-10-12 10:12:40

标签: tensorflow neural-network

I am following the Tensorflow's "Layer Module" from this tutorial link https://www.tensorflow.org/tutorials/layers. You might be able to help me how can I get the results of the predictions and its respective probabilities. I need to see it for further understanding the model. And if there is a way I can save the results - predictions and probabilities to csv.

Thank you so much for your time.

1 个答案:

答案 0 :(得分:0)

我实际上已经找到了一种解决方法,这很简单,就像我想的那样。我觉得有些人可能有类似的问题所以在这里。 Tensorflow是一种处理机器学习模型的新框架,但我最终意识到这很容易。

Tensorflow在您使用tf创建的模型上具有此* .predict(...)函数,它返回您在模型上定义的包含“类”和“概率”的预测变量

在你的模型上举例:

some_classifier = tf.estimator.Estimator(model_fn=model_fn,
    model_dir=...)  

你可以做这个预测(我们知道)

prediction_input_fn = tf.estimator.inputs.numpy_input_fn(
    x={"x": test_feats}, #given you have this variable containing the test data
    y=test_labels, #and the equivalent label for the test data
    num_epochs=1,
    shuffle=False)
prediction_results = some_classifier.predict(input_fn=prediction_input_fn)  

然后变量 prediction_results 包含预测类及其概率的值,然后可以保存(例如使用Panda)

save = panda.DataFrame(list(prediction_results))  
save.to_csv("file.csv")  

上面的代码片段效果很好,因为您已经在模型中编写了一个代码,用于预测的容器,如下所示:

predictions = {
  "classes": tf.argmax(input=logits, axis=1), 
  "probabilities": tf.nn.softmax(logits, name="softmax_tensor")

}