Last active
October 16, 2022 13:53
-
-
Save araffin/e15dd89d1f647cdeecb5ea998819ef94 to your computer and use it in GitHub Desktop.
Training, Saving and Loading an A2C agent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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