이산 분포 다루기
이제 곧 확률적 정책을 다루게 됩니다. 확률적 정책은 특정 상태에서의 에이전트 행동을 행동들에 대한 확률분포로 표현합니다.
PyTorch는 torch.distributions.Categorical 클래스를 사용해 이산 분포를 표현할 수 있으며, 이번에 이를 실습해 보겠습니다.
입력으로 사용하는 숫자들이 확률처럼 합이 1이 아닐 필요는 없다는 것도 보게 됩니다. 값들은 자동으로 정규화됩니다.
이 연습은 강의의 일부입니다
Python으로 배우는 Deep Reinforcement Learning
연습 안내
- 범주형 확률분포를 인스턴스화하세요.
- 분포에서 표본 하나를 추출하세요.
- 확률로 사용할, 합이 1이 되는 양수 3개를 지정하세요.
- 양수 5개를 지정하세요. Categorical이 이를 조용히 정규화해 확률을 얻습니다.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
from torch.distributions import Categorical
def sample_from_distribution(probs):
print(f"\nInput: {probs}")
probs = torch.tensor(probs, dtype=torch.float32)
# Instantiate the categorical distribution
dist = ____(probs)
# Take one sample from the distribution
sampled_index = ____
print(f"Taking one sample: index {sampled_index}, with associated probability {dist.probs[sampled_index]:.2f}")
# Specify 3 positive numbers summing to 1
sample_from_distribution([.3, ____, ____])
# Specify 5 positive numbers that do not sum to 1
sample_from_distribution([2, ____, ____, ____, ____])