유튜브/대충 배우는 머신러닝 AI(영상삭제)

24 - Q Learning

개복치 개발자 2020. 2. 24. 16:12

 

Q-learning 이란 강화학습 학습기법 중에 하나입니다.

 

기존의 학습 모델 없이도 학습을 할 수 있는 방법입니다. 

 

여러 방식으로 시도하여, 가장 가중치가 높은 방법을 찾는 방식인데

 

이 방식으로 하면, 새로운(더 좋은)방법을 찾는 것을 그만두는 문제가 발생합니다.

 

때문에, Exploit과 Exploration이라는 개념이 생기게 됩니다.

 

아는 길을 따라갈 때랑, 아는 길을 따라가지 않을 때로 구분해서 처리하는데

 

E-greedy 라는 개념을 사용합니다. 

 

import gym
import numpy as np
import matplotlib.pyplot as plt
from gym.envs.registration import register
import random as pr

register(
    id='FrozenLake-v3',
    entry_point='gym.envs.toy_text:FrozenLakeEnv',
    kwargs={'map_name': '4x4',
            'is_slippery': False}
)

env = gym.make('FrozenLake-v3')

# Initialize table with all zeros
Q = np.zeros([env.observation_space.n, env.action_space.n])
# Set learning parameters
dis = .99
num_episodes = 2000

# create lists to contain total rewards and steps per episode
rList = []
for i in range(num_episodes):
    # Reset environment and get first new observation
    state = env.reset()
    rAll = 0
    done = False

    e = 1. / ((i // 100) + 1)  # Python2&3

    # The Q-Table learning algorithm
    while not done:
        # Choose an action by e greedy
        if np.random.rand(1) < e:
            action = env.action_space.sample()
        else:
            action = np.argmax(Q[state, :])

        # Get new state and reward from environment
        new_state, reward, done, _ = env.step(action)

        # Update Q-Table with new knowledge using learning rate
        Q[state, action] = reward + dis * np.max(Q[new_state, :])

        rAll += reward
        state = new_state

    rList.append(rAll)

print("Success rate: " + str(sum(rList) / num_episodes))
print("Final Q-Table Values")
print(Q)
plt.bar(range(len(rList)), rList, color="blue")
plt.show()

 

 

 

 

'유튜브 > 대충 배우는 머신러닝 AI(영상삭제)' 카테고리의 다른 글

25 - DQN  (0) 2020.02.25
23 - RL  (0) 2020.02.24
22 - RNN이란  (0) 2020.02.22
21 - CNN MNIST  (0) 2020.02.21
20 - CNN 이란  (0) 2020.02.21