<jupyter_start><jupyter_text>1. Test Random Environment with OpenAI Gym<jupyter_code>import random
import gym
env = gym.make('CartPole-v0')
states = env.observation_space.shape[0]
actions = env.action_space.n
states
actions
episodes = 10
for episode in range(1, episodes + 1): 
    state = env.reset()
    done = False
    score = 0
    
    while not done:
#         env.render()
        action = random.choice([0, 1]) # random step, 0 and 1 correspond to left and right
        n_state, reward, done, info = env.step(action)
        score += reward
        
    print(f"Episode: {episode}, Score: {score}")<jupyter_output>Episode: 1, Score: 33.0
Episode: 2, Score: 21.0
Episode: 3, Score: 17.0
Episode: 4, Score: 11.0
Episode: 5, Score: 29.0
Episode: 6, Score: 30.0
Episode: 7, Score: 13.0
Episode: 8, Score: 27.0
Episode: 9, Score: 37.0
Episode: 10, Score: 11.0<jupyter_text>2. Create DL Model with Keras<jupyter_code>import numpy as np
import tensorflow
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras.optimizers import Adam
def build_model(states, actions):
    model = tensorflow.keras.Sequential()
    model.add(Flatten(input_shape=(1, states)))
    model.add(Dense(24, activation='relu'))
    model.add(Dense(24, activation='relu'))
    model.add(Dense(actions, activation='linear'))
    return model
model = build_model(states, actions)
model.summary()<jupyter_output>Model: "sequential_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
flatten_1 (Flatten)          (None, 4)                 0         
_________________________________________________________________
dense_3 (Dense)              (None, 24)                120       
_________________________________________________________________
dense_4 (Dense)              (None, 24)                600       
_________________________________________________________________
dense_5 (Dense)              (None, 2)                 50        
=================================================================
Total params: 770
Trainable params: 770
Non-trainable params: 0
_________________________________________________________________<jupyter_text>3. Build Agent with Keras-RL<jupyter_code>from rl.agents import DQNAgent
from rl.policy import BoltzmannQPolicy
from rl.memory import SequentialMemory
def build_agent(model, actions):
    policy = BoltzmannQPolicy()
    memory = SequentialMemory(limit=50000, window_length=1)
    dqn = DQNAgent(
        model=model, memory=memory, policy=policy, nb_actions=actions,
        nb_steps_warmup=10, target_model_update=1e-2
    )
    return dqn
dqn = build_agent(model, actions)
dqn.compile(Adam(lr=1e-3), metrics=['mae'])
dqn.fit(env, nb_steps=50000, visualize=False, verbose=1)<jupyter_output>Training for 50000 steps ...
Interval 1 (0 steps performed)
    1/10000 [..............................] - ETA: 8:28 - reward: 1.0000
{'fraction_non_alphanumeric': 0.1452349869451697, 'fraction_numerical': 0.041122715404699736, 'mean_word_length': 3.7226502311248075, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 11, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '229440', 'license': 'MIT', 'n_tokens_mistral': 989, 'n_tokens_neox': 875, 'n_words': 232, 'path': 'notebooks/Reinforcement-Learning/deep_rl_intro.ipynb', 'repo_name': 'NathanielDake/NathanielDake.github.io', 'size': '3064'}