Skip to content

Commit

Permalink
Change tf trainer import (#2271)
Browse files Browse the repository at this point in the history
* Remove tensorflow import for module

* Fix import
  • Loading branch information
ziyiwu9494 authored May 6, 2021
1 parent 90b6090 commit fa0cd08
Showing 1 changed file with 7 additions and 21 deletions.
28 changes: 7 additions & 21 deletions src/garage/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,7 @@
from garage.experiment.experiment import dump_json
from garage.experiment.snapshotter import Snapshotter

# pylint: disable=no-name-in-module

tf = False
try:
import tensorflow as tf
except ImportError:
pass
tf = None


class ExperimentStats:
Expand Down Expand Up @@ -536,6 +530,7 @@ class NotSetupError(Exception):
"""Raise when an experiment is about to run without setup."""


# pylint: disable=no-member
class TFTrainer(Trainer):
"""This class implements a trainer for TensorFlow algorithms.
Expand Down Expand Up @@ -590,6 +585,11 @@ class TFTrainer(Trainer):
"""

def __init__(self, snapshot_config, sess=None):
# pylint: disable=import-outside-toplevel
import tensorflow
# pylint: disable=global-statement
global tf
tf = tensorflow
super().__init__(snapshot_config=snapshot_config)
self.sess = sess or tf.compat.v1.Session()
self.sess_entered = False
Expand Down Expand Up @@ -663,17 +663,3 @@ def initialize_tf_vars(self):
v for v in tf.compat.v1.global_variables()
if v.name.split(':')[0] in uninited_set
]))


class __FakeTFTrainer:
# noqa: E501; pylint: disable=missing-param-doc,too-few-public-methods,no-method-argument
"""Raises an ImportError for environments without TensorFlow."""

def __init__(*args, **kwargs):
raise ImportError(
'TFTrainer requires TensorFlow. To use it, please install '
'TensorFlow.')


if not tf:
TFTrainer = __FakeTFTrainer # noqa: F811

0 comments on commit fa0cd08

Please sign in to comment.