<jupyter_start><jupyter_text>Task 4: Actor-Critic test best A2C models on CartPole-v0<jupyter_code>import numpy as np

from IPython.display import clear_output

import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
sns.set_style("whitegrid")

import torch
import torch.nn as nn

# Check for CUDA
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

# Reproducible results
torch.manual_seed(0)

import gym

from actor_critic import ActorCritic
import os
def sample_one_episode(env: gym.Env, model: ActorCritic):
    state = env.reset()
    done = False

    total_reward = 0

    while not done:
        state = torch.unsqueeze(torch.FloatTensor(state), 0).to(device)
        probability_dist, values = model(state)
        action_to_take = probability_dist.sample()
        next_state, reward, done, _ = env.step(action_to_take.cpu().detach().numpy()[0])
        state = next_state

        total_reward += reward

    return total_reward<jupyter_output><empty_output><jupyter_text>Test the CartPole-v0 best model on CartPole-v0<jupyter_code># Load
PATH = "results/CartPole-v0/best_model/num_env24-num_episodes100000-learning_rate0.002-hidden_layers128-128.pt"

model_load = torch.load(PATH)
model_load.eval()

model_load.state_dict()
model_load_env = gym.make("CartPole-v0")
state = model_load_env.reset()
model_load_env.seed(0)

model_load_total_rewards = []

for _ in range(100):
    model_load_rewards = sample_one_episode(env=model_load_env,model=model_load)
    model_load_total_rewards.append(model_load_rewards)
    
model_load_total_rewards

plt.title(f"Best A2C v0 model test");
plt.xlabel("Episodes");
plt.ylabel("Reward");
plt.ylim([0, 200]);
plt.xlim([0, 100]);
plt.plot(model_load_total_rewards);
plt.savefig("results/CartPole-v0/best_model/best_model_rewards.png", dpi=300, bbox_inches="tight");
plt.show();
min(model_load_total_rewards), max(model_load_total_rewards), np.std(model_load_total_rewards), np.mean(model_load_total_rewards)<jupyter_output><empty_output><jupyter_text>Test the CartPole-v1 best model on CartPole-v0<jupyter_code># Load
PATH = "results/CartPole-v1/best_model/num_env24-num_episodes100000-learning_rate0.003-hidden_layers128-128.pt"

model_load = torch.load(PATH)
model_load.eval()

model_load.state_dict()
model_load_env = gym.make("CartPole-v0")
state = model_load_env.reset()
model_load_env.seed(0)

model_load_total_rewards = []

for _ in range(100):
    model_load_rewards = sample_one_episode(env=model_load_env,model=model_load)
    model_load_total_rewards.append(model_load_rewards)
    
model_load_total_rewards

plt.title(f"Best A2C v0 model test");
plt.xlabel("Episodes");
plt.ylabel("Reward");
plt.ylim([0, 200]);
plt.xlim([0, 100]);
plt.plot(model_load_total_rewards);
plt.savefig("results/CartPole-v0/best_model/best_model_rewards_v1.png", dpi=300, bbox_inches="tight");
plt.show();
min(model_load_total_rewards), max(model_load_total_rewards), np.std(model_load_total_rewards), np.mean(model_load_total_rewards)<jupyter_output><empty_output><jupyter_text>Test the best CartPole-v0 model on CartPole-v1<jupyter_code># Load
PATH = "results/CartPole-v0/best_model/num_env24-num_episodes100000-learning_rate0.002-hidden_layers128-128.pt"

model_load = torch.load(PATH)
model_load.eval()

model_load.state_dict()
model_load_env = gym.make("CartPole-v1")
state = model_load_env.reset()
model_load_env.seed(0)

model_load_total_rewards = []

for _ in range(100):
    model_load_rewards = sample_one_episode(env=model_load_env,model=model_load)
    model_load_total_rewards.append(model_load_rewards)
    
model_load_total_rewards

plt.title(f"Best A2C v0 model test");
plt.xlabel("Episodes");
plt.ylabel("Reward");
plt.ylim([0, 500]);
plt.xlim([0, 100]);
plt.plot(model_load_total_rewards);
plt.savefig("results/CartPole-v0/best_model/best_model_rewards_v0_on_v1.png", dpi=300, bbox_inches="tight");
plt.show();
min(model_load_total_rewards), max(model_load_total_rewards), np.std(model_load_total_rewards), np.mean(model_load_total_rewards)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12121212121212122, 'fraction_numerical': 0.03202759300320276, 'mean_word_length': 6.837837837837838, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '556233', 'license': 'MIT', 'n_tokens_mistral': 1680, 'n_tokens_neox': 1567, 'n_words': 223, 'path': 'actor_critic_test_v0.ipynb', 'repo_name': 'EduardErnest/Deep-Learning-INM707', 'size': '4059'}