tf.train.get.global_step error during Tensorflow classification example

Viewed 1583

I am following Siraj Raval's YouTube video for building a simple classifier for the iris flower data set. The video was dated May of 2016 so I am sure that there are some areas of Tensorflow that have been updated. I am getting an error that says "please switch to tf.train.get.global_step. I am working off of the older dated Tensorflow library and I have tried figuring out the new one by researching feature_columns. I thought this would fix it but the error persists. Any help is greatly appreciated and any advice on becoming an educated Tensorflow user is openly welcome.

Here is my code

import tensorflow.contrib.learn as skflow
from sklearn import datasets, metrics

iris = datasets.load_iris()

feature_columns = skflow.infer_real_valued_columns_from_input(iris.data)

classifier = skflow.LinearClassifier(feature_columns=feature_columns, n_classes=3)

classifier.fit(iris.data, iris.target)


score = metrics.accuracy_score(iris.target, classifier.predict(iris.data))

print("Accuracy: %f" % score)

And here is the error:

WARNING:tensorflow:float64 is not supported by many models, consider casting to float32.
WARNING:tensorflow:Using temporary folder as model directory: C:\Users\isaia\AppData\Local\Temp\tmp8be6vyhq
WARNING:tensorflow:From C:/Users/isaia/PycharmProjects/untitled5/ml.py:10: calling BaseEstimator.fit (from tensorflow.contrib.learn.python.learn.estimators.estimator) with x is deprecated and will be removed after 2016-12-01.
Instructions for updating:
Estimator is decoupled from Scikit Learn interface by moving into
separate class SKCompat. Arguments x, y and batch_size are only
available in the SKCompat class, Estimator will only accept input_fn.
Example conversion:
  est = Estimator(...) -> est = SKCompat(Estimator(...))
WARNING:tensorflow:From C:/Users/isaia/PycharmProjects/untitled5/ml.py:10: calling BaseEstimator.fit (from tensorflow.contrib.learn.python.learn.estimators.estimator) with y is deprecated and will be removed after 2016-12-01.
Instructions for updating:
Estimator is decoupled from Scikit Learn interface by moving into
separate class SKCompat. Arguments x, y and batch_size are only
available in the SKCompat class, Estimator will only accept input_fn.
Example conversion:
  est = Estimator(...) -> est = SKCompat(Estimator(...))
WARNING:tensorflow:float64 is not supported by many models, consider casting to float32.
WARNING:tensorflow:From C:\Users\isaia\AppData\Local\Continuum\anaconda3\lib\site-packages\tensorflow\contrib\learn\python\learn\estimators\linear.py:173: get_global_step (from tensorflow.contrib.framework.python.ops.variables) is deprecated and will be removed in a future version.
Instructions for updating:
Please switch to tf.train.get_global_step

Thank you in advance for your help

2 Answers

You need to specify the number of training steps in the classifier.fit method in your code. I have edited your code and given comments where its necessary.

import tensorflow.contrib.learn as skflow
from sklearn import datasets, metrics

iris = datasets.load_iris()

feature_columns = skflow.infer_real_valued_columns_from_input(iris.data)

classifier = skflow.LinearClassifier(feature_columns=feature_columns,n_classes=3)
classifier.fit(iris.data, iris.target,steps=10) #Define the Number of traning steps here
results = classifier.predict(x=iris.data, as_iterable=False) #Set as_iterable=False to get an 1-D array for metrics.accuracy_score

score = metrics.accuracy_score(iris.target, results)

print("Accuracy: %f" % score)

Moreover, in order to get a 1-D array as class predictions, you may need to set as_iterable=False in the classifier.predict method.

Hope this helps.

This estimator is basically deprecated and can be removed from tensorflow in the next releases (and you get several warnings about it), you should use tf.estimator.LinearClassifier. Its API is slightly different, but the idea is still the same. Here's the complete code:

import numpy as np
import tensorflow as tf
from sklearn import datasets, metrics

iris = datasets.load_iris()

# The classifier
feature_columns = [tf.feature_column.numeric_column("x", shape=[4])]
classifier = tf.estimator.LinearClassifier(feature_columns=feature_columns,
                                           n_classes=3)

# Training
train_input_fn = tf.estimator.inputs.numpy_input_fn(x={"x": iris.data},
                                                    y=iris.target,
                                                    num_epochs=50,
                                                    shuffle=True)
classifier.train(train_input_fn)

# Testing
test_input_fn = tf.estimator.inputs.numpy_input_fn(x={"x": iris.data},
                                                   num_epochs=1,
                                                   shuffle=False)
predictions = classifier.predict(test_input_fn)
predicted_classes = [p["classes"].astype(np.float)[0] for p in predictions]

score = metrics.accuracy_score(iris.target, predicted_classes)
print("Accuracy: %f" % score)
Related