Search code examples
pythontensorflowsvmtflearn

How tensorflow tf.contrib.learn.SVM reload trained model and use predict to classify new data


Training svm model with tensorflow tf.contrib.learn.SVM and saveing model; the codes

feature_columns = [tf.contrib.layers.real_valued_column(feat) for feat in self.feature_columns]
model_dir = os.path.join(define.root, 'src', 'static_data', 'svm_model_dir')
model = svm.SVM(example_id_column='example_id',
                feature_columns=feature_columns,
                 model_dir=model_dir,
                            config=tf.contrib.learn.RunConfig(save_checkpoints_secs=10))
model.fit(input_fn=lambda: self.input_fun(self.df_train), steps=10000)
results = model.evaluate(input_fn=lambda: self.input_fun(self.df_test), steps=5, metrics=validation_metrics)
for key in sorted(results):
    print('% s: % s' % (key, results[key]))

hwo to reload trained model and use predict to classify new data?


Solution

  • When training

    You call svm.SVM(..., model_dir) and then call the fit() and evaluate() method.

    When testing

    You call svm.SVM(..., model_dir) and then can call predict() methods. Your model will find a trained model in the model_dir and will load the trained model params.

    Reference

    Issue #3340 of TF