我想将标签嵌入到 Tensorflow 中的 DNNClassifier 模型中。 与文档示例here不同,我收到以下错误消息:
label_keys_values = ["satan", "ipsweep", "nmap", "portsweep"]
m = tf.contrib.learn.DNNClassifier(model_dir=model_dir,
feature_columns=deep_columns,
n_classes=4,
hidden_units=[12, 4],
label_keys=label_keys_values)
m.fit(input_fn=train_input_fn, steps=200)
File "embedding_model_probe.py", line 118, in <module>
m.fit(input_fn=train_input_fn, steps=200)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/util/deprecation.py", line 281, in new_func
return func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 430, in fit
loss = self._train_model(input_fn=input_fn, hooks=hooks)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 927, in _train_model
model_fn_ops = self._get_train_ops(features, labels)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 1132, in _get_train_ops
return self._call_model_fn(features, labels, model_fn_lib.ModeKeys.TRAIN)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 1103, in _call_model_fn
model_fn_results = self._model_fn(features, labels, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/dnn.py", line 180, in _dnn_model_fn
logits=logits)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/head.py", line 1004, in create_model_fn_ops
labels = self._transform_labels(mode=mode, labels=labels)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/head.py", line 1033, in _transform_labels
"label_ids": table.lookup(labels_tensor),
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/lookup/lookup_ops.py", line 179, in lookup
(self._key_dtype, keys.dtype))
TypeError: Signature mismatch. Keys must be dtype
< dtype: 'string'>, got < dtype: 'int64'>
另一方面,如果我将
label_key_values
列设置为 numpy.array
,我将收到以下错误:
label_keys_values = np.array(["satan", "ipsweep", "nmap", "portsweep"], dtype='string')
Traceback (most recent call last):
File "embedding_model_probe.py", line 116, in <module>
label_keys=label_keys_values)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/dnn.py", line 337, in __init__
label_keys=label_keys),
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/head.py", line 331, in multi_class_head
label_keys=label_keys)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/head.py", line 986, in __init__
if label_keys and len(label_keys) != n_classes:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
我找到了解决方案。正如官方文档所说here:
如果用户在构造函数中指定 label_keys,则标签必须是 label_keys 词汇表中的字符串。
在我的例子中,我将训练和测试集中的标签列转换为一个单热向量(整数值),并且
label_keys_values
数组中的值与它们不匹配。