code: def _add_log_prob_and_value_to_episodes_recurrent(
      episodes,
      model,
      phi,
      batch_states,
      obs_normalizer,
      device,
  ):
      # Sort desc by lengths so that pack_sequence does not change the order
      episodes = sorted(episodes, key=len, reverse=True)
  
      # Prepare data for a recurrent model
      seqs_states = []
      seqs_next_states = []
      for ep in episodes:
          states = batch_states(
              [transition["state"] for transition in ep], device, phi
          )
          next_states = batch_states(
              [transition["next_state"] for transition in ep],
              device,
              phi,
          )
          if obs_normalizer:
              states = obs_normalizer(states, update=False)
              next_states = obs_normalizer(next_states, update=False)
          seqs_states.append(states)
          seqs_next_states.append(next_states)
  
      flat_transitions = flatten_sequences_time_first(episodes)
  
      # Predict values using a recurrent model
      with torch.no_grad(), pfrl.utils.evaluating(model):
          rs = concatenate_recurrent_states(
              [ep[0]["recurrent_state"] for ep in episodes]
          )
          next_rs = concatenate_recurrent_states(
              [ep[0]["next_recurrent_state"] for ep in episodes]
          )
          assert (
              (rs is None)
              or (next_rs is None)
              or (len(rs) == len(next_rs))
          )
  
          (flat_distribs, flat_vs), _ = pack_and_forward(
              model, seqs_states, rs
          )
          (_, flat_next_vs), _ = pack_and_forward(
              model, seqs_next_states, next_rs
          )
  
          flat_actions = torch.tensor(
              [b["action"] for b in flat_transitions], device=device
          )
          flat_log_probs = (
              flat_distribs.log_prob(flat_actions).cpu().numpy()
          )
          flat_vs = flat_vs.cpu().numpy()
          flat_next_vs = flat_next_vs.cpu().numpy()
  
      # Add predicted values to transitions
      for transition, log_prob, v, next_v in zip(
          flat_transitions, flat_log_probs, flat_vs, flat_next_vs
      ):
          transition["log_prob"] = float(log_prob)
          transition["v_pred"] = float(v)
          transition["next_v_pred"] = float(next_v)
  
imports: import collections
  import itertools
  import random
  import numpy
  import torch
  import torch.nn.functional
  import pfrl
  from pfrl import agent
  from pfrl.utils.batch_states import batch_states
  from pfrl.utils.mode_of_distribution import mode_of_distribution
  from pfrl.utils.recurrent import concatenate_recurrent_states, flatten_sequences_time_first, get_recurrent_state_at, mask_recurrent_state_at, one_step_forward, pack_and_forward
label_desc: Use image node_id 5 for calling a global function with example usage: _add_log_prob_and_value_to_episodes_recurrent(episodes, model, phi, batch_states, obs_normalizer, device) without return types