【问题标题】:Shape error using Tensorflow (tf.learn, DNNClassifier)使用 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 使用过的 Titanic 数据集也遇到了完全相同的情况:

>>> 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 向量吗?

【问题讨论】:

    标签: tensorflow


    【解决方案1】:

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

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

    我将 'n_classes' 更改为 3,问题就消失了 - 性能也很好!仍然不能 100% 确定为什么会发生这种情况。

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

    这意味着我必须更新我的 tensorflow...

    【讨论】:

      【解决方案2】:

      here下载最新的whl文件,会修复这个bug。

      【讨论】:

        猜你喜欢
        • 2017-02-26
        • 2017-10-28
        • 1970-01-01
        • 1970-01-01
        • 2017-10-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多