def _compute_logaddexp( t1: TBoxTensor, t2: TBoxTensor, gumbel_beta: float) -> Tuple[torch.Tensor, torch.Tensor]: lse_z = torch.logaddexp(t1.z / gumbel_beta, t2.z / gumbel_beta) z = gumbel_beta * lse_z lse_Z = torch.logaddexp(-t1.Z / gumbel_beta, -t2.Z / gumbel_beta) Z = -gumbel_beta * lse_Z return z, Z
def _compute_logaddexp_with_clipping( t1: TBoxTensor, t2: TBoxTensor, gumbel_beta: float) -> Tuple[torch.Tensor, torch.Tensor]: lse_z = torch.logaddexp(t1.z / gumbel_beta, t2.z / gumbel_beta) z = gumbel_beta * (lse_z) z_value = torch.max(z, torch.max(t1.z, t2.z)) # type: ignore lse_Z = torch.logaddexp(-t1.Z / gumbel_beta, -t2.Z / gumbel_beta) Z = -gumbel_beta * (lse_Z) Z_value = torch.min(Z, torch.min(t1.Z, t2.Z)) return z_value, Z_value
def _compute_logaddexp_with_clipping_and_separate_forward( t1: TBoxTensor, t2: TBoxTensor, gumbel_beta: float) -> Tuple[torch.Tensor, torch.Tensor]: lse_z = torch.logaddexp(t1.z / gumbel_beta, t2.z / gumbel_beta) z = gumbel_beta * (lse_z) z_value = torch.max(z, torch.max(t1.z, t2.z)) # type: ignore z_final = (z - z.detach()) + z_value.detach() lse_Z = torch.logaddexp(-t1.Z / gumbel_beta, -t2.Z / gumbel_beta) Z = -gumbel_beta * (lse_Z) Z_value = torch.min(Z, torch.min(t1.Z, t2.Z)) Z_final = (Z - Z.detach()) + Z_value.detach() return z_final, Z_final
def forward(self, g: th.Tensor, c: th.Tensor) -> th.Tensor: """ Args: g (Tensor): N x U c (Tensor): N*ctc_beam Return: score (Tensor): N x ctc_beam """ # CTC beam ctc_beam = c.shape[0] // g.shape[0] # N*ctc_beam x U repeat_g = th.repeat_interleave(g, ctc_beam, 0) # 1 x N*ctc_beam self.ctc_score = th.repeat_interleave(self.ctc_score, ctc_beam, -1) # T x N*ctc_beam self.gamma_n_g = th.repeat_interleave(self.gamma_n_g, ctc_beam, -1) self.gamma_b_g = th.repeat_interleave(self.gamma_b_g, ctc_beam, -1) # T x N*ctc_beam gamma_n_h = th.zeros_like(self.gamma_n_g) gamma_b_h = th.zeros_like(self.gamma_n_g) # zero based glen = g.shape[-1] - 1 start = max(glen, 1) gamma_n_h[start - 1] = self.ctc_prob[0, c] if glen == 0 else NEG_INF gamma_b_h[start - 1] = NEG_INF # N*ctc_beam score = gamma_n_h[start - 1] repeat = repeat_g[:, -1] != c for t in range(start, self.T): # N*ctc_beam term = th.where(repeat, self.gamma_n_g[t - 1], self.neg_inf) phi = th.logaddexp(self.gamma_b_g[t - 1], term) gamma_n_h[t] = th.logaddexp(gamma_n_h[t - 1], phi) + self.ctc_prob[t, c] gamma_b_h[t] = th.logaddexp( gamma_b_h[t - 1], gamma_n_h[t - 1]) + self.ctc_prob[t, self.blank] score = th.logaddexp(score, phi + self.ctc_prob[t, c]) # fix eos is_eos = c == self.eos gamma_nb_g = th.logaddexp(self.gamma_b_g[-1], self.gamma_n_g[-1]) score[is_eos] = gamma_nb_g[is_eos] delta_score = (score - self.ctc_score).view(-1, ctc_beam) self.gamma_n_g = gamma_n_h self.gamma_b_g = gamma_b_h self.ctc_score = score[None, ...] # N x ctc_beam return delta_score
def lbp(self, s_arc, s_sib, mask): batch_size, seq_len = mask.shape ls, rs = torch.stack(torch.where(mask.new_ones( seq_len, seq_len))).view(-1, seq_len, seq_len).sort(0)[0] mask = mask.index_fill(1, ls.new_tensor(0), 1) # [seq_len, seq_len, batch_size], (h->m) mask = (mask.unsqueeze(-1) & mask.unsqueeze(-2)).permute(2, 1, 0) # [seq_len, seq_len, seq_len, batch_size], (h->m->s) mask2o = mask.unsqueeze(1) & mask.unsqueeze(2) mask2o = mask2o & ls.unsqueeze(-1).ne(ls.new_tensor( range(seq_len))).unsqueeze(-1) mask2o = mask2o & rs.unsqueeze(-1).ne(rs.new_tensor( range(seq_len))).unsqueeze(-1) # [seq_len, seq_len, batch_size], (h->m) s_arc = s_arc.permute(2, 1, 0) # [seq_len, seq_len, seq_len, batch_size], (h->m->s) s_sib = s_sib.permute(2, 1, 3, 0).masked_fill_(~mask2o, MIN) # log beliefs # [seq_len, seq_len, batch_size], (h->m) q = s_arc # [seq_len, seq_len, seq_len, batch_size], (h->m->s) m_sib = s_sib.new_zeros(seq_len, seq_len, seq_len, batch_size) for _ in range(self.max_iter): q = q.log_softmax(0) # m(ik->ij) = logsumexp(q(ik) - m(ij->ik) + s(ij->ik)) m = q.unsqueeze(2) - m_sib # TODO: better solution for OOM m_sib = torch.logaddexp(m.logsumexp(0), m + s_sib).transpose(1, 2).log_softmax(0) # q(ij) = s(ij) + sum(m(ik->ij)), k != i,j q = s_arc + (m_sib * mask2o).sum(2) return q.permute(2, 1, 0)
def alpha_average(log_p0, log_p1, beta, alpha): pow1, pow2 = 1.0 - beta, beta pow1[-1] = 1e-10 pow2[0] = 1e-10 if alpha == 1: log_prob = pow1 * log_p0 + pow2 * log_p1 else: log_prob = (1 / (1 - alpha)) * (torch.logaddexp( torch.log(pow1) + (1 - alpha) * log_p0, torch.log(pow2) + (1 - alpha) * log_p1)) return log_prob
def merge_hypos(hypos_list: List[Node]) -> List[Node]: """ Merge the hypos that has the same prefix """ merge_dict = {} for hypos in hypos_list: prefix_str = hypos["hashid"] if prefix_str in merge_dict: merge_dict[prefix_str].score = th.logaddexp( hypos.score, merge_dict[prefix_str].score) else: merge_dict[prefix_str] = hypos merge_list = [value for _, value in merge_dict.items()] return sorted(merge_list, key=lambda n: n.score, reverse=True)
def E_step(x: torch.Tensor, alpha: torch.Tensor, gmm: torch.distributions.MultivariateNormal, config: dict) -> dict: """E-step to calculate task-related variational parameters Args: x: input data in shape (C, N, D) alpha: concentration matrix in shape (L, K) gmm: K D-variate Gaussian components config: dictionary containing configuration parameters Returns: a dictionary containing task-related variational parameters """ num_classes = x.shape[0] # initialize the parameter LAMBDA of document-topic mixture PHI lambda_ = torch.ones(config['L'], device=config['device'] ) * config['delta'] + num_classes / config['L'] # initialize the parameter ETA of document-topic assignment Y eta = torch.ones(size=(num_classes, config['L']), device=config['device']) / config['L'] # initialize the paramter GAMMA of word-topic mixture THETA gamma = torch.ones(size=(num_classes, config['K']), device=config['device']) * \ alpha[np.random.randint(low=0, high=config['L'], size=num_classes)] \ + config['num_samples_per_class'] / config['K'] # calculate log-likelihood of x log_prob = gmm.log_prob(value=x[:, :, None, :]) # (C, N, K) lambda_count = 0 dlambda = 1 while (dlambda > tols['lambda']) and (lambda_count < num_steps): log_theta_tilde = expected_log_dirichlet(concentration=gamma) #(C, K) # un-normalized r r_unnormalized = log_prob + log_theta_tilde[:, None, :] #(C, N, K) # normalize r log_r = torch.nn.functional.log_softmax(input=r_unnormalized, dim=-1) # calculate new gamma log_Nck = torch.logsumexp(input=log_r, dim=1) # (C, K) log_gamma_temp = torch.matmul(input=eta, other=alpha - 1) # (C, K) log_gamma_temp = torch.log1p(input=log_gamma_temp) # (C, K) log_gamma = torch.logaddexp(input=log_Nck, other=log_gamma_temp) # (C, K) gamma = torch.exp(input=log_gamma).float() # calculate new eta log_phi_tilde = expected_log_dirichlet(concentration=lambda_) # (L, ) eta_unnormalized = log_phi_tilde - torch.distributions.dirichlet.Dirichlet._log_normalizer( self=None, x=alpha) + torch.matmul(input=log_theta_tilde, other=alpha.T - 1) # (C, L) # normalize eta log_eta = torch.nn.functional.log_softmax(input=eta_unnormalized, dim=-1) # (C, L) eta = torch.exp(input=log_eta).float() # store previous lambda_ lambda__ = lambda_ + 0 # calculate new lambda_ log_lambda = torch.logaddexp(input=torch.tensor( np.log(config['delta']), device=config['device']), other=torch.logsumexp(input=log_eta, dim=0)) lambda_ = torch.exp(input=log_lambda).float() dlambda = torch.mean(torch.abs(lambda_ - lambda__)) if torch.isnan(dlambda): raise ValueError('dlambda is NaN') lambda_count += 1 variational_parameters = { 'log_r': log_r, 'log_gamma': log_gamma, 'log_eta': log_eta, 'log_lambda': log_lambda } return variational_parameters
def ctc_beam_search(ctc_prob: th.Tensor, beam_size: int = 8, blank: int = -1, nbest: int = 1, sos: int = -1, eos: int = -1, len_norm: bool = True, **kwargs) -> List[Dict]: """ Do CTC prefix beam search Args: ctc_prob: T x V """ if sos < 0 or eos < 0: raise ValueError(f"Invalid SOS/EOS ID: {sos:d}/{eos:d}") if blank < 0: raise ValueError(f"Invalid blank ID: {blank}") ctc_prob = th.log_softmax(ctc_prob, -1) # T x B topk_score, topk_token = th.topk(ctc_prob, beam_size, -1) T, V = ctc_prob.shape logger.info(f"--- shape of the encoder output (CTC): {T} x {V}") neg_inf = th.tensor(NEG_INF).to(ctc_prob.device) zero = th.tensor(0.0).to(ctc_prob.device) # (prefix, log_pb, log_pn) prev_beam = [((sos, ), PrefixScore(zero, neg_inf))] for t in range(T): next_beam = defaultdict(lambda: PrefixScore(neg_inf, neg_inf)) for n in range(beam_size): symb = topk_token[t, n].item() logp = topk_score[t, n].item() for prefix, prev in prev_beam[:beam_size]: # update log_pb only if symb == blank: other = next_beam[prefix] log_pb_update = th.logaddexp(prev.score() + logp, other.log_pb) next_beam[prefix] = PrefixScore(log_pb_update, other.log_pn) else: prefix_symb = prefix + (symb, ) other = next_beam[prefix_symb] # repeat if prefix[-1] == symb: log_pn_update = th.logaddexp(prev.log_pb + logp, other.log_pn) else: log_pn_update = th.logaddexp(prev.score() + logp, other.log_pn) # update log_pn only next_beam[prefix_symb] = PrefixScore( other.log_pb, log_pn_update) # repeat case if prefix[-1] == symb: other = next_beam[prefix] log_pn_update = th.logaddexp(prev.log_pn + logp, other.log_pn) next_beam[prefix] = PrefixScore( other.log_pb, log_pn_update) # keep top-#beam prev_beam = sorted(next_beam.items(), key=lambda n: n[1].score(), reverse=True) return [{ "score": score.score() / (1 if len_norm else len(prefix) - 1), "trans": prefix + (eos, ) } for prefix, score in prev_beam[:nbest]]
def score(self) -> th.Tensor: return th.logaddexp(self.log_pb, self.log_pn)
def backward(ctx, grad_output): k2x, = ctx.saved_variables return torch.exp(k2x - torch.logaddexp(0, k2x))
torch.lgamma(torch.arange(0.5, 2, 0.5)) # log torch.log(torch.arange(5) + 10) # log10 torch.log10(torch.rand(5)) # log1p torch.log1p(torch.randn(5)) # log2 torch.log2(torch.rand(5)) # logaddexp torch.logaddexp(torch.tensor([-1.0]), torch.tensor([-1.0, -2, -3])) torch.logaddexp(torch.tensor([-100.0, -200, -300]), torch.tensor([-1.0, -2, -3])) torch.logaddexp(torch.tensor([1.0, 2000, 30000]), torch.tensor([-1.0, -2, -3])) # logaddexp2 torch.logaddexp2(torch.tensor([-1.0]), torch.tensor([-1.0, -2, -3])) torch.logaddexp2(torch.tensor([-100.0, -200, -300]), torch.tensor([-1.0, -2, -3])) torch.logaddexp2(torch.tensor([1.0, 2000, 30000]), torch.tensor([-1.0, -2, -3])) # logical_and torch.logical_and(torch.tensor([True, False, True]), torch.tensor([True, False, False])) r = torch.tensor([0, 1, 10, 0], dtype=torch.int8)
def prefix_search_and_merge(self, hyps: Hypotheses, enc_out: Tensor, alpha: Optional[int] = None) -> Hypotheses: """Prefix search and merge scores of hypothese whose sequence is a prefix of a longer hypothesis. It assumes that the hypotheses have been sorted in non-increasing order of sequence length. This implementation is modified from https://github.com/espnet/espnet/blob/master/espnet/nets/beam_search_transducer.py. Note: this function will modify hyps. Args: hyps (Hypotheses): hypotheses to be merged enc_out (Tensor): encoder output of shape `(batch, src_len, embed_dim)` where batch=1 and src_len=1 alpha (int, optional): maximum prefix length in prefix search. Must be an integer, and is advised to keep this as 1 in order to reduce expensive beam search cost later (default: None) Returns: hyps (Hypotheses): merged hypotheses """ bsz = hyps.size() lengths = hyps.sequence_lengths to_merge = is_prefix_tensorized(hyps, are_sorted=True) # B x B if alpha is not None: to_merge = to_merge & (lengths.unsqueeze(1) + alpha >= lengths.unsqueeze(0)) if not to_merge.any(): # no merges return hyps for j in range(bsz - 1): for i in range(j + 1, bsz): len_j = lengths[j] len_i = lengths[i] if to_merge[i, j]: logits = (self.model.joint( enc_out, hyps.dec_out[i:i + 1, len_i - 1:len_i, :], apply_output_layer=True, ).squeeze(2).squeeze(1)) # 1 x 1 x 1 x V -> 1 x V lprobs = self.model.get_normalized_probs( (logits.div_(self.temperature), None), log_probs=True).squeeze(0) # 1 x V -> V token_index = hyps.sequences[j][len_i] score = hyps.scores[i] + lprobs[token_index] if self.lm_model is not None: lm_logits = self.lm_model.output_layer( hyps.lm_dec_out[i:i + 1, len_i - 1:len_i, :]).squeeze( 1) # 1 x 1 x V' -> 1 x V' lm_lprobs = self.lm_model.get_normalized_probs( (lm_logits, None), log_probs=True).squeeze(0) # 1 x V' -> V' if self.no_blank_in_lm and token_index > self.blank: lm_token_index = token_index - 1 else: lm_token_index = token_index local_lm_score = lm_lprobs[lm_token_index] lm_score = hyps.lm_scores[i] + local_lm_score score += self.lm_weight * local_lm_score lprobs_no_blank = lprobs[ self.vocab_nonblank_mask] # V - 1 if not self.no_blank_in_lm: lm_lprobs = lm_lprobs[ self.vocab_nonblank_mask] # V - 1 lprobs_with_lm_no_blank = (lprobs_no_blank + self.lm_weight * lm_lprobs ) # V - 1 log_scaling_factor = ( lprobs_no_blank.exp().sum().log() - lprobs_with_lm_no_blank.exp().sum().log()) score += log_scaling_factor for k in range(len_i, len_j - 1): logits = (self.model.joint( enc_out, hyps.dec_out[j:j + 1, k:k + 1, :], apply_output_layer=True, ).squeeze(2).squeeze(1)) # 1 x 1 x 1 x V -> 1 x V lprobs = self.model.get_normalized_probs( (logits.div_(self.temperature), None), log_probs=True).squeeze(0) # 1 x V -> V token_index = hyps.sequences[j][k + 1] score += lprobs[token_index] if self.lm_model is not None: lm_logits = self.lm_model.output_layer( hyps.lm_dec_out[j:j + 1, k:k + 1, :]).squeeze( 1) # 1 x 1 x V' -> 1 x V' lm_lprobs = self.lm_model.get_normalized_probs( (lm_logits, None), log_probs=True).squeeze(0) # 1 x V' -> V' if self.no_blank_in_lm and token_index > self.blank: lm_token_index = token_index - 1 else: lm_token_index = token_index local_lm_score = lm_lprobs[lm_token_index] lm_score += local_lm_score score += self.lm_weight * local_lm_score lprobs_no_blank = lprobs[ self.vocab_nonblank_mask] # V - 1 if not self.no_blank_in_lm: lm_lprobs = lm_lprobs[ self.vocab_nonblank_mask] # V - 1 lprobs_with_lm_no_blank = ( lprobs_no_blank + self.lm_weight * lm_lprobs ) # V - 1 log_scaling_factor = ( lprobs_no_blank.exp().sum().log() - lprobs_with_lm_no_blank.exp().sum().log()) score += log_scaling_factor hyps.scores[j] = torch.logaddexp(hyps.scores[j], score) if self.lm_model is not None: hyps.lm_scores[j] = torch.logaddexp( hyps.lm_scores[j], lm_score) return hyps
def sigmoid(X): return torch.exp(-torch.logaddexp(torch.zeros_like(X), -X))
def focal_loss_with_logits( input: Tensor, target: Tensor, gamma: float, pos_weight: Optional[float] = None, label_smoothing: Optional[float] = None, reduction: str = "mean", normalize: bool = False, ): r"""Computes the Focal Loss between input and target. See :class:`FocalLossWithLogits` for more details Args: input (torch.Tensor): The predicted values. target (torch.Tensor): The target values. gamma (float): The focusing parameter :math:`\gamma`. Must be non-negative. pos_weight (float, optional): The positive weight coefficient :math:`\alpha` to use on the positive examples. Must be non-negative. label_smoothing (float, optional): Float in [0, 1]. When 0, no smoothing occurs. When positive, the binary ground truth labels are clamped to :math:`[p, 1-p]`. reduction (str, optional): Specifies the reduction to apply to the output: ``'none'`` | ``'mean'`` | ``'sum'``. ``'none'``: no reduction will be applied, ``'mean'``: the sum of the output will be divided by the number of elements in the output, ``'sum'``: the output will be summed. Default: ``'mean'`` normalize (bool, optional): If given, output loss will be divided by the number of positive elements in ``target``. """ positive_indices = target == 1 # apply label smoothing, clamping true labels between x, 1-x if label_smoothing is not None: target = target.clamp(label_smoothing, 1.0 - label_smoothing) # loss in paper can be expressed as # alpha * (1 - pt) ** gamma * (BCE loss) # where pt = p if target == 1 else (1-p) # calculate p, pt, and vanilla BCELoss # NOTE BCE loss gets logits input, NOT p=sigmoid(input) calulated below ce_loss = F.binary_cross_entropy_with_logits(input, target, reduction="none") # Therefore one unified form for positive (z = 1) and negative (z = 0) # samples is: # (1 - p_t)^r = exp(-r * z * x - r * log(1 + exp(-x))). # # Becuase logits are unbounded, log(1 + exp(-x)) must be computed using # torch.logaddexp() neg_logits = input.neg().float() if gamma != 0: _ = torch.tensor([0.0], device=neg_logits.device).type_as(neg_logits) _ = gamma * (target.floor() * neg_logits - torch.logaddexp(neg_logits, _)) focal_term = torch.exp(_) loss = focal_term * ce_loss else: loss = ce_loss if pos_weight is not None: loss = torch.where(positive_indices, pos_weight * loss, (1.0 - pos_weight) * loss) # normalize if normalize: num_positive_examples = positive_indices.sum().clamp_(min=1) loss.div_(num_positive_examples) if reduction == "mean": loss = loss.mean() if reduction == "sum": loss = loss.sum() return loss
def pointwise_ops(self): a = torch.randn(4) b = torch.randn(4) t = torch.tensor([-1, -2, 3], dtype=torch.int8) r = torch.tensor([0, 1, 10, 0], dtype=torch.int8) t = torch.tensor([-1, -2, 3], dtype=torch.int8) s = torch.tensor([4, 0, 1, 0], dtype=torch.int8) f = torch.zeros(3) g = torch.tensor([-1, 0, 1]) w = torch.tensor([0.3810, 1.2774, -0.2972, -0.3719, 0.4637]) return ( torch.abs(torch.tensor([-1, -2, 3])), torch.absolute(torch.tensor([-1, -2, 3])), torch.acos(a), torch.arccos(a), torch.acosh(a.uniform_(1.0, 2.0)), torch.add(a, 20), torch.add(a, torch.randn(4, 1), alpha=10), torch.addcdiv(torch.randn(1, 3), torch.randn(3, 1), torch.randn(1, 3), value=0.1), torch.addcmul(torch.randn(1, 3), torch.randn(3, 1), torch.randn(1, 3), value=0.1), torch.angle(a), torch.asin(a), torch.arcsin(a), torch.asinh(a), torch.arcsinh(a), torch.atan(a), torch.arctan(a), torch.atanh(a.uniform_(-1.0, 1.0)), torch.arctanh(a.uniform_(-1.0, 1.0)), torch.atan2(a, a), torch.bitwise_not(t), torch.bitwise_and(t, torch.tensor([1, 0, 3], dtype=torch.int8)), torch.bitwise_or(t, torch.tensor([1, 0, 3], dtype=torch.int8)), torch.bitwise_xor(t, torch.tensor([1, 0, 3], dtype=torch.int8)), torch.ceil(a), torch.clamp(a, min=-0.5, max=0.5), torch.clamp(a, min=0.5), torch.clamp(a, max=0.5), torch.clip(a, min=-0.5, max=0.5), torch.conj(a), torch.copysign(a, 1), torch.copysign(a, b), torch.cos(a), torch.cosh(a), torch.deg2rad( torch.tensor([[180.0, -180.0], [360.0, -360.0], [90.0, -90.0]])), torch.div(a, b), torch.divide(a, b, rounding_mode="trunc"), torch.divide(a, b, rounding_mode="floor"), torch.digamma(torch.tensor([1.0, 0.5])), torch.erf(torch.tensor([0.0, -1.0, 10.0])), torch.erfc(torch.tensor([0.0, -1.0, 10.0])), torch.erfinv(torch.tensor([0.0, 0.5, -1.0])), torch.exp(torch.tensor([0.0, math.log(2.0)])), torch.exp2(torch.tensor([0.0, math.log(2.0), 3.0, 4.0])), torch.expm1(torch.tensor([0.0, math.log(2.0)])), torch.fake_quantize_per_channel_affine( torch.randn(2, 2, 2), (torch.randn(2) + 1) * 0.05, torch.zeros(2), 1, 0, 255, ), torch.fake_quantize_per_tensor_affine(a, 0.1, 0, 0, 255), torch.float_power(torch.randint(10, (4, )), 2), torch.float_power(torch.arange(1, 5), torch.tensor([2, -3, 4, -5])), torch.floor(a), # torch.floor_divide(torch.tensor([4.0, 3.0]), torch.tensor([2.0, 2.0])), # torch.floor_divide(torch.tensor([4.0, 3.0]), 1.4), torch.fmod(torch.tensor([-3, -2, -1, 1, 2, 3]), 2), torch.fmod(torch.tensor([1, 2, 3, 4, 5]), 1.5), torch.frac(torch.tensor([1.0, 2.5, -3.2])), torch.randn(4, dtype=torch.cfloat).imag, torch.ldexp(torch.tensor([1.0]), torch.tensor([1])), torch.ldexp(torch.tensor([1.0]), torch.tensor([1, 2, 3, 4])), torch.lerp(torch.arange(1.0, 5.0), torch.empty(4).fill_(10), 0.5), torch.lerp( torch.arange(1.0, 5.0), torch.empty(4).fill_(10), torch.full_like(torch.arange(1.0, 5.0), 0.5), ), torch.lgamma(torch.arange(0.5, 2, 0.5)), torch.log(torch.arange(5) + 10), torch.log10(torch.rand(5)), torch.log1p(torch.randn(5)), torch.log2(torch.rand(5)), torch.logaddexp(torch.tensor([-1.0]), torch.tensor([-1, -2, -3])), torch.logaddexp(torch.tensor([-100.0, -200.0, -300.0]), torch.tensor([-1, -2, -3])), torch.logaddexp(torch.tensor([1.0, 2000.0, 30000.0]), torch.tensor([-1, -2, -3])), torch.logaddexp2(torch.tensor([-1.0]), torch.tensor([-1, -2, -3])), torch.logaddexp2(torch.tensor([-100.0, -200.0, -300.0]), torch.tensor([-1, -2, -3])), torch.logaddexp2(torch.tensor([1.0, 2000.0, 30000.0]), torch.tensor([-1, -2, -3])), torch.logical_and(r, s), torch.logical_and(r.double(), s.double()), torch.logical_and(r.double(), s), torch.logical_and(r, s, out=torch.empty(4, dtype=torch.bool)), torch.logical_not(torch.tensor([0, 1, -10], dtype=torch.int8)), torch.logical_not( torch.tensor([0.0, 1.5, -10.0], dtype=torch.double)), torch.logical_not( torch.tensor([0.0, 1.0, -10.0], dtype=torch.double), out=torch.empty(3, dtype=torch.int16), ), torch.logical_or(r, s), torch.logical_or(r.double(), s.double()), torch.logical_or(r.double(), s), torch.logical_or(r, s, out=torch.empty(4, dtype=torch.bool)), torch.logical_xor(r, s), torch.logical_xor(r.double(), s.double()), torch.logical_xor(r.double(), s), torch.logical_xor(r, s, out=torch.empty(4, dtype=torch.bool)), torch.logit(torch.rand(5), eps=1e-6), torch.hypot(torch.tensor([4.0]), torch.tensor([3.0, 4.0, 5.0])), torch.i0(torch.arange(5, dtype=torch.float32)), torch.igamma(a, b), torch.igammac(a, b), torch.mul(torch.randn(3), 100), torch.multiply(torch.randn(4, 1), torch.randn(1, 4)), torch.mvlgamma(torch.empty(2, 3).uniform_(1.0, 2.0), 2), torch.tensor([float("nan"), float("inf"), -float("inf"), 3.14]), torch.nan_to_num(w), torch.nan_to_num(w, nan=2.0), torch.nan_to_num(w, nan=2.0, posinf=1.0), torch.neg(torch.randn(5)), # torch.nextafter(torch.tensor([1, 2]), torch.tensor([2, 1])) == torch.tensor([eps + 1, 2 - eps]), torch.polygamma(1, torch.tensor([1.0, 0.5])), torch.polygamma(2, torch.tensor([1.0, 0.5])), torch.polygamma(3, torch.tensor([1.0, 0.5])), torch.polygamma(4, torch.tensor([1.0, 0.5])), torch.pow(a, 2), torch.pow(torch.arange(1.0, 5.0), torch.arange(1.0, 5.0)), torch.rad2deg( torch.tensor([[3.142, -3.142], [6.283, -6.283], [1.570, -1.570]])), torch.randn(4, dtype=torch.cfloat).real, torch.reciprocal(a), torch.remainder(torch.tensor([-3.0, -2.0]), 2), torch.remainder(torch.tensor([1, 2, 3, 4, 5]), 1.5), torch.round(a), torch.rsqrt(a), torch.sigmoid(a), torch.sign(torch.tensor([0.7, -1.2, 0.0, 2.3])), torch.sgn(a), torch.signbit(torch.tensor([0.7, -1.2, 0.0, 2.3])), torch.sin(a), torch.sinc(a), torch.sinh(a), torch.sqrt(a), torch.square(a), torch.sub(torch.tensor((1, 2)), torch.tensor((0, 1)), alpha=2), torch.tan(a), torch.tanh(a), torch.trunc(a), torch.xlogy(f, g), torch.xlogy(f, g), torch.xlogy(f, 4), torch.xlogy(2, g), )
def log_prob(self, value): if self._validate_args: self._validate_sample(value) z = (value - self.loc) / self.scale return math.log(2 / math.pi) - self.scale.log() - torch.logaddexp( z, -z)