class_docstr: Pre-batched training instances for SLCWA of coherent subgraphs.
class_docstr_tok: ["Pre-batched","training","instances","for","SLCWA","of","coherent","subgraphs","."]
docstr: Sample one subgraph.
docstr_tok: ["Sample","one","subgraph","."]
code: def subgraph_sample(self) -> List[int]:
      """Sample one subgraph."""
      # initialize
      node_weights = self.degrees.detach().clone()
      edge_picked = torch.zeros(
          self.mapped_triples.shape[0], dtype=torch.bool
      )
      node_picked = torch.zeros(self.degrees.shape[0], dtype=torch.bool)
  
      # sample iteratively
      result = []
      for _ in range(self.batch_size):
          # determine weights
          weights = node_weights * node_picked
  
          if torch.sum(weights) == 0:
              # randomly choose a vertex which has not been chosen yet
              pool = (~node_picked).nonzero()
              chosen_vertex = pool[
                  torch.randint(pool.numel(), size=tuple())
              ]
          else:
              # normalize to probabilities
              probabilities = weights.float() / weights.sum().float()
              chosen_vertex = torch.multinomial(
                  probabilities, num_samples=1
              )[0]
  
          # sample a start node
          node_picked[chosen_vertex] = True
  
          # get list of neighbors
          start = self.offset[chosen_vertex]
          chosen_node_degree = self.degrees[chosen_vertex].item()
          stop = start + chosen_node_degree
          adj_list = self.neighbors[start:stop, :]
  
          # sample an outgoing edge at random which has not been chosen yet using rejection sampling
          chosen_edge_index = torch.randint(
              chosen_node_degree, size=(1,)
          )[0]
          chosen_edge = adj_list[chosen_edge_index]
          edge_number = chosen_edge[0]
          while edge_picked[edge_number]:
              chosen_edge_index = torch.randint(
                  chosen_node_degree, size=(1,)
              )[0]
              chosen_edge = adj_list[chosen_edge_index]
              edge_number = chosen_edge[0]
          result.append(edge_number.item())
  
          edge_picked[edge_number] = True
  
          # visit target node
          other_vertex = chosen_edge[1]
          node_picked[other_vertex] = True
  
          # decrease sample counts
          node_weights[chosen_vertex] -= 1
          node_weights[other_vertex] -= 1
      return result
  
imports: import math
  from abc import ABC, abstractmethod
  from typing import Callable, Generic, Iterable, Iterator, List, NamedTuple, Optional, Tuple, TypeVar
  import numpy
  import scipy.sparse
  import torch
  from class_resolver import HintOrType, OptionalKwargs
  from torch.utils import data
  from .utils import compute_compressed_adjacency_list
  from ..sampling import NegativeSampler, negative_sampler_resolver
  from ..typing import MappedTriples
label_desc: Use image node_id 2 for calling the SubGraphSLCWAInstances obj's underlying member method code with example usage: obj.subgraph_sample() and returns: result