【问题标题】:ValueError: Feature not in features dictionaryValueError:特征不在特征字典中
【发布时间】:2018-05-18 01:04:54
【问题描述】:

我正在尝试使用 TensorFlow 编写一个简单的深度机器学习模型。 我正在使用我在 Excel 中制作的玩具数据集,只是为了让模型工作并接受数据。我的代码如下:

import pandas as pd
import numpy as np
import tensorflow as tf

raw_data = np.genfromtxt('ai/mock-data.csv', delimiter=',', dtype=str)
my_data = np.delete(raw_data, (0), axis=0) #deletes the first row, axis=0 indicates row, axis=1 indicates column
my_data = np.delete(my_data, (0), axis=1) #deletes the first column

policy_state = tf.feature_column.categorical_column_with_vocabulary_list('policy_state', [
    'AL', 'CA', 'MI'
])

modern_classic_ind = tf.feature_column.categorical_column_with_vocabulary_list('modern_classic_ind', [
    '0', '1'
])

h_plus_ind = tf.feature_column.categorical_column_with_vocabulary_list('h_plus_ind', [
    '0', '1'
])

retention_ind = tf.feature_column.categorical_column_with_vocabulary_list('retention_ind', [
    '0', '1'
])

feature_columns = [
    tf.feature_column.indicator_column(policy_state),
    tf.feature_column.indicator_column(modern_classic_ind),
    tf.feature_column.indicator_column(h_plus_ind)
]
classifier = tf.estimator.DNNClassifier(feature_columns=feature_columns,
                                      hidden_units=[10, 20, 10],
                                      n_classes=3,
                                      model_dir="/tmp/ret_model")

train_input_fn = tf.estimator.inputs.numpy_input_fn(
x={"x": np.array(my_data[:, 0:3], dtype=str)},
y=np.array(np.array(my_data[:, 3], dtype=str)),
num_epochs=None,
shuffle=True)

classifier.train(input_fn=train_input_fn, steps=2000)

不幸的是,我收到以下错误。我尝试从 csv 文件中修剪标签而不是保留它们,将特征列命名为不同的东西,并更改 numpy 数组的类型。错误仍然存​​在。

ValueError: Feature h_plus_ind is not in features dictionary.

如果我删除 h_plus_ind,它只会在不同的列上抛出错误。

【问题讨论】:

    标签: python numpy dictionary tensorflow


    【解决方案1】:

    使用tf.feature_columns 时,您在 input_fn 中提供的数据应该与之前创建的特征列具有相同的键。 所以,train_input_fnx 应该是一个字典,键名以feature_columns 命名。

    一个模拟示例:

    x = {"policy_state": np.array(['AL','AL','AL','AL','AL']),
         "modern_classic_ind": np.array(['0','0','0','0','0']),
         "h_plus_ind": np.array(['0','0','0','0','0']),}
    

    侧面:

    来自开发者谷歌博客的这个伟大的article 可能是一个很好的读物,因为它介绍了一种使用tf.Dataset API 直接从csv 文件创建input_fn 的新方法。它具有更好的内存管理,并且避免将所有数据集加载到内存中。

    【讨论】:

      【解决方案2】:

      我有同样的问题,但是当我检查数据库列的名称时,列的名称有一点错误。查看您的专栏名称。

      【讨论】:

        【解决方案3】:

        我遇到了同样的问题。在我的例子中,目标变量也被输入到特征字典中。我从特征字典中删除了它并且它起作用了。

        【讨论】:

          【解决方案4】:

          如果您因为 TF 服务而到达此页面,另一种可能是作为 serving_input_fn 传递的字典中的键与您的模型中的键不对应,请仔细检查字典。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2016-10-11
            • 1970-01-01
            • 1970-01-01
            • 2017-12-05
            • 1970-01-01
            • 2022-12-14
            • 2021-02-24
            • 2018-08-19
            相关资源
            最近更新 更多