code: def loss_from_center(
      subs_net_list,
      target_feat_list,
      poison_batch,
      net_repeat,
      end2end,
      feature_layer,
  ) -> "torch.Tensor":
      """
      Calculate loss from center.
      """
      import torch
  
      if end2end:
          loss = torch.tensor(0.0)
          for net, center_feats in zip(subs_net_list, target_feat_list):
              poisons_feats: Union[
                  List[float], "torch.Tensor", np.ndarray
              ]
              if net_repeat > 1:
                  poisons_feats_repeats = [
                      net.get_activations(
                          poison_batch(),
                          layer=feature_layer,
                          framework=True,
                      )
                      for _ in range(net_repeat)
                  ]
                  block_num = len(poisons_feats_repeats[0])
                  poisons_feats = []
                  for block_idx in range(block_num):
                      poisons_feats.append(
                          sum(
                              [
                                  poisons_feat_r[block_idx]
                                  for poisons_feat_r in poisons_feats_repeats
                              ]
                          )
                          / net_repeat
                      )
              elif net_repeat == 1:
                  if isinstance(feature_layer, list):
                      poisons_feats = [
                          torch.flatten(
                              net.get_activations(poison_batch(), layer=layer, framework=True), 0  # type: ignore
                          )
                          for layer in feature_layer
                      ]
                  else:  # pragma: no cover
                      poisons_feats = net.get_activations(
                          poison_batch(),
                          layer=feature_layer,
                          framework=True,
                      )
              else:  # pragma: no cover
                  assert False, f"net_repeat set to {net_repeat}"
  
              net_loss = torch.tensor(0.0)
              for pfeat, cfeat in zip(poisons_feats, center_feats):
                  diff = torch.mean(pfeat, dim=0) - cfeat
                  diff_norm = torch.norm(diff, dim=0)
                  cfeat_norm = torch.norm(cfeat, dim=0)
                  diff_norm = diff_norm / cfeat_norm
                  net_loss += torch.mean(diff_norm)
              loss += net_loss / len(center_feats)
          loss = loss / len(subs_net_list)
  
      else:  # pragma: no cover
          loss = torch.tensor(0.0)
          for net, center in zip(subs_net_list, target_feat_list):
              poisons_list = [
                  net.get_activations(
                      poison_batch(),
                      layer=feature_layer,
                      framework=True,
                  )
                  for _ in range(net_repeat)
              ]
              poisons = torch.tensor(
                  sum(poisons_list) / len(poisons_list)
              )
  
              diff_2 = torch.mean(poisons, dim=0) - center
              diff_norm = torch.norm(diff_2, dim=1) / torch.norm(
                  center, dim=1
              )
              loss += torch.mean(diff_norm)
  
          loss = loss / len(subs_net_list)
  
      return loss
  
file_path: adversarial-robustness-toolbox/art/attacks/poisoning/bullseye_polytope_attack.py
imports: from __future__ import absolute_import, division, print_function, unicode_literals
  import logging
  import time
  from typing import Optional, Tuple, Union, TYPE_CHECKING, List
  import numpy
  from tqdm.auto import trange
  from art.attacks.attack import PoisoningAttackWhiteBox
  from art.estimators import BaseEstimator, NeuralNetworkMixin
  from art.estimators.classification.classifier import ClassifierMixin
  from art.estimators.classification.pytorch import PyTorchClassifier
label_desc: Use image node_id 2 for calling a global function with example usage: loss_from_center(subs_net_list, target_feat_list, poison_batch, net_repeat, end2end, feature_layer) and returns: loss