Tensflow:tf.contrib.learn.DNNClassifier()模型的保存与复用以iris为例

python版本:3.5
tensorflow版本:1.4.0
在学习tensorflow深度学习应用时,很多资料以iris为例,并列举了很简单的例子,如本文下述代码。但是我们在搜集,将训练好的深度神经网络保存后,如何复用做测试时,就会搜到很多关于session相关的内容,但是又与自己好不容易学习的这个简单示例不匹配。本人也遇到同样的问题,查阅了很多博客都大同小异,并不是自己所需要的。接下来我将介绍使用tf.contrib.learn.DNNClassifier()训练保存的模型如何重新的复用。方法真的是非常非常简单

第一步: 训练深度神经网络,并将模型进行保存。

import os
import urllib

import numpy as np
import tensorflow as tf

import time


# Data sets
IRIS_TRAINING = "iris_training.csv"
IRIS_TRAINING_URL = "http://download.tensorflow.org/data/iris_training.csv"

IRIS_TEST = "iris_test.csv"
IRIS_TEST_URL = "http://download.tensorflow.org/data/iris_test.csv"

def main():
  # If the training and test sets aren't stored locally, download them.
  if not os.path.exists(IRIS_TRAINING):
    raw = urllib.urlopen(IRIS_TRAINING_URL).read()
    with open(IRIS_TRAINING, "w") as f:
      f.write(raw)

  if not os.path.exists(IRIS_TEST):
    raw = urllib.urlopen(IRIS_TEST_URL).read()
    with open(IRIS_TEST, "w") as f:
      f.write(raw)

  # Load datasets.
  training_set = tf.contrib.learn.datasets.base.load_csv_with_header(
      filename=IRIS_TRAINING,
      target_dtype=np.int,
      features_dtype=np.float32)
  test_set = tf.contrib.learn.datasets.base.load_csv_with_header(
      filename=IRIS_TEST,
      target_dtype=np.int,
      features_dtype=np.float32)

  print(training_set)
  # print(test_set)

  # Specify that all features have real-value data
  feature_columns = [tf.contrib.layers.real_valued_column("", dimension=4)]
  print(feature_columns)
  # Build 3 layer DNN with 10, 20, 10 units respectively.
  classifier = tf.contrib.learn.DNNClassifier(feature_columns=feature_columns,hidden_units=[10, 20, 20, 10],n_classes=3,model_dir="D:\PycharmProject\Deep_Learning\Tensorflow_learning\model_iris")

  # Define the training inputs
  def get_train_inputs():
    x = tf.constant(training_set.data)
    y = tf.constant(training_set.target)
    return x, y

  # Fit model.
  classifier.fit(input_fn=get_train_inputs, steps=2000)

  # Define the test inputs
  def get_test_inputs():
    x = tf.constant(test_set.data)
    y = tf.constant(test_set.target)
    return x, y

  # Evaluate accuracy.
  accuracy_score = classifier.evaluate(input_fn=get_test_inputs,steps=1)["accuracy"]
  print("\nTest Accuracy: {0:f}\n".format(accuracy_score))

  print(classifier.evaluate(input_fn=get_test_inputs,steps=1))
  print(list(classifier.predict(input_fn=get_test_inputs)))
  # Classify two new flower samples.
  def new_samples():
    return np.array(
      [[6.4, 3.2, 4.5, 1.5],
       [5.8, 3.1, 5.0, 1.7]], dtype=np.float32)

  predictions = list(classifier.predict(input_fn=new_samples))

  print(
      "New Samples, Class Predictions:    {}\n"
      .format(predictions))


if __name__ == "__main__":
    start = time.time()
    main()
    end = time.time()
    print('运行时间:',end-start)

运行部分结果如下:

Test Accuracy: 0.966667
运行时间: 5.970066785812378

生成的文件内容如下:
Tensflow:tf.contrib.learn.DNNClassifier()模型的保存与复用以iris为例_第1张图片
第二步:恢复模型利用,得出测试结果。

import os
import urllib

import numpy as np
import tensorflow as tf

from sklearn.metrics import confusion_matrix
from sklearn.metrics import classification_report

import time

IRIS_TEST = "D:\PycharmProject\Deep_Learning\Tensorflow_learning\iris_test.csv"
IRIS_TEST_URL = "http://download.tensorflow.org/data/iris_test.csv"

def main():

  test_set = tf.contrib.learn.datasets.base.load_csv_with_header(
      filename=IRIS_TEST,
      target_dtype=np.int,
      features_dtype=np.float32)

  # Specify that all features have real-value data
  feature_columns = [tf.contrib.layers.real_valued_column("", dimension=4)]
  print(feature_columns)
  # Build 3 layer DNN with 10, 20, 10 units respectively.
  classifier = tf.contrib.learn.DNNClassifier(feature_columns=feature_columns,hidden_units=[10, 20, 20, 10],n_classes=3,model_dir="D:\PycharmProject\Deep_Learning\Tensorflow_learning\model_iris")


  # Define the test inputs
  def get_test_inputs():
    x = tf.constant(test_set.data)
    y = tf.constant(test_set.target)
    return x, y

  # Evaluate accuracy.
  accuracy_score = classifier.evaluate(input_fn=get_test_inputs,steps=1)["accuracy"]
  print("\nTest Accuracy: {0:f}\n".format(accuracy_score))

  print(classifier.evaluate(input_fn=get_test_inputs,steps=1))
  print(list(classifier.predict(input_fn=get_test_inputs)))
  # Classify two new flower samples.
  def new_samples():
    return np.array(
      [[6.4, 3.2, 4.5, 1.5],
       [5.8, 3.1, 5.0, 1.7]], dtype=np.float32)

  predictions = list(classifier.predict(input_fn=new_samples))

  print(
      "New Samples, Class Predictions:    {}\n"
      .format(predictions))

  # conf_mat = confusion_matrix(testlabel,predictions)
  # report = classification_report(testlabel,predictions)


if __name__ == "__main__":
    start = time.time()
    main()
    end = time.time()
    print('运行时间:',end-start)

运行部分结果如下:

Test Accuracy: 0.966667
运行时间: 2.1934993267059326

完成,是不是很简单,通过测试集得出的accuracy和运行的时间就可以得知,第二步并没有重新的训练模型,而是直接使用已经训练好的深度神经网络,但是需要注意的问题是模型的结构要统一,如下代码所示:

tf.contrib.learn.DNNClassifier(feature_columns=feature_columns,hidden_units=[10, 20, 20,10],n_classes=3,model_dir="D:\PycharmProject\Deep_Learning\Tensorflow_learning\model_iris")

你可能感兴趣的:(tensorflow,神经网络,深度学习)