2
$\begingroup$

Can someone explain the differance between tf.contrib.DNNClassifier (learn) and tf.estimator.DNNClassifier?

tf.contrib.DNNClassifier (learn) works but gives warnings:

WARNING:tensorflow:From C:\Anaconda3\lib.. scalar_summary ...is deprecated and will be removed after 2016-11-30. Please switch to tf.summary.scalar.

But I can load the layers names and values with get_variable_names and get_variable_value.

tf.estimator works fine but how do I get the layers name and values? See code below with a switch (if True/False) for the two versions

Instalation in Windows 10
3.6.1 |Anaconda 4.4.0 (64-bit)| (default, May 11 2017, 13:25:24) [MSC v.1900 64 bit (AMD64)] Tensorflow is installed in the root with:
pip install --upgrade tensorflow --ignore-installed (This is the only combination that works for me)
pip list gives tensorflow (1.3.0) tensorflow-tensorboard (0.1.4)

#Example of DNNClassifier for Iris plant dataset.
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import numpy as np
from sklearn import datasets
from sklearn import metrics
from sklearn import model_selection
import os
import tensorflow as tf
from tensorflow.contrib import learn
os.environ['TF_CPP_MIN_LOG_LEVEL']='3'  # get rid oc tf_jenkins WARNING

def main():
    # Load dataset.
    iris = datasets.load_iris()
    x_train, x_test, y_train, y_test = model_selection.train_test_split(
        iris.data, iris.target, test_size=0.2, random_state=42)

    # Define the training inputs and train
    get_train_input_fn = tf.estimator.inputs.numpy_input_fn(
        x={'x':x_train}, y=y_train, num_epochs=None, shuffle=True)
    #Predict 
    get_test_input_fn = tf.estimator.inputs.numpy_input_fn(
        x={'x':x_test}, y=y_test, num_epochs=1, shuffle=True)

    # Build 3 layer DNN with 10, 20, 10 units respectively.    
    feature_columns = [tf.feature_column.numeric_column('x', shape=np.array(x_train).shape[1:])]    
    #Select code 
    if True: # tf.estimator...
        classifier = tf.estimator.DNNClassifier(feature_columns=feature_columns, 
            hidden_units=[10, 20, 10], n_classes=3)
        #Train
        classifier.train(input_fn=get_train_input_fn, steps=200)

        scores = classifier.evaluate(input_fn=get_test_input_fn)
        print('Accuracy (tf.estimator): {0:f}'.format(scores['accuracy']))
        # get_variable_names, get_variable_value. How ?????

    else: # learn (tf.contrib)
        classifier = learn.DNNClassifier(feature_columns=feature_columns,
            hidden_units=[10, 20, 10], n_classes=3)
        #Train    
        classifier.fit(input_fn=get_train_input_fn, steps=200) 

        scores = classifier.evaluate(input_fn=get_test_input_fn,steps=1)
        print("\nTest Accuracy (learn): {0:f}\n".format(scores["accuracy"]))

        # Get data
        names=classifier.get_variable_names()
        data={}
        for name in names:
            data[name]=classifier.get_variable_value(name)

if __name__ == "__main__":
    main()
$\endgroup$
1
$\begingroup$

Programming questions are a bit out of scope of this site, but it's a more generic one. For some reason the developers didn't specify it in code, but everything in contrib is experimental. From this discussion:

The distinction between core and contrib is really in core things don't change. Things are backward compatible until release 2.0, and nobody's thinking about that right now.

If you have something in core, it's stable, you should use it. If you have something in contrib, the API may change and depending on your needs you may or may not want to use it.

When a particular class or function is only in contrib, there's basically no choice. But for classes that have "graduated" to the core, contrib version gets deprecated automatically. So you want to use tf.estimator.DNNClassifier.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.