12

Some months ago, I used the tf.contrib.learn.DNNRegressor API from TensorFlow, which I found very convenient to use. I didn't keep up with the development of TensorFlow the last few months. Now I have a project where I want to use a Regressor again, but with more control over the actual model as provided by DNNRegressor. As far as I can see, this is supported by the Estimator API using the model_fn parameter.

But there are two Estimators in the TensorFlow API:

  • tf.contrib.learn.Estimator
  • tf.estimator.Estimator

Both provide a similar API, but are nevertheless slightly different in their usage. Why are there two different implementations and are there reasons to prefer one?

Unfortunately, I can't find any differences in the TensorFlow documentation or a guide when to use which of both. Actually, working through the TensorFlow tutorials produced a lot of warnings as some of the interfaces apparently have changed (instead of the x,y parameter, the input_fn parameter et cetera).

Morpheus1822
  • 121
  • 1
  • 4

4 Answers4

7

I wondered the same and cannot give a definitive answer, but I have a few educated guesses that might help you:

It seems that tf.estimator.Estimator together with a model function that returns tf.estimator.EstimatorSpec is the most current one that is used in the newer examples and the one to be used in new code.

My guess now is that the tf.contrib.learn.Estimator is an early prototype that got replaced by the tf.estimator.Estimator. According to the docs everything in tf.contrib is unstable API that may change at any time and it looks like the tf.estimator module is the stable API that “evolved” from the tf.contrib.learn module. I assume that the authors just forgot to mark tf.contrib.learn.Estimator as deprecated and that it wasn't removed yet so existing code won't break.

Christoph Henkelmann
  • 1,437
  • 1
  • 12
  • 17
3

Now there is this explicit statement in the docs:

Note: TensorFlow also includes a deprecated Estimator class at tf.contrib.learn.Estimator, which you should not use.

https://www.tensorflow.org/programmers_guide/estimators

enter image description here

For some reason it's not marked Deprecated in code.

Pekka
  • 2,348
  • 2
  • 21
  • 33
1

To add to Christoph's answer.

The distinction between these packages has been specifically mentioned at Tensorflow Dev Summit 2017 by Martin Wicke:

The distinction between core and contrib is really in core things don't change. Things are backward compatible until release 2.0, and nobody's thinking about that right now.

If you have something in core, it's stable, you should use it. If you have something in contrib, the API may change and depending on your needs you may or may not want to use it.

So you can think of tf.contrib package as "experimental" or "early preview". For classes that are already in both tf.estimator and tf.contrib, you should definitely use tf.estimator version, because tf.contrib class gets deprecated automatically (even if it's not stated explicitly in the documentation) and can be dropped in the next release.

As of tensorflow 1.4 the list of "graduated" classes includes: Estimator DNNClassifier, DNNRegressor, LinearClassifier, LinearRegressor, DNNLinearCombinedClassifier, DNNLinearCombinedRegressor. These should be ported to tf.estimator.

Maxim
  • 52,561
  • 27
  • 155
  • 209
0

I had the same question about to ask.

I guess tf.estimator.Estimator is high level interface and recommended usage while tf.contrib.learn.Estimator is so said not high level interface (but it is indeed).

As Christoph mentioned, tf.contrib is unstable so tf.contrib.learn.Estimator is vulnerable to changes. It is changed from 0.x versions to 1.1 version and changed again in 2016.12. The problem is, the usage of them seems different. You can use tf.contrib.learn.SKCompat to wrap tf.contrib.learn.Estimator while for tf.estimator.Estimator, you can't do the same thing. And model_fn requirement/parameter is different if you check error messages.

The conclusion is that this two Estimator is different thing!

Anyway, I think tf doc did very bad on this topic since tf.estimator is in their tutorial page which means they are very serious about this...

user2189731
  • 558
  • 8
  • 15