Skip to content

Utilities

Useful Python utilities.

clean_ipython_history()

Clean IPython history. This is very useful when we have output cells with large tensors. Credit: code in this function mainly copied from IPython source.

clean_memory()

Clean memory occupied by traceback objects, IPython history, and empty GPU cache.

clean_traceback()

Clean memory used by traceback objects. It comes in handy when traceback has big tensors attached to a traceback while the operation raised Exception. This will lead to the tensor keeps occupying GPU memory and get OOM error even if we try to clean up the GPU memory.

listify(obj)

Change type of any object into a list.

Parameters:

Name Type Description Default
obj Any

Object to turn into a list.

required

Returns:

Type Description
list

Returns list of the provided obj.

set_printoptions(precision=2, linewidth=125, sci_mode=False)

Set print options for numpy and pytorch.

Parameters:

Name Type Description Default
precision int

Number of digits of precision for floating point output.

2
linewidth int

Number of characters per line before inserting line breaks.

125
sci_mode bool

Whether to enable scientific notation.

False

set_seed(seed=42, deterministic=False)

Set seeds for generating random numbers for pytorch, numpy, and random packages.

Parameters:

Name Type Description Default
seed int

Desired seed.

42
deterministic bool

Whether pytorch uses deterministic algorithms.

False

setify(obj)

Change type of any object into a set.

Parameters:

Name Type Description Default
obj Any

Object to turn into a set.

required

Returns:

Type Description
set

Returns set of the provided obj.

tuplify(obj)

Change type of any object into a tuple.

Parameters:

Name Type Description Default
obj Any

Object to turn into a tuple.

required

Returns:

Type Description
tuple

Returns tuple of the provided obj.

uniqueify(x, sort=False)

Returns a list of unique elements in any iterable, optionally sorted.

Parameters:

Name Type Description Default
x Iterable

Iterable to get unique elements from.

required
sort bool

Whether to sort the unique elements in the list.

False

Returns:

Type Description
list

List containing the unique elements, optionally sorted.