Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save&restore DNNClassifier trained in TensorFlow python; iris example

I'm new to TensorFlow, just started learning a few days ago. I've completed this tutorial(https://www.tensorflow.org/versions/r0.9/tutorials/tflearn/index.html#tf-contrib-learn-quickstart) and applied the exact same idea to my own data set. (which came out pretty good!)

Now I'd like to save&restore the trained DNNClassifier for further use. If anyone know how to do that, please let me know by using the iris example code in the link above. Thanks for your help in advance!

like image 932
Maron Avatar asked Jul 13 '16 17:07

Maron


Video Answer


2 Answers

found the solution to this? In case you didn't you can do this specifying the model_dir parameter on the constructor when creating the DNNClassifier, this will create all the checkpoints and files in this directory(the saving step). When you want to do the restore step, you just create another DNNClassifier passing the same model_dir parameter(restore phase) , this will restore the model from the files created the first time.

Hope this helps to you.

like image 87
Luis Leal Avatar answered Dec 15 '22 10:12

Luis Leal


Below is my Code...

import tensorflow as tf
import numpy as np

if __name__ == '__main__':
# Data sets
IRIS_TRAINING = "iris_training.csv"
IRIS_TEST = "iris_test.csv"

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

x_train, x_test, y_train, y_test = training_set.data, test_set.data, training_set.target, test_set.target

# Build 3 layer DNN with 10, 20, 10 units respectively.
classifier = tf.contrib.learn.DNNClassifier(hidden_units=[10, 20, 10], n_classes=3, model_dir="path_to_my_local_dir")

# print classifier.model_dir

# Fit model.
print "start fitting model..."
classifier.fit(x=x_train, y=y_train, steps=200)
print "finished fitting model!!!"

# Evaluate accuracy.
accuracy_score = classifier.evaluate(x=x_test, y=y_test)["accuracy"]
print('Accuracy: {0:f}'.format(accuracy_score))

#Classify two new flower samples.
new_samples = np.array(
    [[6.4, 3.2, 4.5, 1.5], [5.8, 3.1, 5.0, 1.7]], dtype=float)
y = classifier.predict_proba(new_samples)
print ('Predictions: {}'.format(str(y)))

#---------------------------------------------------------------------------------
#model_dir below has to be the same as the previously specified path!
new_classifier = tf.contrib.learn.DNNClassifier(hidden_units=[10, 20, 10], n_classes=3, model_dir="path_to_my_local_dir")
accuracy_score = new_classifier.evaluate(x=x_test, y=y_test)["accuracy"]
print('Accuracy: {0:f}'.format(accuracy_score))
new_samples = np.array(
    [[6.4, 3.2, 4.5, 1.5], [5.8, 3.1, 5.0, 1.7]], dtype=float)
y = classifier.predict_proba(new_samples)
print ('Predictions: {}'.format(str(y)))
like image 43
Maron Avatar answered Dec 15 '22 10:12

Maron