class_docstr: The BaggingEnsemble class can be used to create ensembles of stellargraph's graph neural network algorithms
  including GCN, GraphSAGE, GAT, and HinSAGE. Ensembles can be used for training classification and regression
  problems for node attribute inference and link prediction.
  
  This class can be used to create Bagging ensembles.
  
  Bagging ensembles add model diversity in two ways: (1) by random initialisation of the models' weights (before
  training) to different values; and (2) by bootstrap sampling of the training data for each model. That is, each
  model in the ensemble is trained on a random subset of the training examples, sampled with replacement from the
  original training data.
  
  .. seealso::
  
     `Examples using ensembles <https://stellargraph.readthedocs.io/en/stable/demos/ensembles/index.html>`__.
  
     Related functionality: :class:`.Ensemble` for only random initialisation.
class_docstr_tok: ["The","BaggingEnsemble","class","can","be","used","to","create","ensembles","of","stellargraph","'s","graph","neural","network","algorithms","including","GCN",",","GraphSAGE",",","GAT",",","and","HinSAGE",".","Ensembles","can","be","used","for","training","classification","and","regression","problems","for","node","attribute","inference","and","link","prediction",".","This","class","can","be","used","to","create","Bagging","ensembles",".","Bagging","ensembles","add","model","diversity","in","two","ways",":","(","1",")","by","random","initialisation","of","the","models","'","weights","(","before","training",")","to","different","values",";","and","(","2",")","by","bootstrap","sampling","of","the","training","data","for","each","model",".","That","is",",","each","model","in","the","ensemble","is","trained","on","a","random","subset","of","the","training","examples",",","sampled","with","replacement","from","the","original","training","data",".","..","seealso",":",":","`","Examples","using","ensembles","<","https",":","\/\/stellargraph.readthedocs.io\/en\/stable\/demos\/ensembles\/index.html",">","`","__",".","Related","functionality",":",":","class",":","`",".Ensemble","`","for","only","random","initialisation","."]
docstr: This method trains the ensemble on the data given in train_data and train_targets. If validation data are
  also given, then the training metrics are evaluated on these data and results printed on screen if verbose
  level is greater than 0.
  
  The method trains each model in the ensemble in series for the number of epochs specified. Training can
  also stop early with the best model as evaluated on the validation data, if use_early_stopping is enabled.
  
  Each model in the ensemble is trained using a bootstrapped sample of the data (the train data are re-sampled
  with replacement.) The number of bootstrap samples can be specified via the bag_size parameter; by default,
  the number of bootstrap samples equals the number of training points.
  
  For detail descriptions of Keras-specific parameters consult the Keras documentation
  at https://keras.io/models/sequential/
  
  Args:
      generator: The generator object for training data. It should be one of type
          GraphSAGENodeGenerator, HinSAGENodeGenerator, FullBatchNodeGenerator, GraphSAGELinkGenerator,
          or HinSAGELinkGenerator.
      train_data (iterable): It is an iterable, e.g. list, that specifies the data
          to train the model with.
      train_targets (iterable): It is an iterable, e.g. list, that specifies the target
          values for the train data.
      steps_per_epoch (None or int): (Keras-specific parameter) If not None, it specifies the number of steps
          to yield from the generator before declaring one epoch finished and starting a new epoch.
      epochs (int): (Keras-specific parameter) The number of training epochs.
      verbose (int): (Keras-specific parameter) The verbosity mode that should be 0 , 1, or 2 meaning silent,
          progress bar, and one line per epoch respectively.
      validation_data: A generator for validation data that is optional (None). If not None then, it should
          be one of type GraphSAGENodeGenerator, HinSAGENodeGenerator, FullBatchNodeGenerator,
          GraphSAGELinkGenerator, or HinSAGELinkGenerator.
      validation_steps (None or int): (Keras-specific parameter) If validation_generator is not None, then it
          specifies the number of steps to yield from the generator before stopping at the end of every epoch.
      class_weight (None or dict): (Keras-specific parameter) If not None, it should be a dictionary
          mapping class indices (integers) to a weight (float) value, used for weighting the loss function (during
          training only). This can be useful to tell the model to "pay more attention" to samples from an
          under-represented class.
      max_queue_size (int): (Keras-specific parameter) The maximum size for the generator queue.
      workers (int): (Keras-specific parameter) The maximum number of workers to use.
      use_multiprocessing (bool): (Keras-specific parameter) If True then use process based threading.
      shuffle (bool): (Keras-specific parameter) If True, then it shuffles the order of batches at the
          beginning of each training epoch.
      initial_epoch (int): (Keras-specific parameter) Epoch at which to start training (useful for resuming a
          previous training run).
      bag_size (None or int): The number of samples in a bootstrap sample. If None and bagging is used, then
          the number of samples is equal to the number of training points.
      use_early_stopping (bool): If set to True, then early stopping is used when training each model
          in the ensemble. The default is False.
      early_stopping_monitor (str): The quantity to monitor for early stopping, e.g., 'val_loss',
          'val_weighted_acc'. It should be a valid Keras metric.
  
  Returns:
      list: It returns a list of Keras History objects each corresponding to one trained model in the ensemble.
