我正在尝试修改此代码(请参阅下面的GitHub链接),以便我可以使用自己的数据并使用相同的功能集预测多个标签。
当我一次使用一个标签时,我可以使用它。但是当我尝试创建一个包含多个标签的张量时,我遇到了问题。有什么建议吗?
我修改的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
答案 0 :(得分:2)
来自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,看看哪个有用
答案 1 :(得分:1)
从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")