def test_log_logit_sigmoid(): utils.log(torch.tensor(0.)) assert utils.log(torch.tensor(math.e)) - 1. < 1e-2 assert (utils.logit(torch.tensor(0.9)) - 2.197224577 < 1e-2) assert (utils.logit(torch.tensor(0.2)) + 1.38629436112 < 1e-2) layer = utils.Logit() assert (layer(torch.tensor(0.9)) - 2.197224577 < 1e-2) assert (layer(torch.tensor(0.2)) + 1.38629436112 < 1e-2) layer = utils.Sigmoid() assert (layer(torch.tensor(0.5)) - 0.6224593312018545646389 < 1e-2) assert (layer(torch.tensor(3.)) - 0.9525741268224332191212 < 1e-2) assert (layer(torch.tensor(-3.)) - 0.04742587317756678087885 < 1e-2)
def __init__(self, probs=[0.5, 0.5], learnable=True): super().__init__() self.n_dims = len(probs) if not isinstance(probs, torch.Tensor): probs = torch.tensor(probs) self.logits = log(probs.float()) if learnable: self.logits = Parameter(self.logits)
def __init__(self, probs=[0.5, 0.5], temperature=1.0, hard=True, learnable=True): super().__init__() self.temperature = temperature self.hard = hard if not isinstance(probs, torch.Tensor): probs = torch.tensor(probs) self.n_dims = probs.shape self.logits = log(probs.float()) if learnable: self.logits = Parameter(self.logits)
def __init__(self, total_count=10, probs=[0.5], learnable=True): super().__init__() if not isinstance(probs, torch.Tensor): total_count = torch.tensor(total_count) if not isinstance(probs, torch.Tensor): probs = torch.tensor(probs).view(-1) self.n_dims = len(probs) self.total_count = total_count.float() self.logits = log(probs.float()) if learnable: self.total_count = Parameter(self.total_count) self.logits = Parameter(self.logits)
def log_abs_det_jacobian(self, x, y): return (-(self.alpha.log() + self.beta.log() \ + (self.alpha - 1) * utils.log(y) + (self.beta - 1) * (-y.pow(self.alpha)).log1p())).sum(-1)
def log_abs_det_jacobian(self, x, y): return (-(x / self.scale).pow(self.concentration) \ + (self.concentration * utils.log(x)) + self.concentration.log() \ - self.concentration * self.scale.log()).sum(-1)
def log_prob(self, value): return (value * (-self.probs).log1p() + log(self.probs)).sum(-1)