docstr_tok: ["This","method","trains","the","ensemble","on","the","data","given","in","train_data","and","train_targets",".","If","validation","data","are","also","given",",","then","the","training","metrics","are","evaluated","on","these","data","and","results","printed","on","screen","if","verbose","level","is","greater","than","0",".","The","method","trains","each","model","in","the","ensemble","in","series","for","the","number","of","epochs","specified",".","Training","can","also","stop","early","with","the","best","model","as","evaluated","on","the","validation","data",",","if","use_early_stopping","is","enabled",".","Each","model","in","the","ensemble","is","trained","using","a","bootstrapped","sample","of","the","data","(","the","train","data","are","re-sampled","with","replacement",".",")","The","number","of","bootstrap","samples","can","be","specified","via","the","bag_size","parameter",";","by","default",",","the","number","of","bootstrap","samples","equals","the","number","of","training","points",".","For","detail","descriptions","of","Keras-specific","parameters","consult","the","Keras","documentation","at","https",":","\/\/keras.io\/models\/sequential\/","Args",":","generator",":","The","generator","object","for","training","data",".","It","should","be","one","of","type","GraphSAGENodeGenerator",",","HinSAGENodeGenerator",",","FullBatchNodeGenerator",",","GraphSAGELinkGenerator",",","or","HinSAGELinkGenerator",".","train_data","(","iterable",")",":","It","is","an","iterable",",","e.g",".","list",",","that","specifies","the","data","to","train","the","model","with",".","train_targets","(","iterable",")",":","It","is","an","iterable",",","e.g",".","list",",","that","specifies","the","target","values","for","the","train","data",".","steps_per_epoch","(","None","or","int",")",":","(","Keras-specific","parameter",")","If","not","None",",","it","specifies","the","number","of","steps","to","yield","from","the","generator","before","declaring","one","epoch","finished","and","starting","a","new","epoch",".","epochs","(","int",")",":","(","Keras-specific","parameter",")","The","number","of","training","epochs",".","verbose","(","int",")",":","(","Keras-specific","parameter",")","The","verbosity","mode","that","should","be","0",",","1",",","or","2","meaning","silent",",","progress","bar",",","and","one","line","per","epoch","respectively",".","validation_data",":","A","generator","for","validation","data","that","is","optional","(","None",")",".","If","not","None","then",",","it","should","be","one","of","type","GraphSAGENodeGenerator",",","HinSAGENodeGenerator",",","FullBatchNodeGenerator",",","GraphSAGELinkGenerator",",","or","HinSAGELinkGenerator",".","validation_steps","(","None","or","int",")",":","(","Keras-specific","parameter",")","If","validation_generator","is","not","None",",","then","it","specifies","the","number","of","steps","to","yield","from","the","generator","before","stopping","at","the","end","of","every","epoch",".","class_weight","(","None","or","dict",")",":","(","Keras-specific","parameter",")","If","not","None",",","it","should","be","a","dictionary","mapping","class","indices","(","integers",")","to","a","weight","(","float",")","value",",","used","for","weighting","the","loss","function","(","during","training","only",")",".","This","can","be","useful","to","tell","the","model","to","``","pay","more","attention","''","to","samples","from","an","under-represented","class",".","max_queue_size","(","int",")",":","(","Keras-specific","parameter",")","The","maximum","size","for","the","generator","queue",".","workers","(","int",")",":","(","Keras-specific","parameter",")","The","maximum","number","of","workers","to","use",".","use_multiprocessing","(","bool",")",":","(","Keras-specific","parameter",")","If","True","then","use","process","based","threading",".","shuffle","(","bool",")",":","(","Keras-specific","parameter",")","If","True",",","then","it","shuffles","the","order","of","batches","at","the","beginning","of","each","training","epoch",".","initial_epoch","(","int",")",":","(","Keras-specific","parameter",")","Epoch","at","which","to","start","training","(","useful","for","resuming","a","previous","training","run",")",".","bag_size","(","None","or","int",")",":","The","number","of","samples","in","a","bootstrap","sample",".","If","None","and","bagging","is","used",",","then","the","number","of","samples","is","equal","to","the","number","of","training","points",".","use_early_stopping","(","bool",")",":","If","set","to","True",",","then","early","stopping","is","used","when","training","each","model","in","the","ensemble",".","The","default","is","False",".","early_stopping_monitor","(","str",")",":","The","quantity","to","monitor","for","early","stopping",",","e.g.",",","'val_loss","'",",","'val_weighted_acc","'",".","It","should","be","a","valid","Keras","metric",".","Returns",":","list",":","It","returns","a","list","of","Keras","History","objects","each","corresponding","to","one","trained","model","in","the","ensemble","."]
code: def fit(
      self,
      generator,
      train_data,
      train_targets,
      steps_per_epoch=None,
      epochs=1,
      verbose=1,
      validation_data=None,
      validation_steps=None,
      class_weight=None,
      max_queue_size=10,
      workers=1,
      use_multiprocessing=False,
      shuffle=True,
      initial_epoch=0,
      bag_size=None,
      use_early_stopping=False,
      early_stopping_monitor="val_loss",
  ):
      """
      This method trains the ensemble on the data given in train_data and train_targets. If validation data are
      also given, then the training metrics are evaluated on these data and results printed on screen if verbose
      level is greater than 0.
  
      The method trains each model in the ensemble in series for the number of epochs specified. Training can
      also stop early with the best model as evaluated on the validation data, if use_early_stopping is enabled.
  
      Each model in the ensemble is trained using a bootstrapped sample of the data (the train data are re-sampled
      with replacement.) The number of bootstrap samples can be specified via the bag_size parameter; by default,
      the number of bootstrap samples equals the number of training points.
  
      For detail descriptions of Keras-specific parameters consult the Keras documentation
      at https://keras.io/models/sequential/
  
      Args:
          generator: The generator object for training data. It should be one of type
              GraphSAGENodeGenerator, HinSAGENodeGenerator, FullBatchNodeGenerator, GraphSAGELinkGenerator,
              or HinSAGELinkGenerator.
          train_data (iterable): It is an iterable, e.g. list, that specifies the data
              to train the model with.
          train_targets (iterable): It is an iterable, e.g. list, that specifies the target
              values for the train data.
          steps_per_epoch (None or int): (Keras-specific parameter) If not None, it specifies the number of steps
              to yield from the generator before declaring one epoch finished and starting a new epoch.
          epochs (int): (Keras-specific parameter) The number of training epochs.
          verbose (int): (Keras-specific parameter) The verbosity mode that should be 0 , 1, or 2 meaning silent,
              progress bar, and one line per epoch respectively.
          validation_data: A generator for validation data that is optional (None). If not None then, it should
              be one of type GraphSAGENodeGenerator, HinSAGENodeGenerator, FullBatchNodeGenerator,
              GraphSAGELinkGenerator, or HinSAGELinkGenerator.
          validation_steps (None or int): (Keras-specific parameter) If validation_generator is not None, then it
              specifies the number of steps to yield from the generator before stopping at the end of every epoch.
          class_weight (None or dict): (Keras-specific parameter) If not None, it should be a dictionary
              mapping class indices (integers) to a weight (float) value, used for weighting the loss function (during
              training only). This can be useful to tell the model to "pay more attention" to samples from an
              under-represented class.
          max_queue_size (int): (Keras-specific parameter) The maximum size for the generator queue.
          workers (int): (Keras-specific parameter) The maximum number of workers to use.
          use_multiprocessing (bool): (Keras-specific parameter) If True then use process based threading.
          shuffle (bool): (Keras-specific parameter) If True, then it shuffles the order of batches at the
              beginning of each training epoch.
          initial_epoch (int): (Keras-specific parameter) Epoch at which to start training (useful for resuming a
              previous training run).
          bag_size (None or int): The number of samples in a bootstrap sample. If None and bagging is used, then
              the number of samples is equal to the number of training points.
          use_early_stopping (bool): If set to True, then early stopping is used when training each model
              in the ensemble. The default is False.
          early_stopping_monitor (str): The quantity to monitor for early stopping, e.g., 'val_loss',
              'val_weighted_acc'. It should be a valid Keras metric.
  
      Returns:
          list: It returns a list of Keras History objects each corresponding to one trained model in the ensemble.
  
      """
      if not isinstance(
          generator,
          (
              sg.mapper.GraphSAGENodeGenerator,
              sg.mapper.HinSAGENodeGenerator,
              sg.mapper.FullBatchNodeGenerator,
              sg.mapper.GraphSAGELinkGenerator,
              sg.mapper.HinSAGELinkGenerator,
          ),
      ):
          raise ValueError(
              "({}) generator parameter must be of type GraphSAGENodeGenerator, HinSAGENodeGenerator, "
              "FullBatchNodeGenerator, GraphSAGELinkGenerator, or HinSAGELinkGenerator if you want to use Bagging. "
              "Received type {}".format(
                  type(self).__name__, type(generator).__name__
              )
          )
      if bag_size is not None and (
          bag_size > len(train_data) or bag_size <= 0
      ):
          raise ValueError(
              "({}) bag_size must be positive and less than or equal to the number of training points ({})".format(
                  type(self).__name__, len(train_data)
              )
          )
      if train_targets is None:
          raise ValueError(
              "({}) If train_data is given then train_targets must be given as well.".format(
                  type(self).__name__
              )
          )
  
      self.history = []
  
      num_points_per_bag = (
          bag_size if bag_size is not None else len(train_data)
      )
  
      # Prepare the training data for each model. Use sampling with replacement to create len(self.models)
      # datasets.
      for model in self.models:
          di_index = np.random.choice(
              len(train_data), size=num_points_per_bag
          )  # sample with replacement
          di_train = train_data[di_index]
  
          di_targets = train_targets[di_index]
  
          di_gen = generator.flow(di_train, di_targets)
  
          es_callback = None
          if use_early_stopping and validation_data is not None:
              es_callback = [
                  EarlyStopping(
                      monitor=early_stopping_monitor,
                      patience=self.early_stoppping_patience,
                      restore_best_weights=True,
                  )
              ]
  
          self.history.append(
              model.fit(
                  di_gen,
                  steps_per_epoch=steps_per_epoch,
                  epochs=epochs,
                  verbose=verbose,
                  callbacks=es_callback,
                  validation_data=validation_data,
                  validation_steps=validation_steps,
                  class_weight=class_weight,
                  max_queue_size=max_queue_size,
                  workers=workers,
                  use_multiprocessing=use_multiprocessing,
                  shuffle=shuffle,
                  initial_epoch=initial_epoch,
              )
          )
  
      return self.history
  
imports: from stellargraph.layer import *
  import numpy
  from tensorflow import keras
  from tensorflow.keras.callbacks import EarlyStopping
  import stellargraph
label_desc: Use image node_id 2 for calling the BaggingEnsemble obj's underlying member method code with example usage: obj.fit(generator, train_data, train_targets, steps_per_epoch, epochs, verbose, validation_data, validation_steps, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch, bag_size, use_early_stopping, early_stopping_monitor) and returns: self