将Tensorflow估算器转换为SavedModel时出错

时间:2019-12-16 14:52:18

标签: python tensorflow tensorflow-estimator

我成功地训练了TensowFlow增强树估计器。现在,我想将其另存为SavedModel。问题是我得到下面的错误。 ValueError: All feature_columns must be _FeatureColumn instances. Given: [NumericColumn(key='fcoeffvariation_Result', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None), NumericColumn(key='fcountabove2sigma_Result', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None)]

下面是我的代码。有人看到了什么问题吗? measurementData variabele是熊猫数据框。特征fcoeffvariation_Result, fcountabove2sigma_Result是浮点特征,标签特征是布尔值(0或1)。

measurementData = measurementData[['fcoeffvariation_Result', 'fcountabove2sigma_Result', 'label']]

df = measurementData.copy()

df_features = measurementData.copy()

#Delete target feature from dataframe
del df_features['label']

# Spiting the data to train and test
X_feature = df_features.copy()
Y_label = df['label'].copy()
X_feature_train, X_feature_test, Y_feature_train, Y_feature_test = train_test_split(X_feature, Y_label, test_size=0.3)

############################Create input functions
# Create a input function to train the model
input_func_train = tf.estimator.inputs.pandas_input_fn(x=X_feature_train,y=Y_feature_train, batch_size=50,shuffle=True)
# Create a input function to evaluate the model after train
input_func_test = tf.estimator.inputs.pandas_input_fn(x=X_feature_test,  y=Y_feature_test, batch_size=50,shuffle=False)
# Create a input function for prediction
input_func_prediction = tf.estimator.inputs.pandas_input_fn(x=X_feature_test,y=Y_feature_test, batch_size=50,shuffle=False)

###########################Feature Columns
my_feature_columns = [tf.feature_column.numeric_column(key=key)
                   for key in X_feature_train.keys()]

###########################Train model
linear_est = tf.estimator.LinearClassifier(my_feature_columns)
linear_est.train(input_fn=input_func_train, max_steps=100)

###################################Convert to savedmodel
serving_input_fn = tf.estimator.export.build_parsing_serving_input_receiver_fn(
  tf.feature_column.make_parse_example_spec([my_feature_columns]))
export_path = linear_est.export_saved_model(
  "path", serving_input_fn)




1 个答案:

答案 0 :(得分:0)

您需要将功能转换为Tensorflow可以接受的格式。在拟合模型之前,尝试将列转换为要素列。此处更多信息:https://www.tensorflow.org/tutorials/structured_data/feature_columns