Tensorflow分类示例中的tf.train.get.global_step错误

时间:2017-11-04 21:48:48

标签: python machine-learning tensorflow scikit-learn classification

我正在关注Siraj Raval的YouTube视频,为虹膜花数据集构建一个简单的分类器。该视频的日期为2016年5月,因此我确信Tensorflow的某些区域已更新。我收到的错误是"请切换到tf.train.get.global_step。我正在使用较旧的Tensorflow库,我尝试通过研究feature_columns来找出新的。我认为这会解决它,但错误仍然存​​在。非常感谢任何帮助,并欢迎任何关于成为受过良好教育的Tensorflow用户的建议。

这是我的代码

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)

这是错误:

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

提前感谢您的帮助

2 个答案:

答案 0 :(得分:0)

您需要在代码中的 classifier.fit 方法中指定训练步骤的数量。我已经编辑了你的代码,并在必要时给出了评论。

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)

此外,为了将1-D数组作为类预测,您可能需要在 classifier.predict 方法中设置 as_iterable = False

希望这有帮助。

答案 1 :(得分:0)

此估算工具is basically deprecated可以在下一版本中从tensorflow中删除(并且您会收到几条警告),您应该使用tf.estimator.LinearClassifier。它的API略有不同,但想法仍然相同。这是完整的代码:

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)