如何使用训练有素的估计器在张量流中进行预测

时间:2021-04-25 09:03:16

标签: tensorflow machine-learning tensorflow-estimator

我试图使用训练有素的估计器对一些测试数据进行预测,但我的预测结果不好,您能检查一下我的代码吗?许多 THX!

这是下面的代码:

//create the estimator
model = tf.estimator.LinearClassifier(
  n_classes = 2,
  model_dir = "ongoing",
  feature_columns = categorical_features + continuous_features
)

FEATURES = ['Age', 'Gender', 'ICD9Code']
LABEL = 'Condition'

//create the input function
def get_input_fn(data_set, num_epochs, n_batch, shuffle):
    input = tf.compat.v1.estimator.inputs.pandas_input_fn(
       x = pd.DataFrame({k: data_set[k].values for k in FEATURES}),
       y = pd.Series(data_set[LABEL].values),
       batch_size = n_batch,
       num_epochs = num_epochs,
       shuffle = shuffle
     )
    return input

//train the estimator
model.train(
  input_fn = get_input_fn(csv_data, num_epochs = None, n_batch = 10461, shuffle = False
  ),
  steps = 1000
)
//create the prediction input function
predict_input_fn = tf.compat.v1.estimator.inputs.numpy_input_fn(
                          x = {k: df[k].values for k in FEATURES}, //df is the test data set
                          y = None,
                      batch_size = 1,
                          num_epochs = 1,
                          shuffle = False,
                          num_threads = 1
                       )
 predict_results = model.predict(predict_input_fn)
 print(row_pre, next(predict_results))

0 个答案:

没有答案
相关问题