Skip to content

Instantly share code, notes, and snippets.

@joelouismarino
Created April 8, 2018 05:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joelouismarino/eb81848b5616de71ed06cba9dc36770a to your computer and use it in GitHub Desktop.
Save joelouismarino/eb81848b5616de71ed06cba9dc36770a to your computer and use it in GitHub Desktop.
import math
import torch
from torch.distributions import Normal
# standard univariate Gaussian (Normal)
mean = torch.zeros(1)
std = torch.ones(1)
# evaluate at the origin
value = torch.zeros(1)
# using built-in functions
normal = Normal(mean, std)
log_density = normal.log_prob(value)
# implemented from scratch
var = std.pow(2)
log_density = -0.5 * (math.log(2 * math.pi) + var.log() + (value - mean).pow(2) / var)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment