class_docstr: A data generator for link prediction with Homogeneous GraphSAGE models
  
  At minimum, supply the StellarGraph, the batch size, and the number of
  node samples for each layer of the GraphSAGE model.
  
  The supplied graph should be a StellarGraph object with node features.
  
  Use the :meth:`flow` method supplying the nodes and (optionally) targets,
  or an UnsupervisedSampler instance that generates node samples on demand,
  to get an object that can be used as a Keras data generator.
  
  Example::
  
      G_generator = GraphSAGELinkGenerator(G, 50, [10,10])
      train_data_gen = G_generator.flow(edge_ids)
  
  .. seealso::
  
     Model using this generator: :class:`.GraphSAGE`.
  
     Some examples using this generator (see the model for more):
  
     - `link prediction <https://stellargraph.readthedocs.io/en/stable/demos/link-prediction/graphsage-link-prediction.html>`__
     - `unsupervised representation learning via random walks <https://stellargraph.readthedocs.io/en/stable/demos/embeddings/graphsage-unsupervised-sampler-embeddings.html>`__
  
     Related functionality:
  
     - :class:`.UnsupervisedSampler` for unsupervised training using random walks
     - :class:`.GraphSAGENodeGenerator` for node classification and related tasks
     - :class:`.DirectedGraphSAGELinkGenerator` for directed graphs
     - :class:`.HinSAGELinkGenerator` for heterogeneous graphs
  
  Args:
      G (StellarGraph): A machine-learning ready graph.
      batch_size (int): Size of batch of links to return.
      num_samples (list): List of number of neighbour node samples per GraphSAGE layer (hop) to take.
      seed (int or str), optional: Random seed for the sampling methods.
      weighted (bool, optional): If True, sample neighbours using the edge weights in the graph.
class_docstr_tok: ["A","data","generator","for","link","prediction","with","Homogeneous","GraphSAGE","models","At","minimum",",","supply","the","StellarGraph",",","the","batch","size",",","and","the","number","of","node","samples","for","each","layer","of","the","GraphSAGE","model",".","The","supplied","graph","should","be","a","StellarGraph","object","with","node","features",".","Use","the",":","meth",":","`","flow","`","method","supplying","the","nodes","and","(","optionally",")","targets",",","or","an","UnsupervisedSampler","instance","that","generates","node","samples","on","demand",",","to","get","an","object","that","can","be","used","as","a","Keras","data","generator",".","Example",":",":","G_generator","=","GraphSAGELinkGenerator","(","G",",","50",",","[","10,10","]",")","train_data_gen","=","G_generator.flow","(","edge_ids",")","..","seealso",":",":","Model","using","this","generator",":",":","class",":","`",".GraphSAGE","`",".","Some","examples","using","this","generator","(","see","the","model","for","more",")",":","-","`","link","prediction","<","https",":","\/\/stellargraph.readthedocs.io\/en\/stable\/demos\/link-prediction\/graphsage-link-prediction.html",">","`","__","-","`","unsupervised","representation","learning","via","random","walks","<","https",":","\/\/stellargraph.readthedocs.io\/en\/stable\/demos\/embeddings\/graphsage-unsupervised-sampler-embeddings.html",">","`","__","Related","functionality",":","-",":","class",":","`",".UnsupervisedSampler","`","for","unsupervised","training","using","random","walks","-",":","class",":","`",".GraphSAGENodeGenerator","`","for","node","classification","and","related","tasks","-",":","class",":","`",".DirectedGraphSAGELinkGenerator","`","for","directed","graphs","-",":","class",":","`",".HinSAGELinkGenerator","`","for","heterogeneous","graphs","Args",":","G","(","StellarGraph",")",":","A","machine-learning","ready","graph",".","batch_size","(","int",")",":","Size","of","batch","of","links","to","return",".","num_samples","(","list",")",":","List","of","number","of","neighbour","node","samples","per","GraphSAGE","layer","(","hop",")","to","take",".","seed","(","int","or","str",")",",","optional",":","Random","seed","for","the","sampling","methods",".","weighted","(","bool",",","optional",")",":","If","True",",","sample","neighbours","using","the","edge","weights","in","the","graph","."]
docstr: Sample neighbours recursively from the head nodes, collect the features of the
  sampled nodes, and return these as a list of feature arrays for the GraphSAGE
  algorithm.
  
  Args:
      head_links: An iterable of edges to perform sampling for.
      batch_num (int): Batch number
  
  Returns:
      A list of the same length as ``num_samples`` of collected features from
      the sampled nodes of shape:
      ``(len(head_nodes), num_sampled_at_layer, feature_size)``
      where ``num_sampled_at_layer`` is the cumulative product of `num_samples`
      for that layer.
code: def sample_features(self, head_links, batch_num):
      """
      Sample neighbours recursively from the head nodes, collect the features of the
      sampled nodes, and return these as a list of feature arrays for the GraphSAGE
      algorithm.
  
      Args:
          head_links: An iterable of edges to perform sampling for.
          batch_num (int): Batch number
  
      Returns:
          A list of the same length as ``num_samples`` of collected features from
          the sampled nodes of shape:
          ``(len(head_nodes), num_sampled_at_layer, feature_size)``
          where ``num_sampled_at_layer`` is the cumulative product of `num_samples`
          for that layer.
      """
      node_type = self.head_node_types[0]
      head_size = len(head_links)
  
      # The number of samples for each head node (not including itself)
      num_full_samples = np.sum(np.cumprod(self.num_samples))
  
      # Reshape node samples to sensible format
      def get_levels(loc, lsize, samples_per_hop, walks):
          end_loc = loc + lsize
          walks_at_level = list(
              it.chain(*[w[loc:end_loc] for w in walks])
          )
          if len(samples_per_hop) < 1:
              return [walks_at_level]
          return [walks_at_level] + get_levels(
              end_loc,
              lsize * samples_per_hop[0],
              samples_per_hop[1:],
              walks,
          )
  
      # Get sampled nodes for the subgraphs for the edges where each edge is a tuple
      # of 2 nodes, so we are extracting 2 head nodes per edge
      batch_feats = []
      for hns in zip(*head_links):
          node_samples = self._samplers[batch_num].run(
              nodes=hns,
              n=1,
              n_size=self.num_samples,
              weighted=self.weighted,
          )
  
          nodes_per_hop = get_levels(
              0, 1, self.num_samples, node_samples
          )
  
          # Get features for the sampled nodes
          batch_feats.append(
              [
                  self.graph.node_features(
                      layer_nodes,
                      node_type,
                      use_ilocs=True,
                  )
                  for layer_nodes in nodes_per_hop
              ]
          )
  
      # Resize features to (batch_size, n_neighbours, feature_size)
      # and re-pack features into a list where source, target feats alternate
      # This matches the GraphSAGE link model with (node_src, node_dst) input sockets:
      batch_feats = [
          np.reshape(feats, (head_size, -1, feats.shape[1]))
          for ab in zip(*batch_feats)
          for feats in ab
      ]
      return batch_feats
  
imports: import random
  import operator
  import numpy
  import itertools
  import operator
  import collections
  import abc
  import warnings
  from functools import reduce
  from tensorflow import keras
  from ..core.graph import StellarGraph, GraphSchema
  from ..data import SampledBreadthFirstWalk, SampledHeterogeneousBreadthFirstWalk, UniformRandomWalk, UnsupervisedSampler, DirectedBreadthFirstNeighbours
  from ..core.utils import is_real_iterable
  from .None import LinkSequence, OnDemandLinkSequence
  from ..random import SeededPerBatch
  from .base import Generator
label_desc: Use image node_id 2 for calling the GraphSAGELinkGenerator obj's underlying member method code with example usage: obj.sample_features(head_links, batch_num) and returns: batch_feats, unknown, list