Render#

Single public entry point for pytorch model visualization.

_graph_view/_flow_view/_lenet_view (in visualtorch.graph/.flow/.lenet_style) render the same extract_architecture-derived structure three different ways; this module consolidates them behind one function, render(model, input_shape, style=…, **kwargs), so style picks the rendering style and every other parameter is style-appropriate keyword arguments. Kwargs are validated by constructing a per-style dataclass from them - a typo’d kwarg raises TypeError immediately, rather than being silently ignored.

The old public names for these (graph_view/flow_view/lenet_view) are deprecated in favor of render() - render() calls the private implementations directly, not the deprecated public wrappers, so it never triggers their deprecation warning itself.

animate() is the equivalent unified entry point for the animated GIF versions of each style. Unlike the static functions, the underlying per-style animate implementations are private (_graph_view_animate/etc.) and always have been - they never shipped in a public release, so animate() was the one clean public entry point from the start, with no deprecation path ever needed for them.

class visualtorch.render.CommonOptions(input_dtype=None, to_file=None, color_map=None, palette='okabe_ito', background_fill='white', padding=10, opacity=255, font=None, font_color='black', level_gap=None)#

Bases: object

Options accepted by every rendering style.

class visualtorch.render.FlowStyleOptions(min_z=10, min_xy=10, max_z=400, max_xy=2000, scale_z=0.1, scale_xy=1, type_ignore=None, outline_width=1, low_dim_orientation='z', draw_volume=True, spacing=10, draw_funnel=True, shade_step=10, legend=False, legend_position='bottom-left', show_dimension=False, show_input=True, connector_fill=None, connector_width=1, one_dim_orientation=None)#

Bases: object

Options specific to style=”flow” - stacked volumetric/2D boxes connected by funnels.

class visualtorch.render.GraphStyleOptions(node_size=50, layer_spacing=250, node_spacing=10, type_ignore=None, outline_width=1, connector_fill='gray', connector_width=1, ellipsize_after=10, show_neurons=True, show_dimension=False, show_input=True, show_arrows=False, legend=False, legend_position='bottom-left')#

Bases: object

Options specific to style=”graph” - a node/edge diagram, one node per neuron or layer.

class visualtorch.render.LenetStyleOptions(min_z=1, min_xy=10, max_xy=2000, scale_z=1, scale_xy=1, type_ignore=None, outline_width=1, low_dim_orientation='z', spacing=10, draw_funnel=True, shade_step=10, max_channels=100, offset_z=10, show_dimension=True, show_input=True, connector_fill=None, connector_width=1, one_dim_orientation=None, legend=False, legend_position='bottom-left')#

Bases: object

Options specific to style=”lenet” - the classic LeNet stacked-plane look.

visualtorch.render.animate(model, input_shape, style='graph', **kwargs)#

Generate an animated GIF revealing a PyTorch model’s architecture one column at a time.

This is the animated counterpart to render(): style picks which of the three styles to animate, every other parameter matches that style’s GraphStyleOptions/FlowStyleOptions/ LenetStyleOptions/CommonOptions fields (see render()’s docstring), plus 3 shared animation-only parameters: frame_duration, final_hold_duration, and loop. Unlike render(), kwargs aren’t validated via a dataclass here - the underlying implementation functions already have explicit (not **kwargs) signatures, so an unrecognized keyword already raises TypeError on its own.

Parameters:
  • model (torch.nn.Module) – A PyTorch model that will be visualized.

  • input_shape (tuple) – The shape of the input tensor, including batch dim. For a model whose forward() takes multiple separate input tensors, pass a tuple of per-tensor shapes instead, one per positional argument in order, e.g. ((1, 3, 224, 224), (1, 10)).

  • style (str, optional) – Which rendering style to use - “graph” (a node/edge diagram), “flow” (stacked volumetric/2D boxes connected by funnels), or “lenet” (the classic LeNet look).

  • **kwargs (Any) – Style-specific options (see GraphStyleOptions/FlowStyleOptions/ LenetStyleOptions/CommonOptions for the full per-style list), plus 3 shared animation-only options: frame_duration (milliseconds each intermediate frame is displayed, default 600), final_hold_duration (milliseconds the final, fully-revealed frame is displayed before the GIF loops, default 1500), and loop (if True, the default, the GIF loops forever; if False, it plays once).

Returns:

A list of frames (one per column, in reveal order) if to_file is None, else None - the GIF is written to to_file instead.

Return type:

list[Image.Image] | None

visualtorch.render.render(model, input_shape, style='graph', **kwargs)#

Generate an architecture visualization for a given PyTorch model.

Parameters:
  • model (torch.nn.Module) – A PyTorch model that will be visualized.

  • input_shape (tuple) – The shape of the input tensor, including batch dim. For a model whose forward() takes multiple separate input tensors, pass a tuple of per-tensor shapes instead, one per positional argument in order, e.g. ((1, 3, 224, 224), (1, 10)).

  • style (str, optional) – Which rendering style to use - “graph” (a node/edge diagram), “flow” (stacked volumetric/2D boxes connected by funnels), or “lenet” (the classic LeNet look).

  • **kwargs (Any) – Style-specific and common options (see GraphStyleOptions/FlowStyleOptions/ LenetStyleOptions/CommonOptions for the full list per style). Forwarded into the relevant dataclass constructor, so an unrecognized keyword raises TypeError rather than being silently ignored.

Returns:

Generated architecture image.

Return type:

Image.Image