code: def train(train_data, test_data=None):
      G = train_data[0]
      features = train_data[1]
      id_map = train_data[2]
      class_map = train_data[4]
      if isinstance(list(class_map.values())[0], list):
          num_classes = len(list(class_map.values())[0])
      else:
          num_classes = len(set(class_map.values()))
  
      if not features is None:
          # pad with dummy zero vector
          features = np.vstack(
              [features, np.zeros((features.shape[1],))]
          )
  
      context_pairs = train_data[3] if FLAGS.random_context else None
      placeholders = construct_placeholders(num_classes)
      minibatch = NodeMinibatchIterator(
          G,
          id_map,
          placeholders,
          class_map,
          num_classes,
          batch_size=FLAGS.batch_size,
          max_degree=FLAGS.max_degree,
          context_pairs=context_pairs,
      )
      adj_info_ph = tf.placeholder(tf.int32, shape=minibatch.adj.shape)
      adj_info = tf.Variable(
          adj_info_ph, trainable=False, name="adj_info"
      )
  
      if FLAGS.model == "graphsage_mean":
          # Create model
          sampler = UniformNeighborSampler(adj_info)
          if FLAGS.samples_3 != 0:
              layer_infos = [
                  SAGEInfo(
                      "node", sampler, FLAGS.samples_1, FLAGS.dim_1
                  ),
                  SAGEInfo(
                      "node", sampler, FLAGS.samples_2, FLAGS.dim_2
                  ),
                  SAGEInfo(
                      "node", sampler, FLAGS.samples_3, FLAGS.dim_2
                  ),
              ]
          elif FLAGS.samples_2 != 0:
              layer_infos = [
                  SAGEInfo(
                      "node", sampler, FLAGS.samples_1, FLAGS.dim_1
                  ),
                  SAGEInfo(
                      "node", sampler, FLAGS.samples_2, FLAGS.dim_2
                  ),
              ]
          else:
              layer_infos = [
                  SAGEInfo(
                      "node", sampler, FLAGS.samples_1, FLAGS.dim_1
                  )
              ]
  
          model = SupervisedGraphsage(
              num_classes,
              placeholders,
              features,
              adj_info,
              minibatch.deg,
              layer_infos,
              model_size=FLAGS.model_size,
              sigmoid_loss=FLAGS.sigmoid,
              identity_dim=FLAGS.identity_dim,
              logging=True,
          )
      elif FLAGS.model == "gcn":
          # Create model
          sampler = UniformNeighborSampler(adj_info)
          layer_infos = [
              SAGEInfo(
                  "node", sampler, FLAGS.samples_1, 2 * FLAGS.dim_1
              ),
              SAGEInfo(
                  "node", sampler, FLAGS.samples_2, 2 * FLAGS.dim_2
              ),
          ]
  
          model = SupervisedGraphsage(
              num_classes,
              placeholders,
              features,
              adj_info,
              minibatch.deg,
              layer_infos=layer_infos,
              aggregator_type="gcn",
              model_size=FLAGS.model_size,
              concat=False,
              sigmoid_loss=FLAGS.sigmoid,
              identity_dim=FLAGS.identity_dim,
              logging=True,
          )
  
      elif FLAGS.model == "graphsage_seq":
          sampler = UniformNeighborSampler(adj_info)
          layer_infos = [
              SAGEInfo("node", sampler, FLAGS.samples_1, FLAGS.dim_1),
              SAGEInfo("node", sampler, FLAGS.samples_2, FLAGS.dim_2),
          ]
  
          model = SupervisedGraphsage(
              num_classes,
              placeholders,
              features,
              adj_info,
              minibatch.deg,
              layer_infos=layer_infos,
              aggregator_type="seq",
              model_size=FLAGS.model_size,
              sigmoid_loss=FLAGS.sigmoid,
              identity_dim=FLAGS.identity_dim,
              logging=True,
          )
  
      elif FLAGS.model == "graphsage_maxpool":
          sampler = UniformNeighborSampler(adj_info)
          layer_infos = [
              SAGEInfo("node", sampler, FLAGS.samples_1, FLAGS.dim_1),
              SAGEInfo("node", sampler, FLAGS.samples_2, FLAGS.dim_2),
          ]
  
          model = SupervisedGraphsage(
              num_classes,
              placeholders,
              features,
              adj_info,
              minibatch.deg,
              layer_infos=layer_infos,
              aggregator_type="maxpool",
              model_size=FLAGS.model_size,
              sigmoid_loss=FLAGS.sigmoid,
              identity_dim=FLAGS.identity_dim,
              logging=True,
          )
  
      elif FLAGS.model == "graphsage_meanpool":
          sampler = UniformNeighborSampler(adj_info)
          layer_infos = [
              SAGEInfo("node", sampler, FLAGS.samples_1, FLAGS.dim_1),
              SAGEInfo("node", sampler, FLAGS.samples_2, FLAGS.dim_2),
          ]
  
          model = SupervisedGraphsage(
              num_classes,
              placeholders,
              features,
              adj_info,
              minibatch.deg,
              layer_infos=layer_infos,
              aggregator_type="meanpool",
              model_size=FLAGS.model_size,
              sigmoid_loss=FLAGS.sigmoid,
              identity_dim=FLAGS.identity_dim,
              logging=True,
          )
  
      else:
          raise Exception("Error: model name unrecognized.")
  
      config = tf.ConfigProto(
          log_device_placement=FLAGS.log_device_placement
      )
      config.gpu_options.allow_growth = True
      # config.gpu_options.per_process_gpu_memory_fraction = GPU_MEM_FRACTION
      config.allow_soft_placement = True
  
      # Initialize session
      sess = tf.Session(config=config)
      merged = tf.summary.merge_all()
      summary_writer = tf.summary.FileWriter(log_dir(), sess.graph)
  
      # Init variables
      sess.run(
          tf.global_variables_initializer(),
          feed_dict={adj_info_ph: minibatch.adj},
      )
  
      # Train model
  
      total_steps = 0
      avg_time = 0.0
      epoch_val_costs = []
  
      train_adj_info = tf.assign(adj_info, minibatch.adj)
      val_adj_info = tf.assign(adj_info, minibatch.test_adj)
      for epoch in range(FLAGS.epochs):
          minibatch.shuffle()
  
          iter = 0
          print("Epoch: %04d" % (epoch + 1))
          epoch_val_costs.append(0)
          while not minibatch.end():
              # Construct feed dictionary
              feed_dict, labels = minibatch.next_minibatch_feed_dict()
              feed_dict.update({placeholders["dropout"]: FLAGS.dropout})
  
              t = time.time()
              # Training step
              outs = sess.run(
                  [merged, model.opt_op, model.loss, model.preds],
                  feed_dict=feed_dict,
              )
              train_cost = outs[2]
  
              if iter % FLAGS.validate_iter == 0:
                  # Validation
                  sess.run(val_adj_info.op)
                  if FLAGS.validate_batch_size == -1:
                      (
                          val_cost,
                          val_f1_mic,
                          val_f1_mac,
                          duration,
                      ) = incremental_evaluate(
                          sess, model, minibatch, FLAGS.batch_size
                      )
                  else:
                      (
                          val_cost,
                          val_f1_mic,
                          val_f1_mac,
                          duration,
                      ) = evaluate(
                          sess,
                          model,
                          minibatch,
                          FLAGS.validate_batch_size,
                      )
                  sess.run(train_adj_info.op)
                  epoch_val_costs[-1] += val_cost
  
              if total_steps % FLAGS.print_every == 0:
                  summary_writer.add_summary(outs[0], total_steps)
  
              # Print results
              avg_time = (avg_time * total_steps + time.time() - t) / (
                  total_steps + 1
              )
  
              if total_steps % FLAGS.print_every == 0:
                  train_f1_mic, train_f1_mac = calc_f1(labels, outs[-1])
                  print(
                      "Iter:",
                      "%04d" % iter,
                      "train_loss=",
                      "{:.5f}".format(train_cost),
                      "train_f1_mic=",
                      "{:.5f}".format(train_f1_mic),
                      "train_f1_mac=",
                      "{:.5f}".format(train_f1_mac),
                      "val_loss=",
                      "{:.5f}".format(val_cost),
                      "val_f1_mic=",
                      "{:.5f}".format(val_f1_mic),
                      "val_f1_mac=",
                      "{:.5f}".format(val_f1_mac),
                      "time=",
                      "{:.5f}".format(avg_time),
                  )
  
              iter += 1
              total_steps += 1
  
              if total_steps > FLAGS.max_total_steps:
                  break
  
          if total_steps > FLAGS.max_total_steps:
              break
  
      print("Optimization Finished!")
      sess.run(val_adj_info.op)
      val_cost, val_f1_mic, val_f1_mac, duration = incremental_evaluate(
          sess, model, minibatch, FLAGS.batch_size
      )
      print(
          "Full validation stats:",
          "loss=",
          "{:.5f}".format(val_cost),
          "f1_micro=",
          "{:.5f}".format(val_f1_mic),
          "f1_macro=",
          "{:.5f}".format(val_f1_mac),
          "time=",
          "{:.5f}".format(duration),
      )
      with open(log_dir() + "val_stats.txt", "w") as fp:
          fp.write(
              "loss={:.5f} f1_micro={:.5f} f1_macro={:.5f} time={:.5f}".format(
                  val_cost, val_f1_mic, val_f1_mac, duration
              )
          )
  
      print("Writing test set stats to file (don't peak!)")
      val_cost, val_f1_mic, val_f1_mac, duration = incremental_evaluate(
          sess, model, minibatch, FLAGS.batch_size, test=True
      )
      with open(log_dir() + "test_stats.txt", "w") as fp:
          fp.write(
              "loss={:.5f} f1_micro={:.5f} f1_macro={:.5f}".format(
                  val_cost, val_f1_mic, val_f1_mac
              )
          )
  
file_path: GraphSAGE/graphsage/supervised_train.py
imports: from __future__ import division
  from __future__ import print_function
  import os
  import time
  import tensorflow
  import numpy
  import sklearn
  from sklearn import metrics
  from graphsage.supervised_models import SupervisedGraphsage
  from graphsage.models import SAGEInfo
  from graphsage.minibatch import NodeMinibatchIterator
  from graphsage.neigh_samplers import UniformNeighborSampler
  from graphsage.utils import load_data
label_desc: Use image node_id 6 for calling a global function with example usage: train(train_data, test_data) without return types