`torch.distributions.relaxed_bernoulli.LogitRelaxedBernoulli.param_shape`:詳細解説とサンプルコード


torch.distributions.relaxed_bernoulli.LogitRelaxedBernoulli.param_shape は、LogitRelaxedBernoulli 分布のパラメータの形状を返すメソッドです。この形状は、分布をパラメータ化するのに必要なテンソルの次元数を表します。

数学的表現

LogitRelaxedBernoulli 分布は、2つのパラメータで定義されます。

  1. temperature: 温度と呼ばれる正の実数値。この値が大きいほど、分布はより連続的になり、小さいほどより離散的になります。
  2. logits: ロジットと呼ばれるテンソル。各要素は、対応する事象の対数のオッズを表します。

param_shape メソッドは、これらのパラメータの形状をタプルとして返します。具体的には、次のようになります。

param_shape = (temperature_shape, logits_shape)

ここで、

  • logits_shape: logits パラメータの形状を表すタプル。
  • temperature_shape: temperature パラメータの形状を表すタプル。

次の例は、param_shape メソッドの使い方を示しています。

import torch
from torch.distributions import relaxed_bernoulli

# 温度をスカラー、ロジットをベクトルとして設定
temperature = 1.0
logits = torch.tensor([0.0, 1.0, 2.0])

# LogitRelaxedBernoulli分布を作成
distribution = relaxed_bernoulli.LogitRelaxedBernoulli(temperature, logits)

# パラメータの形状を取得
param_shape = distribution.param_shape
print(param_shape)

このコードを実行すると、次の出力が得られます。

((), (3,))

この出力は、temperature パラメータはスカラーであり、logits パラメータは3要素のベクトルであることを意味します。

torch.distributions.relaxed_bernoulli.LogitRelaxedBernoulli.param_shape メソッドは、LogitRelaxedBernoulli 分布のパラメータの形状を返すのに役立ちます。この情報は、分布をサンプリングしたり、確率密度関数を計算したりする際に必要です。

  • パラメータの形状は、分布を作成する際に明示的に指定することもできます。たとえば、次のようにコードできます。
  • param_shape メソッドは、Distribution クラスのメソッドです。これは、torch.distributions モジュールのすべての分布で利用できます。
temperature = torch.tensor(1.0)
logits = torch.tensor([0.0, 1.0, 2.0])

distribution = relaxed_bernoulli.LogitRelaxedBernoulli(temperature=temperature, logits=logits)
  • パラメータの形状が不明な場合は、param_shape メソッドは None を返します。


import torch
from torch.distributions import relaxed_bernoulli

# 温度をスカラー、ロジットをベクトルとして設定
temperature = 1.0
logits = torch.tensor([0.0, 1.0, 2.0])

# LogitRelaxedBernoulli分布を作成
distribution = relaxed_bernoulli.LogitRelaxedBernoulli(temperature, logits)

# サンプルを生成
samples = distribution.sample()
print(samples)

# 確率密度関数を計算
log_prob = distribution.log_prob(samples)
print(log_prob)
  1. temperaturelogits を定義します。
  2. LogitRelaxedBernoulli 分布を作成します。
  3. 分布からサンプルを生成します。
  4. サンプルの確率密度関数を計算します。

サンプル出力は以下のようになります。

tensor([ 0.9932,  0.0041,  0.9892])
tensor([-0.0068, -2.7756, -0.0108])

この出力は、生成されたサンプルと、それぞれのサンプルの確率密度関数に対応しています。

import torch
from torch.distributions import relaxed_bernoulli

# 温度をテンソル、ロジットをベクトルとして設定
temperature = torch.linspace(0.1, 2.0, 10)
logits = torch.tensor([0.0, 1.0, 2.0])

# 各温度での確率密度関数を計算
log_prob_list = []
for temp in temperature:
    distribution = relaxed_bernoulli.LogitRelaxedBernoulli(temp, logits)
    log_prob = distribution.log_prob(logits)
    log_prob_list.append(log_prob)

# 結果を可視化
import matplotlib.pyplot as plt

plt.plot(temperature, log_prob_list)
plt.xlabel("Temperature")
plt.ylabel("Log Probability")
plt.show()

このコードは、temperature が異なる場合の LogitRelaxedBernoulli 分布の確率密度関数をプロットします。



直接属性にアクセスする

param_shape メソッドは、LogitRelaxedBernoulli オブジェクトの属性として直接アクセスできます。

import torch
from torch.distributions import relaxed_bernoulli

# 温度をスカラー、ロジットをベクトルとして設定
temperature = 1.0
logits = torch.tensor([0.0, 1.0, 2.0])

# LogitRelaxedBernoulli分布を作成
distribution = relaxed_bernoulli.LogitRelaxedBernoulli(temperature, logits)

# パラメータ形状を取得
param_shape = distribution.param_shape
print(param_shape)

このコードは、param_shape メソッドを使用するのと同じ結果を出力します。

distribution.info 属性を使用する

distribution.info 属性は、分布に関する情報を含む辞書を返します。この辞書には、param_shape キーを含むいくつかのキーが含まれています。

import torch
from torch.distributions import relaxed_bernoulli

# 温度をスカラー、ロジットをベクトルとして設定
temperature = 1.0
logits = torch.tensor([0.0, 1.0, 2.0])

# LogitRelaxedBernoulli分布を作成
distribution = relaxed_bernoulli.LogitRelaxedBernoulli(temperature, logits)

# パラメータ形状を取得
param_shape = distribution.info.param_shape
print(param_shape)

手動で計算する

param_shape は、分布のパラメータの形状を構成する要素の数に基づいて手動で計算できます。

import torch
from torch.distributions import relaxed_bernoulli

# 温度をスカラー、ロジットをベクトルとして設定
temperature = 1.0
logits = torch.tensor([0.0, 1.0, 2.0])

# パラメータ形状を計算
param_shape = (temperature.shape(), logits.shape())
print(param_shape)

torch.distributions.relaxed_bernoulli.LogitRelaxedBernoulli.param_shape メソッドのパラメータ形状を取得するには、以下の3つの方法があります。

  1. 直接属性にアクセスする
  2. distribution.info 属性を使用する
  3. 手動で計算する

どの方法を使用するかは、状況によって異なります。シンプルで分かりやすい方法は、1. または 2. の方法です。しかし、より柔軟な制御が必要な場合は、3. の方法を使用することができます。

  • パラメータ形状は、分布を作成する際に明示的に指定することもできます。
  • 上記の方法は、torch.distributions モジュールの他の分布にも適用できます。