import gym
from stable_baselines import DQN,PPO2
from stable_baselines.common.evaluation import evaluate_policy
env=gym.make('LunarLander-v2')
model=DQN('MlpPolicy',env,learning_rate=1e-3,prioritized_replay=True,verbose=1)
model.learn(total_timesteps=int(2e5))
model.save('dqn_lunar')
del model
model=DQN.load('dqn_lunar')
mean_reward,std_reward=evaluate_policy(model,model.get_env(),n_eval_episodes=10)
obs=env.reset()
for i in range(1000):
action,_state=model.predict(obs)
obs,rewards,dones,info=env.step(action)
env.render()
Describe the bug
Traceback (most recent call last):
File "", line 1, in
File "C:\Program Files\JetBrains\PyCharm 2020.2\plugins\python\helpers\pydev_pydev_bundle\pydev_umd.py", line 197, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
File "C:\Program Files\JetBrains\PyCharm 2020.2\plugins\python\helpers\pydev_pydev_imps_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"n", file, 'exec'), glob, loc)
File "C:/Users/born_/PycharmProjects/test_sb/main.py", line 41, in
mean_reward,std_reward=evaluate_policy(model,model.get_env(),n_eval_episodes=10)
File "C:\Users\born_\anaconda3\envs\workEnv\lib\site-packages\stable_baselines\common\evaluation.py", line 49, in evaluate_policy
obs = env.reset()
AttributeError: 'NoneType' object has no attribute 'reset'
System Info
Hello,
let me give you a hint: when you load the model as you do, there is normally a warning that will explain you why it does not work ;)
Loading a model without an environment, this model cannot be trained until it has a valid environment.
It should be this sentence?
I have tried model.get_env() command, but it returns a None. Why?
Model does not contain an environment, you need to provide one yourself. See docs on load.
Loading a model without an environment, this model cannot be trained until it has a valid environment.
It should be this sentence?
yes
I have tried model.get_env() command, but it returns a None. Why?
As mentioned by the warning and by @Miffyli the model is without an environment (not saved).
OK,thank you! I have succeed with your help.