class_docstr: Autoregressive sequence model utilizing beam search.
  
  * batch_shape -> Given by initializer
  * event_shape -> N x T sequence of choices
  
  Parameters:
      model (AutoregressiveModel): A lazily computed autoregressive model.
      init (tuple of tensors, batch_shape x ...): initial state of autoregressive model.
      n_classes (int): number of classes in each time step
      n_length (int): max length of sequence
class_docstr_tok: ["Autoregressive","sequence","model","utilizing","beam","search",".","*","batch_shape","-",">","Given","by","initializer","*","event_shape","-",">","N","x","T","sequence","of","choices","Parameters",":","model","(","AutoregressiveModel",")",":","A","lazily","computed","autoregressive","model",".","init","(","tuple","of","tensors",",","batch_shape","x","...",")",":","initial","state","of","autoregressive","model",".","n_classes","(","int",")",":","number","of","classes","in","each","time","step","n_length","(","int",")",":","max","length","of","sequence"]
docstr: Compute log probability over values :math:`p(z)`.
  
  Parameters:
      value (tensor): One-hot events (*sample_shape x batch_shape x N*)
      sparse (bool): implement sparse
  
  Returns:
      log_probs (*sample_shape x batch_shape*)
docstr_tok: ["Compute","log","probability","over","values",":","math",":","`","p","(","z",")","`",".","Parameters",":","value","(","tensor",")",":","One-hot","events","(","*","sample_shape","x","batch_shape","x","N","*",")","sparse","(","bool",")",":","implement","sparse","Returns",":","log_probs","(","*","sample_shape","x","batch_shape","*",")"]
code: def log_prob(self, value, sparse=False):
      """
      Compute log probability over values :math:`p(z)`.
  
      Parameters:
          value (tensor): One-hot events (*sample_shape x batch_shape x N*)
          sparse (bool): implement sparse
  
      Returns:
          log_probs (*sample_shape x batch_shape*)
      """
      value = value.long()
      if not sparse:
          sample, batch_shape, n_length, n_classes = value.shape
          value = (
              (value * torch.arange(n_classes).view(1, 1, n_classes))
              .sum(-1)
              .long()
          )
      else:
          sample, batch_shape, n_length = value.shape
  
      value = torch.cat(
          [
              torch.zeros(sample, batch_shape, 1, device=value.device)
              .fill_(self.start_class)
              .long(),
              value,
          ],
          dim=2,
      )
      value = unwrap(value)
      state = tuple(
          (
              unwrap(i.unsqueeze(0).expand((sample,) + i.shape))
              for i in self.init
          )
      )
  
      logits, _ = self.model(value, state)
      b2, n2, c2 = logits.shape
      assert (
          (b2 == sample * batch_shape)
          and (n2 == n_length + 1)
          and (c2 == self.n_classes)
      ), "Model should return logits of shape `batch x N x C` "
  
      if self.normalize:
          log_probs = logits.log_softmax(-1)
      else:
          log_probs = logits
  
      scores = (
          log_probs[:, :-1]
          .gather(2, value[:, 1:].unsqueeze(-1))
          .sum(-1)
          .sum(-1)
      )
      return wrap(scores, sample)
  
file_path: pytorch-struct/torch_struct/autoregressive.py
imports: import torch
  from .semirings import MaxSemiring, KMaxSemiring, TempMax
  from torch.distributions.distribution import Distribution
label_desc: Use image node_id 2 for calling the Autoregressive obj's underlying member method code with example usage: obj.log_prob(value, sparse) and returns: wrap