0

我想在Tensorflow中將標籤嵌入到DNNClassifier模型中。 不同於文檔例如,here,我收到以下錯誤信息:DNNCLassifier上的label_keys類型錯誤Tensorflow

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 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

< D型: '串'>,得到了< D型: 'Int64的'>

。另一方面,如果我做了label_key_valuesnumpy.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 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()

回答

0

我得到了解決方案。正如文檔所述,here

「如果用戶在構造函數中指定了label_keys,那麼標籤必須是來自label_keys詞彙表的字符串。」

就我而言,我將訓練和測試集中的標籤列轉換爲單向量向量(整數值),並且label_keys_values數組中的值與它們不匹配。