class_docstr: Represents structured linear-chain CRFs, generalizing HMMs smoothing, tagging models,
  and anything with chain-like dynamics.
docstr: Convert a sequence representation to edges
  
  Parameters:
      sequence : b x N long tensor in [0, C-1]
      extra : number of states
      lengths: b long tensor of N values
  
  Returns:
      edge : b x (N-1) x C x C markov indicators
                  (t x z_t x z_{t-1})
docstr_tok: ["Convert","a","sequence","representation","to","edges","Parameters",":","sequence",":","b","x","N","long","tensor","in","[","0",",","C-1","]","extra",":","number","of","states","lengths",":","b","long","tensor","of","N","values","Returns",":","edge",":","b","x","(","N-1",")","x","C","x","C","markov","indicators","(","t","x","z_t","x","z_","{","t-1","}",")"]
code: def to_parts(sequence, extra, lengths=None):
      """
      Convert a sequence representation to edges
  
      Parameters:
          sequence : b x N long tensor in [0, C-1]
          extra : number of states
          lengths: b long tensor of N values
  
      Returns:
          edge : b x (N-1) x C x C markov indicators
                      (t x z_t x z_{t-1})
      """
      C = extra
      batch, N = sequence.shape
      labels = torch.zeros(batch, N - 1, C, C).long()
      if lengths is None:
          lengths = torch.LongTensor([N] * batch)
      for n in range(1, N):
          labels[
              torch.arange(batch),
              n - 1,
              sequence[:, n],
              sequence[:, n - 1],
          ] = 1
      for b in range(batch):
          labels[b, lengths[b] - 1 :, :, :] = 0
      return labels
  
file_path: pytorch-struct/torch_struct/linearchain.py
imports: import torch
  from .helpers import _Struct
label_desc: Use image node_id 3 for calling the LinearChain obj's underlying member method code with example usage: obj.to_parts(sequence, extra, lengths) and returns: labels