使用Tensorflow形成错误(tf.learn,DNNClassifier)

时间:2016-10-22 14:51:44

标签: tensorflow

我在张量流中遇到了一个我无法弄清楚的形状错误。

如果我使用tf.learn运行iris数据集的基本演示,它看起来像这样:

iris = datasets.load_iris()
x_iris = iris.data
y_iris = iris.target
>>> x_iris.shape
(150, 4)
>>> y_iris.shape
(150,)
>>> type(x_iris)
<class 'numpy.ndarray'>

看起来不错。我运行这段代码:

feature_columns = [ tf.contrib.layers.real_valued_column( "", dimension = 4 ) ]
classifier = tf.contrib.learn.DNNClassifier( feature_columns = feature_columns, hidden_units = [ 10, 20, 10 ], n_classes = 3, model_dir = "/tmp/iris_model" )
classifier.fit( x = x_iris, y = y_tiris, steps = 2000 )

它很棒!这很好。

现在,我和Kaggle一起使用的泰坦尼克号数据集完全相同:

>>> x_titanic.shape
(700, 14)
>>> y_titanic.shape
(700,)
>>>type(x_titanic)
<class 'numpy.ndarray'>

相同形状,相同类型。应该可以。我运行相同的代码:

feature_columns = [ tf.contrib.layers.real_valued_column( "", dimension = 14 ) ]
classifier = tf.contrib.learn.DNNClassifier( feature_columns = feature_columns, hidden_units = [ 10, 20, 10 ], n_classes = 2, model_dir = "/tmp/iris_model" )
classifier.fit( x = x_titanic, y = y_titanic, steps = 2000 )

我收到了这个错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.4/dist-packages/tensorflow/contrib/learn/python/learn/estimators/dnn.py", line 435, in fit
    max_steps=max_steps)
  File "/usr/local/lib/python3.4/dist-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 333, in fit
    max_steps=max_steps)
  File "/usr/local/lib/python3.4/dist-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 662, in _train_model
    train_op, loss_op = self._get_train_ops(features, targets)
  File "/usr/local/lib/python3.4/dist-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 963, in _get_train_ops
    _, loss, train_op = self._call_model_fn(features, targets, ModeKeys.TRAIN)
  File "/usr/local/lib/python3.4/dist-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 944, in _call_model_fn
    return self._model_fn(features, targets, mode=mode, params=self.params)
  File "/usr/local/lib/python3.4/dist-packages/tensorflow/contrib/learn/python/learn/estimators/dnn.py", line 258, in _dnn_classifier_model_fn
    weight=_get_weight_tensor(features, weight_column_name))
  File "/usr/local/lib/python3.4/dist-packages/tensorflow/contrib/losses/python/losses/loss_ops.py", line 329, in sigmoid_cross_entropy
    logits.get_shape().assert_is_compatible_with(multi_class_labels.get_shape())
  File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/framework/tensor_shape.py", line 750, in assert_is_compatible_with
    raise ValueError("Shapes %s and %s are incompatible" % (self, other))
ValueError: Shapes (?, 1) and (?,) are incompatible

这是为什么?看起来y_titanic的形状存在问题:

sigmoid_cross_entropy
logits.get_shape().assert_is_compatible_with(multi_class_labels.get_shape())

由于它只是二进制(0,1),但这是DNNClassifier的默认值。有什么特别的东西我要改变吗?我是否需要tf.one_hot y矢量?

2 个答案:

答案 0 :(得分:0)

看起来这是一个可能仍在修复的已知问题:

https://github.com/tensorflow/tensorflow/issues/4715

我改变了&#39; n_classes&#39;到3,问题消失了 - 性能也很好!仍然没有100%确定为什么这发生了。

看起来修复是在11天前制作的https://github.com/tensorflow/tensorflow/commit/b718fd6ad8cebc470fcc7e53bb6902168edd5587

这意味着我必须更新张量流...

答案 1 :(得分:0)

转到here并下载最新的whl文件,将修复此错误。