Skip to content

Instantly share code, notes, and snippets.

@araffin
Last active October 16, 2022 13:53
Show Gist options
  • Save araffin/e15dd89d1f647cdeecb5ea998819ef94 to your computer and use it in GitHub Desktop.
Save araffin/e15dd89d1f647cdeecb5ea998819ef94 to your computer and use it in GitHub Desktop.
Training, Saving and Loading an A2C agent
import gym
from stable_baselines import A2C
from stable_baselines.common.policies import MlpPolicy
from stable_baselines.common.vec_env import DummyVecEnv
# Create and wrap the environment
env = gym.make('LunarLander-v2')
env = DummyVecEnv([lambda: env])
model = A2C(MlpPolicy, env, ent_coef=0.1, verbose=1)
# Train the agent
model.learn(total_timesteps=100000)
# Save the agent
model.save("a2c_lunar")
del model # delete trained model to demonstrate loading
# Load the trained agent
model = A2C.load("a2c_lunar")
# Enjoy trained agent
obs = env.reset()
for i in range(1000):
action, _states = model.predict(obs)
obs, rewards, dones, info = env.step(action)
env.render()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment