TensorFlow estimator.DNNClassifier:export_savedmodel导致“ ValueError:无效功能”

时间:2018-08-29 13:49:40

标签: python tensorflow tensorflow-estimator

我读了很多话题,但是没有一个答案对我有帮助...

我有DNN分类器:

dataTable

DataFrame X_train包含452个数字列(其中大多数-由OneHodEncode虚拟列转换):形状为(84692,452)。 len(feature_columns)= 452

但是当我尝试使用脚本保存模型时:

import tensorflow as tf
feature_columns = []
for key in X_train.keys():
    feature_columns.append(tf.feature_column.numeric_column(key=key))

classifier = tf.estimator.DNNClassifier(
    feature_columns=feature_columns,
    hidden_units=[10, 20, 10],
    n_classes=2
    )

def train_input_fn(features, labels, batch_size):
    """An input function for training"""
    dataset = tf.data.Dataset.from_tensor_slices((dict(features), labels))
    dataset = dataset.shuffle(10).repeat().batch(batch_size)
    return dataset

#train the Model
batch_size = 100
train_steps = 400

for i in range(0,100):
    classifier.train(
        input_fn=lambda:train_input_fn(X_train, y_train, batch_size),
        steps=train_steps
        )

我遇到错误:

  

ValueError:无效的功能dummy_feature_N_value_M:0。

还尝试使用其他脚本进行保存(但在这里我并不了解每个参数值...):

def serving_input_receiver_fn():
    feature_spec = tf.feature_column.make_parse_example_spec(feature_columns)
    return tf.estimator.export.build_parsing_serving_input_receiver_fn(feature_spec)()

classifier.export_savedmodel(export_dir_base="export_model/", serving_input_receiver_fn=_serving_input_receiver_fn)

但是它还会返回几乎错误:

  

ValueError:功能 dummy_feature_N_value_M 不在功能字典中。

当我查看 feature_columns 列表时-是否存在:

  

_NumericColumn(key ='dummy_feature_N_value_M',shape =(1,),default_value = None,dtype = tf.float32,normalizer_fn = None),

我做错了什么?

1 个答案:

答案 0 :(得分:0)

不知道那是什么...但是现在一切正常。

首先,我尝试不使用自己创建的OneHodEncode虚拟列,而是输入带有分类列的初始数据框“ train_dummy_features”:

# split columns and indexes of categorical and continues columns
categorical_columns = list(train_dummy_features.select_dtypes(include=['category','object']))
print(categorical_columns)
numeric_columns = list(train_dummy_features.select_dtypes(include=['int','uint8']))
print(numeric_columns)
cat_features_indexes = [train_dummy_features.columns.get_loc(c) for c in train_dummy_features.columns if c in categorical_columns] 
print(cat_features_indexes)
continues_features_indexes = [train_dummy_features.columns.get_loc(c) for c in train_dummy_features.columns if c not in categorical_columns] 
print(continues_features_indexes)

然后使用TensorFlow函数创建feature_columns列表:

numeric_features = [tf.feature_column.numeric_column(key = column) for column in numeric_columns]
print(numeric_features)
categorical_features = [
    tf.feature_column.embedding_column(
        categorical_column = tf.feature_column.categorical_column_with_vocabulary_list
                             (key = column
                              , vocabulary_list = train_dummy_features[column].unique()
                             ),
        dimension = len(train_dummy_features[column].unique())
        ) 
    for column in categorical_columns
    ]
print(categorical_features[3])

feature_columns = numeric_features + categorical_features
feature_columns[2]

并将带有分类列的初始数据框“ train_dummy_features”放入X_train:

X = train_dummy_features
y = train_measure # since we already have dataframe with the measure

X_train, y_train = X, y

已声明的初始分类训练的分类器中指定了“分类器”和“ train_input_fn”。

之后

def serving_input_receiver_fn():
    #feature_spec = {INPUT_TENSOR_NAME: tf.FixedLenFeature(dtype=tf.float32, shape=[452])}
    feature_spec = tf.feature_column.make_parse_example_spec(feature_columns)
    return tf.estimator.export.build_parsing_serving_input_receiver_fn(feature_spec)()

classifier.export_savedmodel(export_dir_base="export_model2/", serving_input_receiver_fn=serving_input_receiver_fn)

def serving_input_receiver_fn():
    serialized_tf_example = tf.placeholder(dtype=tf.string, shape=[None], name='input_tensors')
    receiver_tensors      = {"predictor_inputs": serialized_tf_example}
    feature_spec          = tf.feature_column.make_parse_example_spec(feature_columns) #{"words": tf.FixedLenFeature([len(feature_columns)],tf.float32)}
    features              = tf.parse_example(serialized_tf_example, feature_spec)
    return tf.estimator.export.ServingInputReceiver(features, receiver_tensors)

classifier.export_savedmodel(export_dir_base="export_model3/", serving_input_receiver_fn=serving_input_receiver_fn)

成功导出了模型。

我尝试重复昨天导致错误的步骤的第一版-但现在无法重复该错误。

因此,所描述的步骤已成功训练并导出tf.estimator.DNNClassifier模型