<jupyter_start><jupyter_text>Deep Q Network for Mountain Car<jupyter_code>import gym
import numpy as np
import tensorflow as tf
from keras.models import Sequential
from keras.layers import Dense, Dropout, Input
from keras.optimizers import SGD<jupyter_output>Using TensorFlow backend.<jupyter_text>Choose the Environment<jupyter_code>env = gym.make('MountainCar-v0').env
env.reset()
env.reset()
for i in range(500):
    env.render()
    obs = env.step(2)
    
    if obs[2]:
        print(i)
        break<jupyter_output><empty_output><jupyter_text>Select the model<jupyter_code>model = Sequential()
model.add(Dense(32,activation='relu',input_dim = 5))
model.add(Dense(20,activation='relu'))
model.add(Dense(16,activation='relu'))
model.add(Dense(10,activation='relu'))
model.add(Dense(5,activation='relu'))
model.add(Dense(1,activation='linear'))
model.summary()
model.compile(loss='mean_squared_error', optimizer='Adam')<jupyter_output><empty_output><jupyter_text>Create another model with same size and to learn quickly<jupyter_code>model_learn = Sequential()
model_learn.add(Dense(32,activation='relu',input_dim = 5))
model_learn.add(Dense(20,activation='relu'))
model_learn.add(Dense(16,activation='relu'))
model_learn.add(Dense(10,activation='relu'))
model_learn.add(Dense(5,activation='relu'))
model_learn.add(Dense(1,activation='linear'))
model_learn.summary()
model_learn.compile(loss='mean_squared_error', optimizer='Adam')<jupyter_output><empty_output><jupyter_text>Start to play the game<jupyter_code>episode = 2
max_length_play = 9000
discount_factor = 0.99
learing_rate = 0.01
epsilon = 1
for i in range(episode):
    Experience = []
    
    epsilon = 1 / (np.sqrt(i+2800) + 1)
    #epsilon = 1
    
    #implement SARSA
    S = env.reset()
    A = env.action_space.sample()
    
    for j in range(max_length_play):
        
        env.render()
        observe = env.step(A)
        
        R = observe[2]
        
        S_new = observe[0]
        
        velocity = (S_new - S) / (0.1) 
        
      
        
        #find Greedy action which give maximum value
        Test = np.array([[S_new[0],S_new[1],velocity[0],velocity[1],0],
                         [S_new[0],S_new[1],velocity[0],velocity[1],1],
                         [S_new[0],S_new[1],velocity[0],velocity[1],2]])
        
        action_value = model.predict(Test)
        
        A_new = np.argmax(action_value)
        
        #Epsilon Greedy
        val = np.random.uniform([0])
        
        if val > epsilon:
            A_new = A_new
        else :
            A_new = np.random.choice([0,1,2])
        
        Target = np.max(action_value)
        
        if observe[2]:
            Target = 1000
            ep = 70
            Experience.append([S[0],S[1],velocity[0],velocity[1],A,R+ (discount_factor *Target)])
            break
        else :
            ep = 5
        
        Experience.append([S[0],S[1],velocity[0],velocity[1],A,R+ (discount_factor *Target)])
        S = S_new
        A = A_new
    
    Data = np.array(Experience)
    
    X_data = Data[:,[0,1,2,3,4]]
    Y_data = Data[:,[5]]
    
    model.fit(X_data,Y_data, epochs=ep, batch_size= 64 )
model.save_weights('./weights')
model.load_weights('./weigts',by_name=False)<jupyter_output><empty_output><jupyter_text>Use another model to learn new Deep Q-network<jupyter_code>episode = 2
max_length_play = 9000
discount_factor = 0.99
learing_rate = 0.01
epsilon = 1
for i in range(episode):
    Experience = []
    
    epsilon = 1 / (np.sqrt(i+280) + 1)
   # epsilon = 1
    
    #implement SARSA
    S = env.reset()
    A = env.action_space.sample()
    
    for j in range(max_length_play):
        
        env.render()
        observe = env.step(A)
        
        R = observe[2]
        
        S_new = observe[0]
        
        velocity = (S_new - S) / (0.1) 
        
      
        
        #find Greedy action which give maximum value
        Test = np.array([[S_new[0],S_new[1],velocity[0],velocity[1],0],
                         [S_new[0],S_new[1],velocity[0],velocity[1],1],
                         [S_new[0],S_new[1],velocity[0],velocity[1],2]])
        
        action_value = model_learn.predict(Test)
        
        A_new = np.argmax(action_value)
        
        #Epsilon Greedy
        val = np.random.uniform([0])
        
        if val > epsilon:
            A_new = A_new
        else :
            A_new = np.random.choice([0,1,2])
        
        Target = np.max(action_value)
        
        if observe[2]:
            Target = 1000
            ep = 70
            Experience.append([S[0],S[1],velocity[0],velocity[1],A,R+ (discount_factor *Target)])
            break
        else :
            ep = 20
        
        Experience.append([S[0],S[1],velocity[0],velocity[1],A,R+ (discount_factor *Target)])
        S = S_new
        A = A_new
    
    Data = np.array(Experience)
    
    X_data = Data[:,[0,1,2,3,4]]
    Y_data = Data[:,[5]]
    
    model_learn.fit(X_data,Y_data, epochs=ep, batch_size= 64 )
model_learn.save_weights('./learn_weight')
env.close()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13782302270947533, 'fraction_numerical': 0.031323414252153486, 'mean_word_length': 2.4849931787175987, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 26, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '522185', 'license': 'MIT', 'n_tokens_mistral': 1893, 'n_tokens_neox': 1784, 'n_words': 295, 'path': 'DQN/Mountain_Car.ipynb', 'repo_name': 'HemantharajM/Reinforcement-Learning-Intro-to-expect', 'size': '5108'}