Skip to content Skip to sidebar Skip to footer

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-qui

Solution 1:

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.

Solution 2:

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)))

Post a Comment for "How To Save&restore Dnnclassifier Trained In Tensorflow Python; Iris Example"