code: def linear_classification(args):
      import torch
      from torch import optim
      from torch.nn import functional as F
      from .network import NodeClassifier
  
      def generate_one_vs_rest(indexes, labels):
          new_indexes = []
          new_labels = []
          num_class = labels.shape[1]
          for index, sample_labels in zip(indexes, labels):
              for cls in np.where(sample_labels)[0]:
                  new_indexes.append(index)
                  new_label = np.zeros(num_class, dtype=np.int)
                  new_label[cls] = 1
                  new_labels.append(new_label)
          return torch.as_tensor(new_indexes), torch.as_tensor(
              new_labels
          )
  
      (
          embeddings,
          labels,
          portion,
          normalization,
          times,
          patience,
          gpu,
      ) = args
      num_sample, num_class = labels.shape
      num_train = int(num_sample * portion)
  
      macro_f1s = []
      micro_f1s = []
      for t in range(times):
          samples = np.random.permutation(num_sample)
          train_samples = samples[:num_train]
          train_labels = np.asarray(labels[train_samples])
          train_samples, train_labels = generate_one_vs_rest(
              train_samples, train_labels
          )
          test_samples = torch.as_tensor(samples[num_train:])
          test_labels = torch.as_tensor(labels[test_samples])
  
          model = NodeClassifier(
              embeddings, num_class, normalization=normalization
          )
  
          train_samples = train_samples.cuda(gpu)
          train_labels = train_labels.cuda(gpu)
          test_samples = test_samples.cuda(gpu)
          test_labels = test_labels.cuda(gpu)
          model = model.cuda(gpu)
  
          # train
          optimizer = optim.SGD(
              model.parameters(), lr=1, weight_decay=2e-5, momentum=0.9
          )
          best_loss = float("inf")
          best_epoch = -1
          for epoch in range(100000):
              optimizer.zero_grad()
              logits = model(train_samples)
              loss = F.binary_cross_entropy_with_logits(
                  logits, train_labels.float()
              )
              loss.backward()
              optimizer.step()
  
              loss = loss.item()
              if loss < best_loss:
                  best_epoch = epoch
                  best_loss = loss
              if epoch == best_epoch + patience:
                  break
  
          # test
          logits = model(test_samples)
          num_labels = test_labels.sum(dim=1, keepdim=True)
          sorted, _ = logits.sort(dim=1, descending=True)
          thresholds = sorted.gather(dim=1, index=num_labels - 1)
          predictions = (logits >= thresholds).int()
          # compute metric
          num_TP_per_class = (
              (predictions & test_labels).sum(dim=0).float()
          )
          num_T_per_class = test_labels.sum(dim=0).float()
          num_P_per_class = predictions.sum(dim=0).float()
          macro_f1s.append(
              (
                  2
                  * num_TP_per_class
                  / (num_T_per_class + num_P_per_class)
              )
              .mean()
              .item()
          )
          num_TP = (predictions & test_labels).sum().float()
          num_T = test_labels.sum().float()
          num_P = predictions.sum().float()
          micro_f1s.append((2 * num_TP / (num_T + num_P)).item())
  
      return {
          "macro-F1@%g%%" % (portion * 100): np.mean(macro_f1s),
          "micro-F1@%g%%" % (portion * 100): np.mean(micro_f1s),
      }
  
imports: from __future__ import print_function, absolute_import, unicode_literals, division
  import os
  import re
  import pickle
  import logging
  import multiprocessing
  from collections import defaultdict
  from future.builtins import str, map, range
  from easydict import EasyDict
  import numpy
  from ..None import lib, cfg, auto
  from ..None import graph, solver
  from ..util import assert_in, monitor, SharedNDArray
label_desc: Use image node_id 1 for calling a global function with example usage: linear_classification(args) and returns: dict, torch,  torch