docstr: Compute the marginals of a structured model.
  
  Parameters:
      logpotentials : generic params (see class)
      lengths: None or b long tensor mask
  
  Returns:
      marginals: b x (N-1) x C x C table
docstr_tok: ["Compute","the","marginals","of","a","structured","model",".","Parameters",":","logpotentials",":","generic","params","(","see","class",")","lengths",":","None","or","b","long","tensor","mask","Returns",":","marginals",":","b","x","(","N-1",")","x","C","x","C","table"]
code: def marginals(self, logpotentials, lengths=None, _raw=False):
      """
      Compute the marginals of a structured model.
  
      Parameters:
          logpotentials : generic params (see class)
          lengths: None or b long tensor mask
  
      Returns:
          marginals: b x (N-1) x C x C table
  
      """
      v, edges = self.logpartition(
          logpotentials, lengths=lengths, force_grad=True
      )
      if _raw:
          all_m = []
          for k in range(v.shape[0]):
              obj = v[k].sum(dim=0)
  
              marg = torch.autograd.grad(
                  obj,
                  edges,
                  create_graph=True,
                  only_inputs=True,
                  allow_unused=False,
              )
              all_m.append(
                  self.semiring.unconvert(self._arrange_marginals(marg))
              )
          return torch.stack(all_m, dim=0)
      else:
          obj = self.semiring.unconvert(v).sum(dim=0)
          marg = torch.autograd.grad(
              obj,
              edges,
              create_graph=True,
              only_inputs=True,
              allow_unused=False,
          )
          a_m = self._arrange_marginals(marg)
          return self.semiring.unconvert(a_m)
  
file_path: pytorch-struct/torch_struct/helpers.py
imports: import torch
  import math
  from .semirings import LogSemiring
label_desc: Use image node_id 8 for calling the _Struct obj's underlying member method code with example usage: obj.marginals(logpotentials, lengths, _raw) and returns: torch, self