class_docstr: A Pytorch implementation of NetAdapt compression algorithm.
  
  Parameters
  ----------
  model : pytorch model
      The model to be pruned.
  config_list : list
      Supported keys:
          - sparsity : The target overall sparsity.
          - op_types : The operation type to prune.
  short_term_fine_tuner : function
      function to short-term fine tune the masked model.
      This function should include `model` as the only parameter,
      and fine tune the model for a short term after each pruning iteration.
      Example::
  
          def short_term_fine_tuner(model, epoch=3):
              device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
              train_loader = ...
              criterion = torch.nn.CrossEntropyLoss()
              optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
              model.train()
              for _ in range(epoch):
                  for batch_idx, (data, target) in enumerate(train_loader):
                      data, target = data.to(device), target.to(device)
                      optimizer.zero_grad()
                      output = model(data)
                      loss = criterion(output, target)
                      loss.backward()
                      optimizer.step()
  evaluator : function
      function to evaluate the masked model.
      This function should include `model` as the only parameter, and returns a scalar value.
      Example::
  
          def evaluator(model):
              device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
              val_loader = ...
              model.eval()
              correct = 0
              with torch.no_grad():
                  for data, target in val_loader:
                      data, target = data.to(device), target.to(device)
                      output = model(data)
                      # get the index of the max log-probability
                      pred = output.argmax(dim=1, keepdim=True)
                      correct += pred.eq(target.view_as(pred)).sum().item()
              accuracy = correct / len(val_loader.dataset)
              return accuracy
  optimize_mode : str
      optimize mode, `maximize` or `minimize`, by default `maximize`.
  base_algo : str
      Base pruning algorithm. `level`, `l1`, `l2` or `fpgm`, by default `l1`. Given the sparsity distribution among the ops,
      the assigned `base_algo` is used to decide which filters/channels/weights to prune.
  sparsity_per_iteration : float
      sparsity to prune in each iteration.
  experiment_data_dir : str
      PATH to save experiment data,
      including the config_list generated for the base pruning algorithm and the performance of the pruned model.
class_docstr_tok: ["A","Pytorch","implementation","of","NetAdapt","compression","algorithm",".","Parameters","--","--","--","--","--","model",":","pytorch","model","The","model","to","be","pruned",".","config_list",":","list","Supported","keys",":","-","sparsity",":","The","target","overall","sparsity",".","-","op_types",":","The","operation","type","to","prune",".","short_term_fine_tuner",":","function","function","to","short-term","fine","tune","the","masked","model",".","This","function","should","include","`","model","`","as","the","only","parameter",",","and","fine","tune","the","model","for","a","short","term","after","each","pruning","iteration",".","Example",":",":","def","short_term_fine_tuner","(","model",",","epoch=3",")",":","device","=","torch.device","(","``","cuda","''","if","torch.cuda.is_available","(",")","else","``","cpu","''",")","train_loader","=","...","criterion","=","torch.nn.CrossEntropyLoss","(",")","optimizer","=","torch.optim.SGD","(","model.parameters","(",")",",","lr=0.01",")","model.train","(",")","for","_","in","range","(","epoch",")",":","for","batch_idx",",","(","data",",","target",")","in","enumerate","(","train_loader",")",":","data",",","target","=","data.to","(","device",")",",","target.to","(","device",")","optimizer.zero_grad","(",")","output","=","model","(","data",")","loss","=","criterion","(","output",",","target",")","loss.backward","(",")","optimizer.step","(",")","evaluator",":","function","function","to","evaluate","the","masked","model",".","This","function","should","include","`","model","`","as","the","only","parameter",",","and","returns","a","scalar","value",".","Example",":",":","def","evaluator","(","model",")",":","device","=","torch.device","(","``","cuda","''","if","torch.cuda.is_available","(",")","else","``","cpu","''",")","val_loader","=","...","model.eval","(",")","correct","=","0","with","torch.no_grad","(",")",":","for","data",",","target","in","val_loader",":","data",",","target","=","data.to","(","device",")",",","target.to","(","device",")","output","=","model","(","data",")","#","get","the","index","of","the","max","log-probability","pred","=","output.argmax","(","dim=1",",","keepdim=True",")","correct","+=","pred.eq","(","target.view_as","(","pred",")",")",".sum","(",")",".item","(",")","accuracy","=","correct","\/","len","(","val_loader.dataset",")","return","accuracy","optimize_mode",":","str","optimize","mode",",","`","maximize","`","or","`","minimize","`",",","by","default","`","maximize","`",".","base_algo",":","str","Base","pruning","algorithm",".","`","level","`",",","`","l1","`",",","`","l2","`","or","`","fpgm","`",",","by","default","`","l1","`",".","Given","the","sparsity","distribution","among","the","ops",",","the","assigned","`","base_algo","`","is","used","to","decide","which","filters\/channels\/weights","to","prune",".","sparsity_per_iteration",":","float","sparsity","to","prune","in","each","iteration",".","experiment_data_dir",":","str","PATH","to","save","experiment","data",",","including","the","config_list","generated","for","the","base","pruning","algorithm","and","the","performance","of","the","pruned","model","."]
code: def __init__(
      self,
      model,
      config_list,
      short_term_fine_tuner,
      evaluator,
      optimize_mode="maximize",
      base_algo="l1",
      sparsity_per_iteration=0.05,
      experiment_data_dir="./",
  ):
      # models used for iterative pruning and evaluation
      self._model_to_prune = copy.deepcopy(model)
      self._base_algo = base_algo
  
      super().__init__(model, config_list)
  
      self._short_term_fine_tuner = short_term_fine_tuner
      self._evaluator = evaluator
      self._optimize_mode = OptimizeMode(optimize_mode)
  
      # hyper parameters for NetAdapt algorithm
      self._sparsity_per_iteration = sparsity_per_iteration
  
      # overall pruning rate
      self._sparsity = config_list[0]["sparsity"]
  
      # config_list
      self._config_list_generated = []
  
      self._experiment_data_dir = experiment_data_dir
      if not os.path.exists(self._experiment_data_dir):
          os.makedirs(self._experiment_data_dir)
  
      self._tmp_model_path = os.path.join(
          self._experiment_data_dir, "tmp_model.pth"
      )
  
imports: import logging
  import os
  import copy
  import json
  import torch
  from schema import And, Optional
  from ..torch_utils import OptimizeMode
  from ..compressor import Pruner
  from ..utils.config_validation import CompressorSchema
  from ..utils.num_param_counter import get_total_num_weights
  from .constants_pruner import PRUNER_DICT
label_desc: Use image node_id 1 to create a new NetAdaptPruner object from inherited base classes: Pruner with example: obj = NetAdaptPruner(model, config_list, short_term_fine_tuner, evaluator, optimize_mode, base_algo, sparsity_per_iteration, experiment_data_dir)