การทำงานกับการแจกแจงแบบไม่ต่อเนื่อง
ในไม่ช้าคุณจะได้ทำงานกับ stochastic policy ซึ่งเป็น policy ที่แทนพฤติกรรมของ agent ในแต่ละ state ด้วยการแจกแจงความน่าจะเป็นเหนือ action ต่าง ๆ
PyTorch รองรับการแจกแจงแบบไม่ต่อเนื่องผ่านคลาส torch.distributions.Categorical ซึ่งเราจะมาทดลองใช้งานกัน
สิ่งที่น่าสนใจคือ ตัวเลขที่ใช้เป็น input ไม่จำเป็นต้องรวมกันได้ 1 เหมือนกับความน่าจะเป็นทั่วไป เพราะระบบจะ normalize ให้โดยอัตโนมัติ
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Deep Reinforcement Learning ด้วย Python
คำแนะนำการฝึกหัด
- สร้าง instance ของการแจกแจงความน่าจะเป็นแบบ categorical
- สุ่มตัวอย่างหนึ่งค่าจากการแจกแจง
- กำหนดตัวเลขบวก 3 ตัวที่รวมกันได้ 1 เพื่อใช้เป็นค่าความน่าจะเป็น
- กำหนดตัวเลขบวก 5 ตัว โดย Categorical จะ normalize ให้กลายเป็นความน่าจะเป็นโดยอัตโนมัติ
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
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, ____, ____, ____, ____])