使用创建的张量流模型进行预测

时间:2018-01-03 05:44:59

标签: python tensorflow tensorflow-datasets tensorflow-estimator

我正在查看此Tensorflow文章中的源代码,该文章讨论了如何创建广泛深入的学习模型。 https://www.tensorflow.org/versions/r1.3/tutorials/wide_and_deep

以下是python源代码的链接:https://github.com/tensorflow/tensorflow/blob/r1.3/tensorflow/examples/learn/wide_n_deep_tutorial.py

它的目标是培训一个模型,根据人口普查信息中的数据,预测某人每年的收入是否高于或低于5万美元。

按照说明,我正在运行此命令来执行:

python wide_n_deep_tutorial.py --model_type=wide_n_deep

我得到的结果如下:

$ python wide_n_deep.py --model_type=wide_n_deep
Training data is downloaded to /tmp/tmp_pwqo2h8
Test data is downloaded to /tmp/tmph6jcimik
2018-01-03 05:34:12.236038: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA
WARNING:tensorflow:enqueue_data was called with num_epochs and num_threads > 1. num_epochs is applied per thread, so this will produce more epochs than you probably intend. If you want to limit epochs, use one thread.
WARNING:tensorflow:enqueue_data was called with shuffle=False and num_threads > 1. This will create multiple threads, all reading the array/dataframe in order. If you want examples read in order, use one thread; if you want multiple threads, enable shuffling.
WARNING:tensorflow:Casting <dtype: 'float32'> labels to bool.
WARNING:tensorflow:Casting <dtype: 'float32'> labels to bool.
model directory = /tmp/tmp_ab6cfsf
accuracy: 0.808673
accuracy_baseline: 0.763774
auc: 0.841373
auc_precision_recall: 0.66043
average_loss: 0.418642
global_step: 2000
label/mean: 0.236226
loss: 41.8154
prediction/mean: 0.251593

在我在网上看到的各种文章中,它讨论了加载.ckpt文件。当我查看我的模型目录时,我看到这些文件:

$ ls /tmp/tmp_ab6cfsf
checkpoint  eval  events.out.tfevents.1514957651.ml-1  graph.pbtxt  model.ckpt-1.data-00000-of-00001  model.ckpt-1.index  model.ckpt-1.meta  model.ckpt-2000.data-00000-of-00001  model.ckpt-2000.index  model.ckpt-2000.meta

我猜测我将使用的那个是model.ckpt-1.meta,这是正确的吗?

但我也对如何使用和提供此模型数据感到困惑。我在Tensorflow的网站上查看了这篇文章:https://www.tensorflow.org/versions/r1.3/programmers_guide/saved_model

其中说&#34;请注意,Estimators会自动保存和恢复变量(在model_dir中)。&#34; (不确定在这种情况下意味着什么)

我如何以人口普查数据的格式生成信息,除了薪水,因为这是我们应该预测的?对我来说,如何使用两个Tensorflow文章以便能够使用训练有素的模型进行预测并不明显。

1 个答案:

答案 0 :(得分:1)

您可以查看来自TensorFlow团队的官方博客文章(part 1part 3),其中详细介绍了如何使用估算工具。

特别是他们解释了如何使用自定义输入进行预测。这使用了估算器的内置predict方法:

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

predict_input_fn = ...  # define this using tf.data

predict_results = estimator.predict(predict_input_fn)
for idx, prediction in enumerate(predict_results):
    print(idx)
    for key in prediction:
        print("...{}: {}".format(key, prediction[key]))

对于您的示例,我们可以使用其他csv文件创建预测输入函数。假设我们有一个名为"predict.csv"的csv文件,其中包含三个示例(可能是"test.csv"的前三行,例如没有标签)。这会给:

predict.csv

  

...跳过这一行......
  25,私人,226802,11,7,从未结婚,机器操作,自己的孩子,黑人,男性,0,0,40,美国

  38,私人,89814,HS-grad,9,已婚 - 公民 - 配偶,农业捕鱼,丈夫,白人,男性,0,0,50,美国


  28,Local-gov,336951,Assoc-acdm,12,已婚,civ-spouse,Protective-serv,丈夫,白人,男,0,0,40,美国

estimator = build_estimator(FLAGS.model_dir, FLAGS.model_type)

def predict_input_fn(data_file):
    """Input builder function."""
    df_data = pd.read_csv(
        tf.gfile.Open(data_file),
        names=CSV_COLUMNS[:-1],  # remove the last name "income_bracket" that corresponds to the label
        skipinitialspace=True,
        engine="python",
        skiprows=1)
    # remove NaN elements
    df_data = df_data.dropna(how="any", axis=0)
    return tf.estimator.inputs.pandas_input_fn(x=df_data, y=None, shuffle=False)

predict_file_name = "wide_n_deep/predict.csv"
predict_results = estimator.predict(input_fn=predict_input_fn(predict_file_name))
for idx, prediction in enumerate(predict_results):
    print(idx)
    for key in prediction:
        print("...{}: {}".format(key, prediction[key]))
相关问题