问题
I am attempting to write a simple deep machine learning model using TensorFlow. I'm using a toy dataset I made up in Excel just to get the model working and accepting data. My code is as follows:
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)
Unfortunately, I am getting the following error. I have tried trimming the labels off the csv file versus leaving them, naming the feature columns different things, and changing the type of the numpy array. The error persists.
ValueError: Feature h_plus_ind is not in features dictionary.
If I remove h_plus_ind
, it simply throws the error on a different column.
回答1:
When using tf.feature_columns
, the data you feed in your input_fn should have the same keys as the feature columns previously created.
So, the x
of your train_input_fn
should be a dictionary, with keys named after the feature_columns
.
A mock example :
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']),}
On the side :
This great article from the developers google blog could be a great read, as it introduces a new way to create input_fn
directly from a csv file with the tf.Dataset
API. It has a better memory management, and avoid loading all the dataset into memory.
回答2:
I have the same problem but when i checked the names of my columns of database, there was a little mistake in the name of column. Check out your column's names.
回答3:
I have faced the same problem.In my case, the target variable was also fed to the features dictionary.I removed it from features dictionary and it worked.
回答4:
If you've reached this page because of TF serving, another possibility is that the keys in the dictionary passed as serving_input_fn
do not correspond to the ones in your model, just double-check the dict.
来源:https://stackoverflow.com/questions/47635507/valueerror-feature-not-in-features-dictionary