반응형
def GaussianPDF(mean, logvar, z):
r""" Return the PDF value of z in N(mean,exp(logvar))
mean, logvar : [*, dim_z]
z : [*, N, dim_z]
return: [*, N, dim_z]
"""
if type(mean) is torch.Tensor:
mean, logvar = mean.unsqueeze(-2), logvar.unsqueeze(-2)
return 1/(np.sqrt(2*np.pi)*torch.exp(logvar*0.5)) * torch.exp(-((z-mean)**2) / (2*torch.exp(logvar)))
elif type(mean) is np.ndarray:
mean, logvar = np.expand_dims(mean, axis=-2), np.expand_dims(logvar, axis=-2)
return 1/(np.sqrt(2*np.pi)*np.exp(logvar*0.5)) * np.exp(-((z-mean)**2) / (2*np.exp(logvar)))
return None
def LogGaussianPDF(mean, logvar, z):
r""" Return the log PDF value of z in N(mean,exp(logvar))
mean, logvar : [*, dim_z]
z : [*, N, dim_z]
return: [*, N, dim_z]
"""
if type(mean) is torch.Tensor:
mean, logvar = mean.unsqueeze(-2), logvar.unsqueeze(-2)
return -0.5*np.log(2*np.pi) -0.5*logvar - ((z-mean)**2+1e-6) / (2*torch.exp(logvar)+1e-6)
elif type(mean) is np.ndarray:
mean, logvar = np.expand_dims(mean, axis=-2), np.expand_dims(logvar, axis=-2)
return -0.5*np.log(2*np.pi) -0.5*logvar - ((z-mean)**2+1e-6) / (2*np.exp(logvar)+1e-6)
return None
'Deep Learning' 카테고리의 다른 글
RTX 3090 을 지원하는 pytorch anaconda로 설치하기 (0) | 2023.03.08 |
---|---|
Anaconda를 이용한 env 생성 및 제거 (0) | 2023.03.08 |
Implement KL Divergence using Pytorch (0) | 2022.12.06 |
Entropy, Mutual Information, KL Divergence (1) | 2022.12.02 |
Kullback–Leibler divergence (KL divergence) (0) | 2022.11.18 |