class_docstr: A RL task: environment and a collection of trajectories.
class_docstr_tok: ["A","RL","task",":","environment","and","a","collection","of","trajectories","."]
docstr: Returns a random TimeStepBatch slice from a given trajectory.
docstr_tok: ["Returns","a","random","TimeStepBatch","slice","from","a","given","trajectory","."]
code: def _random_slice(self, trajectory, max_slice_length, margin):
      """Returns a random TimeStepBatch slice from a given trajectory."""
      # Sample a slice from the trajectory.
      slice_start = np.random.randint(
          _n_slices(trajectory, max_slice_length, margin)
      )
  
      # Convert the whole trajectory to Numpy while adding the margin. The
      # result is cached, so we don't have to repeat this for every sample.
      timestep_batch = trajectory.to_np(margin, self._timestep_to_np)
  
      # Slice and yield the result.
      slice_end = slice_start + (
          max_slice_length or timestep_batch.observation.shape[0]
      )
      return fastmath.nested_map(
          lambda x: x[slice_start:slice_end], timestep_batch
      )
  
imports: import collections
  import os
  import gin
  import gym
  import numpy
  from trax import fastmath
  from trax.rl import advantages
  from trax.supervised import training
label_desc: Use image node_id 20 for calling the RLTask obj's underlying member method code with example usage: obj._random_slice(trajectory, max_slice_length, margin) and returns: fastmath