Visualization#
This module provides tools to generate useful visualizations.
Installation#
Since this module is optional, we need to install it before using it:
pip install ninetoothed[visualization]
Visualizing a Tensor#
visualize
can be used to visualize a single tensor:
- ninetoothed.visualization.visualize(tensor, color=None, save_path=None)#
Visualize a tensor as a structured grid representation.
- Parameters:
tensor – The tensor to be visualized.
color – The color to be used for visualization.
save_path – The path where the visualization should be saved.
Basic Usage#
If we just want to temporarily visualize a tensor, the simplest way is to pass the tensor to visualize
and provide a save path. Then, visualize
will save the generated image at the specified location.
x = Tensor(shape=(4, 8))
visualize(x, save_path="x.png")
This method can also be used to temporarily visualize multiple tensors. You just need to pass the corresponding tensors and save paths to visualize
.
x = Tensor(shape=(4, 8))
visualize(x, save_path="x.png")
y = Tensor(shape=(8, 4))
visualize(y, save_path="y.png")
z = Tensor(shape=(4, 4))
visualize(z, save_path="z.png")
Specifying Colors#
When using the above method, tensors are assigned default colors. To customize colors, we can use the color
parameter, following Matplotlib’s color formats.
x = Tensor(shape=(4, 8))
visualize(x, color="orange", save_path="x.png")
Visualizing an Arrangement#
visualize_arrangement
can be used to visualize an arrangement:
- ninetoothed.visualization.visualize_arrangement(arrangement, tensors)#
Visualize the arrangement of the tensors.
- Parameters:
arrangement – The arrangement of the tensors.
tensors – The tensors.
Note: Currently, this API must be run in a CUDA-available environment.
This API requires arrangement
and tensors
as parameters, similar to ninetoothed.make()
. However, there is a key difference: neither arrangement
nor tensors
can contain symbols; they can only contain concrete values.
As demonstrated below, we can use functools.partial
to pass concrete arguments to the _arrangement
function, constructing a symbol-free arrangement
. In addition, we can create a tensor with a concrete shape using the shape
parameter of Tensor
.
def _arrangement(tensor, tile_shape):
return (tensor.tile(tile_shape),)
arrangement = functools.partial(_arrangement, tile_shape=(2, 2))
tensors = (Tensor(shape=(5, 5)),)
visualize_arrangement(arrangement, tensors)