【问题标题】:Multiple labels with tensorflow带有张量流的多个标签
【发布时间】:2017-03-19 15:16:46
【问题描述】:

我正在尝试修改此代码(请参阅下面的 GitHub 链接),以便我可以使用自己的数据并使用同一组功能预测多个标签。

https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/tutorials/input_fn/boston.py

当我一次使用一个标签时,它可以正常工作。但是,当我尝试创建一个包含多个标签的张量时,我遇到了问题。有什么建议吗?

我修改后的 LABELS 和 input_fn 如下所示:

LABELS = ["Label1", "Label2", "Label3"]

def input_fn(data_set):
  feature_cols = {k: tf.constant(len(data_set), shape=[data_set[k].size, 1]) for k in FEATURES}
  labels_data = []
  for i in range(0, len(data_set)):
    temp = []
    for label in LABELS:
        temp.append(data_set[label].values[i])
    labels_data.append(temp)
  labels = tf.constant(labels_data, shape=[len(data_set), len(LABELS)])
  return feature_cols, labels 

这是我收到的错误消息的结尾:

  File "/usr/lib/python2.7/site-packages/tensorflow/contrib/learn/python/learn/estimators/dnn.py", line 175, in _dnn_model_fn
return head.head_ops(features, labels, mode, _train_op_fn, logits)
  File "/usr/lib/python2.7/site-packages/tensorflow/contrib/learn/python/learn/estimators/head.py", line 403, in head_ops
head_name=self.head_name)
  File "/usr/lib/python2.7/site-packages/tensorflow/contrib/learn/python/learn/estimators/head.py", line 1358, in _training_loss
loss_fn(logits, labels),
  File "/usr/lib/python2.7/site-packages/tensorflow/contrib/learn/python/learn/estimators/head.py", line 330, in _mean_squared_loss
logits.get_shape().assert_is_compatible_with(labels.get_shape())
  File "/usr/lib/python2.7/site-packages/tensorflow/python/framework/tensor_shape.py", line 735, in assert_is_compatible_with
raise ValueError("Shapes %s and %s are incompatible" % (self, other))
ValueError: Shapes (118, 1) and (118, 3) are incompatible

【问题讨论】:

    标签: tensorflow


    【解决方案1】:

    this link看来,您必须将类(标签)的数量指定为 3:

    regressor = tf.contrib.learn.DNNRegressor(feature_columns=feature_cols,
                                            n_classes=3,
                                            hidden_units=[10, 10],
                                            model_dir="/tmp/boston_model")
    

    n_classes 值可能应该设置为 2 而不是 3,看看哪个有效

    【讨论】:

    • 完全重复的答案
    【解决方案2】:

    here,你必须改变这个参数:

    label_dimension: Dimension of the label for multilabels. Defaults to 1.
    

    所以这应该可行:

    regressor = tf.contrib.learn.DNNRegressor(feature_columns=feature_cols,
                                            label_dimension=3,
                                            hidden_units=[10, 10],
                                            model_dir="/tmp/boston_model")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-16
      • 1970-01-01
      • 1970-01-01
      • 2016-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多