ValueError:logits和targets必须具有相同的形状(tf.learn,DNNLinearCombinedClassifier)

时间:2016-11-04 13:03:01

标签: python machine-learning neural-network tensorflow deep-learning

我试图在我自己的数据集上训练'Wide & Deep Learning'模型,当我将模型拟合到训练集时会发生此错误。

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-15-8f5351c1fdf8> in <module>()
----> 1 m.fit(input_fn=train_input_fn, steps=200)

/Users/prisma/anaconda/lib/python2.7/site-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.pyc in fit(self, x, y, input_fn, steps, batch_size, monitors, max_steps)
331                              steps=steps,
332                              monitors=monitors,
--> 333                              max_steps=max_steps)
334     logging.info('Loss for final step: %s.', loss)
335     return self

/Users/prisma/anaconda/lib/python2.7/site-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.pyc in _train_model(self, input_fn, steps, feed_fn, init_op, init_feed_fn, init_fn, device_fn, monitors, log_every_steps, fail_on_nan_loss, max_steps)
660       features, targets = input_fn()
661       self._check_inputs(features, targets)
--> 662       train_op, loss_op = self._get_train_ops(features, targets)
663 
664       # Add default monitors.

/Users/prisma/anaconda/lib/python2.7/site-packages/tensorflow/contrib/learn/python/learn/estimators/dnn_linear_combined.pyc in _get_train_ops(self, features, targets)
188     logits = self._logits(features, is_training=True)
189     if self._enable_centered_bias:
--> 190       centered_bias_step = [self._centered_bias_step(targets, features)]
191     else:
192       centered_bias_step = []

/Users/prisma/anaconda/lib/python2.7/site-packages/tensorflow/contrib/learn/python/learn/estimators/dnn_linear_combined.pyc in _centered_bias_step(self, targets, features)
272     with ops.name_scope(None, "centered_bias", (targets, features)):
273       training_loss = self._target_column.training_loss(
--> 274           logits, targets, features)
275     # Learn central bias by an optimizer. 0.1 is a convervative lr for a
276     # single variable.

/Users/prisma/anaconda/lib/python2.7/site-packages/tensorflow/contrib/layers/python/layers/target_column.pyc in training_loss(self, logits, target, features, name)
204     """
205     target = target[self.name] if isinstance(target, dict) else target
--> 206     loss_unweighted = self._loss_fn(logits, target)
207 
208     weight_tensor = self.get_weight_tensor(features)

/Users/prisma/anaconda/lib/python2.7/site-packages/tensorflow/contrib/layers/python/layers/target_column.pyc in _log_loss_with_two_classes(logits, target)
387     target = array_ops.expand_dims(target, dim=[1])
388   loss_vec = nn.sigmoid_cross_entropy_with_logits(logits,
--> 389                                                   math_ops.to_float(target))
390   return loss_vec
391 

/Users/prisma/anaconda/lib/python2.7/site-packages/tensorflow/python/ops/nn.pyc in sigmoid_cross_entropy_with_logits(logits, targets, name)
432     except ValueError:
433       raise ValueError("logits and targets must have the same shape (%s vs %s)"
--> 434                        % (logits.get_shape(), targets.get_shape()))
435 
436     # The logistic loss formula from above is

ValueError: logits and targets must have the same shape ((?, 1) vs (13647309, 24))

我无法弄清楚为什么logits的形状(?,1)而不是(13647309,24)。 input_fn函数应该返回一个大小为(13647309,24)的特征字典和一个形状的标签张量(13647309,24)。就我而言,logits应该是模型的输出,但DNNLinearCombinedClassifier中没有指定输出大小的位置,因此我假设输出大小自动调整为与标签尺寸相同,即(13647309,24)。我不知道为什么会出现这种错误,但我猜我的模型出了问题。由于整个代码太长而无法粘贴,我只需将模型构建部分粘贴在此处。

model_dir = tempfile.mkdtemp()
m = tf.contrib.learn.DNNLinearCombinedClassifier(
    model_dir=model_dir,
    linear_feature_columns=wide_columns,
    dnn_feature_columns=deep_columns,
    dnn_hidden_units=[100, 50])

我没有从tensorflow教程改变模型的参数。我刚刚定义了&#39; wide_columns&#39;和&#39; deep_columns&#39;就我自己的数据集而言。模型或输入功能有问题吗?我无法在tf.learn api网站上找到DNNLinearCombinedClassifier的参考资料。

更新输入功能的代码

def input_fn(df):
  continuous_cols = {k: tf.constant(df[k].values)
                     for k in CONTINUOUS_COLUMNS}

  categorical_cols = {k: tf.SparseTensor(
      indices=[[i, 0] for i in range(df[k].size)],
      values=df[k].values,
      shape=[df[k].size, 1])
                      for k in CATEGORICAL_COLUMNS}

  feature_cols = dict(continuous_cols.items() + categorical_cols.items())

  label = tf.constant(df[Label_COLUMNS].values)

  return feature_cols, label

&#39; Label_COLUMNS&#39;。

中有24个频道

1 个答案:

答案 0 :(得分:0)

问题是您必须在n_classes=24的构造函数中指定DNNLinearCombinedClassifier。有关文档,请参阅here