Repository: optuna/optuna

Issue:
TensorFlow examples do not work with TensorFlow==2.0.0.
**Conditions**
- Optuna version: 0.16.0
- Python version: 3.7.2
- OS: macOS 10.13.6
- Machine Learning library to be optimized: TensorFlow 2.0.0

**Code to reproduce**

`examples/tensorflow_*` and `examples/pruning/tensorflow_*` do not work with TensorFlow==2.0.0.

```
$ python examples/tensorflow_estimator_simple.py

Traceback (most recent call last):
  File "examples/tensorflow_estimator_simple.py", line 137, in <module>
    tf.app.run()
AttributeError: module 'tensorflow' has no attribute 'app'
```

```
$ python examples/tensorflow_eager_simple.py
Traceback (most recent call last):
  File "examples/tensorflow_eager_simple.py", line 22, in <module>
    import tensorflow.contrib.eager as tfe
ModuleNotFoundError: No module named 'tensorflow.contrib'
```

Related PR #564.


Discussion:

Resolving patch:
diff --git a/examples/tensorflow_eager_simple.py b/examples/tensorflow_eager_simple.py
--- a/examples/tensorflow_eager_simple.py
+++ b/examples/tensorflow_eager_simple.py
@@ -18,17 +18,20 @@
 
 """
 
+import pkg_resources
 import tensorflow as tf
 from tensorflow.keras.datasets import mnist
 
 import optuna
 
+if pkg_resources.parse_version(tf.__version__) < pkg_resources.parse_version('2.0.0'):
+    raise RuntimeError('tensorflow>=2.0.0 is required for this example.')
+
 N_TRAIN_EXAMPLES = 3000
 N_TEST_EXAMPLES = 1000
 BATCHSIZE = 128
 CLASSES = 10
 EPOCHS = 1
-tf.enable_eager_execution()
 
 
 def create_model(trial):
@@ -51,25 +54,25 @@ def create_model(trial):
 def create_optimizer(trial):
     # We optimize the choice of optimizers as well as their parameters.
     kwargs = {}
-    optimizer_options = ['RMSPropOptimizer', 'AdamOptimizer', 'MomentumOptimizer']
+    optimizer_options = ['RMSprop', 'Adam', 'SGD']
     optimizer_selected = trial.suggest_categorical('optimizer', optimizer_options)
-    if optimizer_selected == 'RMSPropOptimizer':
+    if optimizer_selected == 'RMSprop':
         kwargs['learning_rate'] = trial.suggest_loguniform('rmsprop_learning_rate', 1e-5, 1e-1)
         kwargs['decay'] = trial.suggest_uniform('rmsprop_decay', 0.85, 0.99)
         kwargs['momentum'] = trial.suggest_loguniform('rmsprop_momentum', 1e-5, 1e-1)
-    elif optimizer_selected == 'AdamOptimizer':
+    elif optimizer_selected == 'Adam':
         kwargs['learning_rate'] = trial.suggest_loguniform('adam_learning_rate', 1e-5, 1e-1)
-    elif optimizer_selected == 'MomentumOptimizer':
-        kwargs['learning_rate'] = trial.suggest_loguniform('momentum_opt_learning_rate', 1e-5,
+    elif optimizer_selected == 'SGD':
+        kwargs['learning_rate'] = trial.suggest_loguniform('sgd_opt_learning_rate', 1e-5,
                                                            1e-1)
-        kwargs['momentum'] = trial.suggest_loguniform('momentum_opt_momentum', 1e-5, 1e-1)
+        kwargs['momentum'] = trial.suggest_loguniform('sgd_opt_momentum', 1e-5, 1e-1)
 
-    optimizer = getattr(tf.train, optimizer_selected)(**kwargs)
+    optimizer = getattr(tf.optimizers, optimizer_selected)(**kwargs)
     return optimizer
 
 
 def learn(model, optimizer, dataset, mode='eval'):
-    accuracy = tf.contrib.eager.metrics.Accuracy('accuracy', dtype=tf.float32)
+    accuracy = tf.metrics.Accuracy('accuracy', dtype=tf.float32)
 
     for batch, (images, labels) in enumerate(dataset):
         with tf.GradientTape() as tape:
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -92,9 +92,7 @@ def get_extras_require():
             'keras',
             'pytorch-ignite',
             'pytorch-lightning',
-            # TODO(Yanase): Update examples to support TensorFlow 2.0.
-            # See https://github.com/optuna/optuna/issues/565 for further details.
-            'tensorflow<2.0.0',
+            'tensorflow>=2.0.0',
             'torch',
             'torchvision>=0.5.0'
         ] if sys.version_info[:2] < (3, 8) else []),


Test patch: