शुरू करेंमुफ़्त में शुरू करें

डिस्क्रीट डिस्ट्रीब्यूशंस के साथ काम करना

आप जल्द ही स्टोकेस्टिक पॉलिसीज़ के साथ काम करने वाले हैं: ऐसी पॉलिसीज़ जो किसी दिए गए स्टेट में एजेंट के व्यवहार को एक्शन पर आधारित probability distribution के रूप में दर्शाती हैं.

PyTorch डिस्क्रीट डिस्ट्रीब्यूशंस को torch.distributions.Categorical क्लास के ज़रिए दर्शा सकता है, और अभी आप इसी के साथ प्रयोग करेंगे.

आप देखेंगे कि इनपुट में दिए गए नंबरों का 1 तक जोड़ना जरूरी नहीं है, जैसा कि probabilities में होता है; वे अपने-आप normalize हो जाते हैं.

यह अभ्यास पाठ्यक्रम का हिस्सा है

Python में Deep Reinforcement Learning

पाठ्यक्रम देखें

अभ्यास निर्देश

  • केटेगोरिकल probability distribution को instantiate करें.
  • डिस्ट्रीब्यूशन से एक सैंपल लें.
  • 3 धनात्मक नंबर बताएँ जिनका योग 1 हो, ताकि वे probabilities की तरह काम करें.
  • 5 धनात्मक नंबर बताएँ; Categorical उन्हें probabilities पाने के लिए चुपचाप 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, ____, ____, ____, ____])
कोड संपादित करें और चलाएँ