멀티암드 밴딧

Python으로 배우는 Gymnasium 기반 Reinforcement Learning

Fouad Trad

Machine Learning Engineer

멀티암드 밴딧

 

  • 슬롯머신 앞에 선 도박사
  • 과제 → 승률 극대화
  • 해결 → 탐색-활용 균형

여러 대의 슬롯머신 앞에 선 남성을 보여주는 이미지

Python으로 배우는 Gymnasium 기반 Reinforcement Learning

슬롯머신

4대의 슬롯머신이 각각 45%, 35%, 85%, 62%의 서로 다른 승률을 가지며, 사용자는 이를 모름을 보여주는 이미지.

  • 각 암의 보상은 0 또는 1
  • 에이전트 목표 → 총 보상 최대화
Python으로 배우는 Gymnasium 기반 Reinforcement Learning

문제 해결

 

  • 감소형 엡실론-그리디
  • 엡실론 → 임의 머신 선택

확률 엡실론로 에이전트가 임의로 머신을 탐색 선택함을 보여주는 다이어그램.

Python으로 배우는 Gymnasium 기반 Reinforcement Learning

문제 해결

 

  • 감소형 엡실론-그리디
  • 엡실론 → 임의 머신 선택
  • 1 - 엡실론 → 현재 최선 머신 선택
  • 엡실론은 시간에 따라 감소

확률 엡실론로 임의 머신을 탐색하고, 확률 1 - 엡실론로 가장 좋은 머신을 활용 선택함을 보여주는 다이어그램.

Python으로 배우는 Gymnasium 기반 Reinforcement Learning

초기화

n_bandits = 4  
true_bandit_probs = np.random.rand(n_bandits)

n_iterations = 100000 epsilon = 1.0 min_epsilon = 0.01 epsilon_decay = 0.999
counts = np.zeros(n_bandits) # 각 밴딧이 플레이된 횟수
values = np.zeros(n_bandits) # 각 밴딧의 추정 승률
rewards = np.zeros(n_iterations) # 보상 기록
selected_arms = np.zeros(n_iterations, dtype=int) # 선택된 암 기록
Python으로 배우는 Gymnasium 기반 Reinforcement Learning

상호작용 루프

for i in range(n_iterations):
    arm = epsilon_greedy()

reward = np.random.rand() < true_bandit_probs[arm]
rewards[i] = reward selected_arms[i] = arm counts[arm] += 1
values[arm] += (reward - values[arm]) / counts[arm]
epsilon = max(min_epsilon, epsilon * epsilon_decay)
Python으로 배우는 Gymnasium 기반 Reinforcement Learning

선택 분석

selections_percentage = np.zeros((n_iterations, n_bandits))


프로세스의 첫 단계: (iterations, n_bandits) 크기의 0으로 채워진 배열 샘플을 보여주는 다이어그램.

Python으로 배우는 Gymnasium 기반 Reinforcement Learning

선택 분석

selections_percentage = np.zeros((n_iterations, n_bandits))

for i in range(n_iterations): selections_percentage[i, selected_arms[i]] = 1

두 번째 단계: 각 반복에서 선택된 암을 배열 안에 값 1로 표시하는 다이어그램.

Python으로 배우는 Gymnasium 기반 Reinforcement Learning

선택 분석

selections_percentage = np.zeros((n_iterations, n_bandits))

for i in range(n_iterations): selections_percentage[i, selected_arms[i]] = 1
selections_percentage = np.cumsum(selections_percentage, axis=0) / np.arange(1, n_iterations + 1).reshape(-1, 1)

마지막 단계: 선택 누적합을 계산한 뒤, 반복 횟수로 나누어 각 반복에서 암 선택 비율을 얻는 과정을 보여주는 다이어그램.

Python으로 배우는 Gymnasium 기반 Reinforcement Learning

선택 분석

  각 밴딧의 selection_percentage 곡선을 보여주는 이미지로, 반복이 진행될수록 에이전트가 2번 밴딧을 더 자주 선택함을 나타냄.

for arm in range(n_bandits):
    plt.plot(selections_percentage[:, arm], label=f'Bandit #{arm+1}')
plt.xscale('log')
plt.title('Bandit Action Choices Over Time')
plt.xlabel('Episode Number')
plt.ylabel('Percentage of Bandit Selections (%)')
plt.legend()
plt.show()

for i, prob in enumerate(true_bandit_probs, 1): print(f"Bandit #{i} -> {prob:.2f}")
Bandit #1 -> 0.37
Bandit #2 -> 0.95
Bandit #3 -> 0.73
Bandit #4 -> 0.60
  • 에이전트는 승률이 가장 높은 밴딧을 학습해 선택함
Python으로 배우는 Gymnasium 기반 Reinforcement Learning

연습해 봅시다!

Python으로 배우는 Gymnasium 기반 Reinforcement Learning

Preparing Video For Download...