openai / spinningup

An educational resource to help anyone learn deep reinforcement learning.
https://spinningup.openai.com/
MIT License
10.11k stars 2.22k forks source link

AttributeError: module 'tensorflow._api.v2.train' has no attribute 'AdamOptimizer' #333

Open mmcaulif opened 3 years ago

mmcaulif commented 3 years ago

Attempting the spinning up tutorial using windows and wsl2 by following the link given in the installation tutorial.

After setting up conda and wsl2, I made my conda environment, then followed the installation from there.

When I run the test line:

"python -m spinup.run ppo --hid "[32,32]" --env LunarLander-v2 --exp_name installtest --gamma 0.999"

to check the install i initially received "illegal instruction" so I ran "conda install tensorflow" which I'd seen recommended online.

The installation check no longer returned illegal instruction but now returns:

 "Traceback (most recent call last):
  File "/home/manus/miniconda3/envs/spinningup/lib/python3.6/runpy.py", line 183, in _run_module_as_main
    mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
  File "/home/manus/miniconda3/envs/spinningup/lib/python3.6/runpy.py", line 109, in _get_module_details
    __import__(pkg_name)
  File "/home/manus/spinningup/spinup/__init__.py", line 8, in <module>
    from spinup.algos.tf1.ppo.ppo import ppo as ppo_tf1
  File "/home/manus/spinningup/spinup/algos/tf1/ppo/ppo.py", line 7, in <module>
    from spinup.utils.mpi_tf import MpiAdamOptimizer, sync_all_params
  File "/home/manus/spinningup/spinup/utils/mpi_tf.py", line 29, in <module>
    class MpiAdamOptimizer(tf.train.AdamOptimizer):
AttributeError: module 'tensorflow._api.v2.train' has no attribute 'AdamOptimizer'"

With I'd imagine the important line being the last one.

Google searches come up with an old thread where someone fixes it themselves but with no explanation apart from "user error" and "bad install"

JakobThumm commented 3 years ago

Hi mmcaulif,

I just had the same problem and was able to fix (hack) it. The problem is, that spinningup has two implementations, pytorch and tf1. Pytorch depends on tensorflow==2.4.1, at least that´s what they put in theier setup.py. So I guess, it is either use the pytorch or tf1 implementation. I strongly suspect, that pytorch is the preferred way to go. But here is the bug: in __init__.py they still try to import the tf1 modules

from spinup.algos.tf1.ddpg.ddpg import ddpg as ddpg_tf1
from spinup.algos.tf1.ppo.ppo import ppo as ppo_tf1
from spinup.algos.tf1.sac.sac import sac as sac_tf1
from spinup.algos.tf1.td3.td3 import td3 as td3_tf1
from spinup.algos.tf1.trpo.trpo import trpo as trpo_tf1
from spinup.algos.tf1.vpg.vpg import vpg as vpg_tf1

Despite, we never installed them in the setup.py. When you don't want to use the tf1 implementations, you can simply put a try catch around the imports like this, they are not needed for the pytorch algorithms.

try:
    from spinup.algos.tf1.ddpg.ddpg import ddpg as ddpg_tf1
    from spinup.algos.tf1.ppo.ppo import ppo as ppo_tf1
    from spinup.algos.tf1.sac.sac import sac as sac_tf1
    from spinup.algos.tf1.td3.td3 import td3 as td3_tf1
    from spinup.algos.tf1.trpo.trpo import trpo as trpo_tf1
    from spinup.algos.tf1.vpg.vpg import vpg as vpg_tf1
except:
    print("TF1 module not imported")

Note: This is more like a hack than a proper solution. You might either:

  1. Include tf1 in the setup.py (I don't know if you can have tf1 and tf2 in parallel?)
  2. Write a proper check which version should be used in the __init__.py

I hope, this was a help.