tf.train.get.global_step张量流分类示例中的错误



我正在关注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:UsersisaiaAppDataLocalTemptmp8be6vyhq
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:UsersisaiaAppDataLocalContinuumanaconda3libsite-packagestensorflowcontriblearnpythonlearnestimatorslinear.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

提前感谢您的帮助

这个估计器基本上被弃用了,可以在下一个版本从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)

您需要在代码的 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)

此外,为了获得一维数组作为类预测,您可能需要在分类器.predict方法中设置as_iterable=False

希望这有帮助。

相关内容

  • 没有找到相关文章

最新更新