Search code examples
pythontensorflowlogistic-regression

coefficients of logistic regression model in tensorflow


I'm following this tutorial to build a logistic regression model using TensorFlow. I have a simple question: How can I print the model's coefficients like .coef_ in sklearn?

Piece of code after preprocessing and before training the model:

model_dir = tempfile.mkdtemp()
m = tf.contrib.learn.LinearClassifier(feature_columns=[
  gender, native_country, education, occupation, workclass, marital_status, race,
  age_buckets, education_x_occupation, age_buckets_x_education_x_occupation],
  model_dir=model_dir)
m.fit(input_fn=train_input_fn, steps=200)
results = m.evaluate(input_fn=eval_input_fn, steps=1)
for key in sorted(results):
    print("%s: %s" % (key, results[key]))

and here is the results of ls -l on model_dir:

-rw-r--r--  1 builder  staff       305 Jul 17 13:29 checkpoint
drwxr-xr-x  3 builder  staff       102 Jul 17 13:29 eval
-rw-r--r--  1 builder  staff  39038807 Jul 17 13:29 events.out.tfevents.1500323287.builder-MBP.ikuni.com
-rw-r--r--  1 builder  staff  32604586 Jul 17 13:28 graph.pbtxt
-rw-r--r--  1 builder  staff  12567520 Jul 17 13:28 model.ckpt-1.data-00000-of-00001
-rw-r--r--  1 builder  staff      3240 Jul 17 13:28 model.ckpt-1.index
-rw-r--r--  1 builder  staff   9830014 Jul 17 13:28 model.ckpt-1.meta
-rw-r--r--  1 builder  staff  12567520 Jul 17 13:29 model.ckpt-200.data-00000-of-00001
-rw-r--r--  1 builder  staff      3240 Jul 17 13:29 model.ckpt-200.index
-rw-r--r--  1 builder  staff   9830014 Jul 17 13:29 model.ckpt-200.meta

Solution

  • You can use the two methods: get_variable_names() to get the variables in the model and then use get_variable_value() to get the coefficients of a particular variable of the trained model.