0
votes

I want to embed labels into a DNNClassifier model in Tensorflow. Unlike the documentation example, here , I get the following error message:

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

< dtype: 'string'>, got < dtype: 'int64'>

On the other hand, if I make the label_key_values column a numpy.array, I will get the following error:

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

1

1 Answers

1
votes

I got the solution. As the official documentation says here :

"If the user specifies label_keys in constructor, labels must be strings from the label_keys vocabulary."

In my case, I transformed the label column from the training and testing set into an one-hot vector(integer values) and the values from label_keys_values array did not match with them.