class_docstr: Adds PyTorch specific functions.
class_docstr_tok: ["Adds","PyTorch","specific","functions","."]
docstr: Build a model from a list of keywords.
  
  A BackendEngine should implement get_model in order to
  simplify the user's work. In particular, we will adhere
  to the following API where list_nodes is a list of neural network
  layers.
  
  Parameters
  ----------
  list_nodes: list
      list of keywords. Integer keywords indicate a layer with
      a number of nodes.
      Callable keywords are added to the model as a layer directly,
      which is useful for activation functions. String keywords are
      not supported in the Pytorch backend (try tensorflow instead).
  
  Returns
  -------
  model : torch.nn.Module
      initialized model with layers as specified.
docstr_tok: ["Build","a","model","from","a","list","of","keywords",".","A","BackendEngine","should","implement","get_model","in","order","to","simplify","the","user","'s","work",".","In","particular",",","we","will","adhere","to","the","following","API","where","list_nodes","is","a","list","of","neural","network","layers",".","Parameters","--","--","--","--","--","list_nodes",":","list","list","of","keywords",".","Integer","keywords","indicate","a","layer","with","a","number","of","nodes",".","Callable","keywords","are","added","to","the","model","as","a","layer","directly",",","which","is","useful","for","activation","functions",".","String","keywords","are","not","supported","in","the","Pytorch","backend","(","try","tensorflow","instead",")",".","Returns","--","--","--","-","model",":","torch.nn.Module","initialized","model","with","layers","as","specified","."]
code: def get_model(self, list_nodes):
      """
      Build a model from a list of keywords.
  
      A BackendEngine should implement get_model in order to
      simplify the user's work. In particular, we will adhere
      to the following API where list_nodes is a list of neural network
      layers.
  
      Parameters
      ----------
      list_nodes: list
          list of keywords. Integer keywords indicate a layer with
          a number of nodes.
          Callable keywords are added to the model as a layer directly,
          which is useful for activation functions. String keywords are
          not supported in the Pytorch backend (try tensorflow instead).
  
      Returns
      -------
      model : torch.nn.Module
          initialized model with layers as specified.
      """
  
      class FullyConnected(torch.nn.Module):
          """Neural network class."""
  
          def __init__(self):
              """Initialize the layers of the NN."""
              super(FullyConnected, self).__init__()
              layers = []
              nodes = None
              for i, item in enumerate(list_nodes):
                  if isinstance(item, int):
                      if nodes:
                          layers.append(
                              torch.nn.Linear(nodes, list_nodes[i])
                          )
                      nodes = item
                  elif callable(item):
                      layers.append(item)
                  elif isinstance(item, str):
                      if item.lower() == "sigmoid":
                          layers.append(torch.nn.Sigmoid())
                      elif item.lower() == "softmax":
                          layers.append(torch.nn.Softmax())
                      elif item.lower() == "relu":
                          layers.append(torch.nn.ReLU())
                      elif item.lower() == "leaky_relu":
                          layers.append(torch.nn.LeakyReLU())
                      else:
                          raise ValueError(
                              _MODEL_UNRECOGNIZED_STR.format(item)
                          )
                      # TODO support more strings? Or better option?
                      # possibly gather all activation classes, get __name__,
                      # and do pattern matching.
                  else:
                      raise ValueError(
                          _MODEL_UNRECOGNIZED_ITEM.format(item)
                      )
              self.layers_ = torch.nn.ModuleList(layers)
  
          def forward(self, x):
              """Propagate x through the network."""
              for layer in self.layers_:
                  x = layer(x)
              return x
  
      model = FullyConnected()
  
      def init_weights(m):
          if isinstance(m, torch.nn.Linear):
              torch.nn.init.xavier_uniform_(m.weight)
              m.bias.data.fill_(0.0)
  
      model.apply(init_weights)
  
      return model
  
file_path: fairlearn/fairlearn/adversarial/_pytorch_engine.py
imports: from ._backend_engine import BackendEngine
  from ._constants import _MODEL_UNRECOGNIZED_STR, _MODEL_UNRECOGNIZED_ITEM
label_desc: Use image node_id 8 for calling the PytorchEngine obj's underlying member method code with example usage: obj.get_model(list_nodes) and returns: model, x