0

I am trying to install stable-baselines and run the first two lines from Getting Started section of the online manual but no option is working. I started with

pip install stable-baselines

Now when I run:

import gym
from stable_baselines.common.policies import MlpPolicy

I get

No module named 'tensorflow.contrib'

This apparently is because tensorflow version 2 doesn't have tensorflow.contrib. But version 2 was released in Sept 2019. Do I really have to use only tensorflow version 1?

What is the right way to install stable-baselines and run that simple example?


I tried

pip install stable-baselines3 

in a virtual environment. This gives a different error:

In [2]: from stable_baselines.common.policies import MlpPolicy
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Input In [2], in <module>
----> 1 from stable_baselines.common.policies import MlpPolicy

ModuleNotFoundError: No module named 'stable_baselines'

In [3]: from stable_baselines3.common.policies import MlpPolicy
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
Input In [3], in <module>
----> 1 from stable_baselines3.common.policies import MlpPolicy

ImportError: cannot import name 'MlpPolicy' from 'stable_baselines3.common.policies' (/home/raph/RL/stable-baselines/venv/lib/python3.8/site-packages/stable_baselines3/common/policies.py)
Simd
  • 19,447
  • 42
  • 136
  • 271

2 Answers2

4

To quote the github readme:

Note: Stable-Baselines supports Tensorflow versions from 1.8.0 to 1.14.0. Support for Tensorflow 2 API is planned.

The same github readme also recommends to use stable-baselines3, as stable-baselines is currently only being maintained and its functionality is not extended. Thus, I would not expect the TF1 -> TF2 update any time soon.

If you can not install this version of tensorflow, I suggest to use stable-baselines3 and follow the examples. The code you posted above is not consistent with the stable-baselines3 docs, which import the MlpPolicy as

from stable_baselines3.sac.policies import MlpPolicy
André
  • 1,034
  • 9
  • 19
  • (As above comment) You can't install tensorflow 1.x with pip any more so this is a real pain. – Simd Jan 13 '22 at 12:14
  • @graffe answer has been edited to address the modified question – André Jan 13 '22 at 12:34
  • Thank you! It's a really annoying the way the dependencies aren't set correctly. I tried the Lunar Landing example from https://stable-baselines3.readthedocs.io/en/master/guide/examples.html which failed until I separately did `pip install wheel`. – Simd Jan 13 '22 at 13:42
0

According to the stable-baselines documentation you can only use Tensorflow version 1.8.0 to version 1.15.0.

If you want to run Tensorflow 1, and you want to use pip as your package manager, you'll need to install python version 3.7 or lower. I did the following in my ubuntu terminal

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
sudo apt install python3.7
sudo apt install virtualenv
virtualenv --python=/usr/bin/python3.7 <env-name>
source <env-name>/bin/activate
pip install tensorflow==1.15.0
...

Alternatively, you could try using this guide, which gives instructions as to how to migrate something to Tensorflow version 2.

tionichm
  • 163
  • 10