code: def _sequence_from_episode(
      observations: acme_types.NestedTensor,
      actions: tf.Tensor,
      rewards: tf.Tensor,
      discounts: tf.Tensor,
      extra_spec: acme_types.NestedSpec,
      period: int,
      sequence_length: int,
  ):
      """Produce Reverb-like sequence from a full episode.
  
      Observations, actions, rewards and discounts have the same length. This
      function will ignore the first reward and discount and the last action.
  
      This function generates fake (all-zero) extras.
  
      See docs for reverb.SequenceAdder() for more details.
  
      Args:
        observations: [L, ...] Tensor.
        actions: [L, ...] Tensor.
        rewards: [L] Tensor.
        discounts: [L] Tensor.
        extra_spec: A possibly nested structure of specs for extras. This function
          will generate fake (all-zero) extras.
        period: The period with which we add sequences.
        sequence_length: The fixed length of sequences we wish to add.
  
      Returns:
        (o_t, a_t, r_t, d_t, e_t) Tuple.
      """
  
      length = tf.shape(rewards)[0]
      first = tf.random.uniform(
          shape=(), minval=0, maxval=length, dtype=tf.int32
      )
      first = first // period * period  # Get a multiple of `period`.
      to = tf.minimum(first + sequence_length, length)
  
      def _slice_and_pad(x):
          pad_length = sequence_length + first - to
          padding_shape = tf.concat(
              [[pad_length], tf.shape(x)[1:]], axis=0
          )
          result = tf.concat(
              [x[first:to], tf.zeros(padding_shape, x.dtype)], axis=0
          )
          result.set_shape([sequence_length] + x.shape.as_list()[1:])
          return result
  
      o_t = tree.map_structure(_slice_and_pad, observations)
      a_t = tree.map_structure(_slice_and_pad, actions)
      r_t = _slice_and_pad(rewards)
      d_t = _slice_and_pad(discounts)
      start_of_episode = tf.equal(first, 0)
      start_of_episode = tf.expand_dims(start_of_episode, axis=0)
      start_of_episode = tf.tile(start_of_episode, [sequence_length])
  
      def _sequence_zeros(spec):
          return tf.zeros([sequence_length] + spec.shape, spec.dtype)
  
      e_t = tree.map_structure(_sequence_zeros, extra_spec)
      info = tree.map_structure(
          lambda dtype: tf.ones([], dtype),
          reverb.SampleInfo.tf_dtypes(),
      )
      return reverb.ReplaySample(
          info=info,
          data=adders.Step(
              observation=o_t,
              action=a_t,
              reward=r_t,
              discount=d_t,
              start_of_episode=start_of_episode,
              extras=e_t,
          ),
      )
  
imports: import functools
  from typing import Optional
  from acme import datasets
  from acme import specs
  from acme import types
  from acme.adders import reverb
  from acme.agents import agent
  from acme.agents.tf import actors
  from acme.agents.tf.r2d2 import learning
  from acme.tf import savers
  from acme.tf import utils
  from acme.utils import counting
  from acme.utils import loggers
  import reverb
  import sonnet
  import tensorflow
  import tree
  import trfl
label_desc: Use image node_id 1 for calling a global function with example usage: _sequence_from_episode(observations, actions, rewards, discounts, extra_spec, period, sequence_length) and returns: reverb, result, tf