gpt4 book ai didi

python-2.7 - DNNCLassifier Tensorflow 上的 label_keys 类型错误

转载 作者:行者123 更新时间:2023-11-30 09:51:36 26 4
gpt4 key购买 nike

我想将标签嵌入到 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 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

,得到

另一方面,如果我将 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 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 数组中的值与它们不匹配。

关于python-2.7 - DNNCLassifier Tensorflow 上的 label_keys 类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44219077/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com