<jupyter_start><jupyter_text>Training Loop  跑一个CartPole都要这么久，也不知道写得靠不靠谱。。。<jupyter_code>name = 'CartPole-v0'
env_eval = gym.make(name)
env_eval.seed(seed)

config = Config()
config.lr = 1e-3

start = timer()
running_returns = []
agent_a2c = A2CAgent(env_name=name, policy=A2CCategoricalNetwork, config=config)

for episode in count(1): # 一直加1的while, 表示一条episode
    # print('episode%d'%episode)
    obs, ep_return = agent_a2c.env.reset(), 0
    agent_a2c.rewards.append([])                # record rewards separately for each episode
    agent_a2c.log_probs_values.append([])
    
    for step in range(10000):
        action = agent_a2c.get_action(obs)
        obs, reward, done, _ = agent_a2c.env.step(action)
        agent_a2c.rewards[-1].append(reward)
        ep_return += reward
        if done:
            running_returns.append(ep_return)
            break
            
    
    if episode % 20 == 0:          # 每20条轨迹进行一次梯度更新，当然也可以设置成1，就相当与mini-batch和stochastic gradient descent的区别
        agent_a2c.train()
    
    
    if episode % 20 == 0:
        clear_output(True)
        plt.plot(pd.Series(running_returns).rolling(100, 20).mean())
        plt.title('episide:{}, time:{}, returns'.format(episode, timedelta(seconds=int(timer()-start))))
        plt.show()
    if np.array(running_returns)[-20:].mean() > 195:
        eval_return = agent_a2c.eval_(env_eval, 100)
        if eval_return > 195:
            print("Solved! eval return is now {}!".format(eval_return))
            break 
eval_return = agent_a2c.eval_(env_eval, 100)
print(eval_return)
torch.save(agent_a2c.policy.state_dict(), './A2C_cartpole.pt')
agent_a2c.render(env_eval)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12826603325415678, 'fraction_numerical': 0.032660332541567694, 'mean_word_length': 3.201995012468828, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '222963', 'license': 'MIT', 'n_tokens_mistral': 684, 'n_tokens_neox': 640, 'n_words': 97, 'path': 'pytorch/Advantage_Actor_Critic(A2C)/A2C.ipynb', 'repo_name': 'leyuanheart/Reinforcement_Learning_Algorithms', 'size': '1684'}