[Pytorch] MixtureSameFamily 을 사용해서 bimodal distribution 만들기
2020. 5. 5. 23:33ㆍ분석 Python/Pytorch
torch 1.5.0 버전부터 생김
bimodal gaussian distribution 만들기
n = 1000
import torch
import seaborn as sns
from torch.distributions import Normal
from torch import distributions as D
from torch.distributions.mixture_same_family import MixtureSameFamily
d = torch.cat((Normal(loc=-3, scale =1.0).sample((n,1)) ,
Normal(loc=0.5, scale =1.0).sample((n,1))),dim=1)
mix = D.Categorical(probs = torch.nn.Softmax()(torch.rand(n,2)))
comp = D.Independent(D.Normal(d.unsqueeze(dim=2) ,
Normal(loc=0, scale =0.5).sample((n,2,1))), 1)
gmm = MixtureSameFamily(mix, comp)
sns.distplot(gmm.sample().detach().numpy().reshape(-1,1))
확장하면 쉽게 n-modal이 가능하다.
trimodal 만들기
n = 1000
import torch
import seaborn as sns
from torch.distributions import Normal
from torch import distributions as D
from torch.distributions.mixture_same_family import MixtureSameFamily
d = torch.cat((Normal(loc=-3, scale =1.0).sample((n,1)) ,
Normal(loc=0.5, scale =1.0).sample((n,1)),
Normal(loc=5.5, scale =1.0).sample((n,1))
),dim=1)
mix = D.Categorical(probs = torch.nn.Softmax()(torch.rand(n,3)))
comp = D.Independent(D.Normal(d.unsqueeze(dim=2) ,
Normal(loc=0, scale =0.5).sample((n,3,1))), 1)
gmm = MixtureSameFamily(mix, comp)
sns.distplot(gmm.sample().detach().numpy().reshape(-1,1))
728x90
'분석 Python > Pytorch' 카테고리의 다른 글
[Pytorch] Pytorch를 Keras처럼 API 호출 하는 방식으로 사용하는 방법 (0) | 2020.08.25 |
---|---|
[Pytorch] LSTM AutoEncoder for Anomaly Detection (3) | 2020.08.23 |
Pytorch 1.6 Release Note Information (0) | 2020.08.21 |
[Pytorch] 1.5.0 버전 설치하기 (0) | 2020.05.05 |
PyTorch에서 SHAP을 사용하여 모델 해석하기 (정형 데이터) (7) | 2020.03.14 |