General useful utility functions.

colorize[source]

colorize(string:str, color:int, bold:Optional[bool]=False, highlight:Optional[bool]=False)

Colorize a string. This function was originally written by John Schulman.

calc_logstd_anneal[source]

calc_logstd_anneal(n_anneal_cycles:int, anneal_start:float, anneal_end:float, epochs:int)

Calculate log standard deviation annealing schedule. Can be used in PG algorithms on continuous action spaces.

Args:

  • n_anneal_cycles (int): How many times to cycle from anneal_start to anneal_end over the training epochs.
  • anneal_start (float): Starting log standard deviation value.
  • anneal_end (float): Ending log standard deviation value.
  • epochs (int): Number of training cycles.

Returns:

  • logstds (NumPy array): Log standard deviations for each training epoch.

save_frames_as_gif[source]

save_frames_as_gif(frames, filename=None)

Save a list of frames as a gif

This code from this floydhub blog post: https://blog.floydhub.com/spinning-up-with-deep-reinforcement-learning/

conv2d_output_size[source]

conv2d_output_size(kernel_size, stride, sidesize)

Calculate output size of Conv2D layer given kernel_size, stride, and size of one side of input image. Assumes square input image.

num2tuple[source]

num2tuple(num)

Convert an input number to a tuple of (num, num).

conv2d_output_shape[source]

conv2d_output_shape(h_w:tuple, kernel_size:Optional[int]=1, stride:Optional[int]=1, pad:Optional[int]=0, dilation:Optional[int]=1)

Calculate output shape of a Conv2D layer.

Args:

  • h_w (tuple): tuple of input height and width
  • kernel_size (int): size of kernel
  • stride (int): kernel stride
  • pad (int): padding
  • dilation (int): dilation applied

Returns:

  • h (int): height of output
  • w (int): width of output

convtransp2d_output_shape[source]

convtransp2d_output_shape(h_w:tuple, kernel_size:Optional[int]=1, stride:Optional[int]=1, pad:Optional[int]=0, dilation:Optional[int]=1, out_pad:Optional[int]=0)

Calculate output shape of a ConvTranspose2D layer.

Args:

  • h_w (tuple): tuple of input height and width
  • kernel_size (int): size of kernel
  • stride (int): kernel stride
  • pad (int): padding
  • dilation (int): dilation applied
  • out_pad (int): output padding

Returns:

  • h (int): height of output
  • w (int): width of output

class Saver[source]

Saver(out_dir:str, keys:Optional[list]=[])

A class to pickle generic Python objects saved over a model's training cycle.

Args:

  • out_dir (str): Directory to save to.
  • keys (list): Keys to initialize the save dictionary with.

printdict[source]

printdict(dictionary, out_file:Optional[str]=<ipykernel.iostream.OutStream object at 0x7ff507e9b150>)

Print the contents of the epoch tracking dict to stdout or to a file.

Args:

  • out_file (sys.stdout or file object): File for output. If writing to a file, opening it for writing should be handled outside this function.