離散分布を扱う
まもなく確率的方策(stochastic policies)を扱います。これは、ある状態でのエージェントの振る舞いを、行動に対する確率分布として表す方策です。
PyTorch では、torch.distributions.Categorical クラスを使って離散分布を表現できます。ここではこれを実験してみます。
入力として使う数値は確率のように合計が 1 である必要はないことが分かります。自動的に正規化されます。
この演習はコースの一部です
Pythonで学ぶDeep Reinforcement Learning
演習の手順
- カテゴリ分布(categorical probability distribution)をインスタンス化します。
- その分布からサンプルを1つ取得します。
- 確率として機能する、合計が 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, ____, ____, ____, ____])