code: def build_lstm_policy(
      model_config,
      value_network=None,
      estimate_q=False,
      **policy_kwargs,
  ):
      """
      Build lstm policy and value network, they share the same lstm network.
      the parameters all use their default values.
  
      Parameter
      ---------
      model_config : obj
          Configurations of the model
      value_network : obj
          The network for value function
      estimate_q : bool
          Whether to estimate ``q``
      **policy_kwargs
          The kwargs for policy network, i.e., lstm model
  
      Returns
      -------
      func
          The policy network
      """
      policy_network = lstm_model(**policy_kwargs)
  
      def policy_fn(
          nbatch=None,
          nsteps=None,
          sess=None,
          observ_placeholder=None,
          np_mask=None,
          is_act_model=False,
      ):
          ob_space = model_config.observation_space
  
          X = (
              observ_placeholder
              if observ_placeholder is not None
              else observation_placeholder(ob_space, batch_size=nbatch)
          )
  
          extra_tensors = {}
  
          # encode_observation is not necessary anymore as we use embedding_lookup
          encoded_x = X
  
          with tf.variable_scope("pi", reuse=tf.AUTO_REUSE):
              policy_latent = policy_network(
                  encoded_x, 1, model_config.observation_space.n
              )
              if isinstance(policy_latent, tuple):
                  policy_latent, recurrent_tensors = policy_latent
  
                  if recurrent_tensors is not None:
                      # recurrent architecture, need a few more steps
                      nenv = nbatch // nsteps
                      assert (
                          nenv > 0
                      ), "Bad input for recurrent policy: batch size {} smaller than nsteps {}".format(
                          nbatch, nsteps
                      )
                      policy_latent, recurrent_tensors = policy_network(
                          encoded_x,
                          nenv,
                          model_config.observation_space.n,
                      )
                      extra_tensors.update(recurrent_tensors)
  
          _v_net = value_network
  
          assert _v_net is None or _v_net == "shared"
          vf_latent = policy_latent
  
          policy = PolicyWithValue(
              env=model_config,
              observations=X,
              latent=policy_latent,
              vf_latent=vf_latent,
              sess=sess,
              estimate_q=estimate_q,
              np_mask=np_mask,
              is_act_model=is_act_model,
              **extra_tensors,
          )
          return policy
  
      return policy_fn
  
file_path: nni/nni/algorithms/hpo/ppo_tuner/policy.py
imports: import tensorflow.compat.v1
  from .distri import CategoricalPdType
  from .util import lstm_model, fc, observation_placeholder, adjust_shape
label_desc: Use image node_id 1 for calling a global function with example usage: build_lstm_policy(model_config, value_network, estimate_q) and returns: policy_fn, policy