

.. _sphx_glr_usage_examples_graph:

============
Graph
============

These examples cover visualization via graph view


.. raw:: html

  <div id='sg-tag-list' class='sphx-glr-tag-list'></div>


.. raw:: html

    <div class="sphx-glr-thumbnails">

.. thumbnail-parent-div-open

.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Visualization of basic dense model. No color_map is passed here, so each layer type is assigned a color automatically from a colorblind-safe default palette (see the Custom Color example to override this).">

.. only:: html

  .. image:: /usage_examples/graph/images/thumb/sphx_glr_plot_basic_dense_thumb.png
    :alt:

  :doc:`/usage_examples/graph/plot_basic_dense`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Basic Dense</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Instead of hand-building a color_map entry for every layer type, pick a named palette - it&#x27;s used as the fallback fill color for any layer type not given an explicit color_map override. A handful of the built-in palettes are shown below; see visualtorch.PALETTES.keys() for the full list.">

.. only:: html

  .. image:: /usage_examples/graph/images/thumb/sphx_glr_plot_color_palettes_graph_thumb.png
    :alt:

  :doc:`/usage_examples/graph/plot_color_palettes_graph`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Color Palettes</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Visualization of custom color. The synthetic input node can be recolored too, keyed by visualtorch.Input in color_map just like any real layer type.">

.. only:: html

  .. image:: /usage_examples/graph/images/thumb/sphx_glr_plot_custom_node_color_thumb.png
    :alt:

  :doc:`/usage_examples/graph/plot_custom_node_color`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Custom Color</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Visualization of custom node size">

.. only:: html

  .. image:: /usage_examples/graph/images/thumb/sphx_glr_plot_custom_node_size_thumb.png
    :alt:

  :doc:`/usage_examples/graph/plot_custom_node_size`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Custom Node Size</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Change the color transparency">

.. only:: html

  .. image:: /usage_examples/graph/images/thumb/sphx_glr_plot_custom_opacity_graph_thumb.png
    :alt:

  :doc:`/usage_examples/graph/plot_custom_opacity_graph`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Custom Opacity</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Visualization of custom spacing between layers">

.. only:: html

  .. image:: /usage_examples/graph/images/thumb/sphx_glr_plot_custom_spacing_layers_thumb.png
    :alt:

  :doc:`/usage_examples/graph/plot_custom_spacing_layers`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Custom Spacing for Layers</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="background_fill isn&#x27;t limited to plain white - it also accepts a transparent color (e.g. (0, 0, 0, 0)), useful for dropping a figure onto a paper/slide without a white box around it, or an opaque dark color for a nicer look on dark-mode pages.">

.. only:: html

  .. image:: /usage_examples/graph/images/thumb/sphx_glr_plot_dark_background_graph_thumb.png
    :alt:

  :doc:`/usage_examples/graph/plot_dark_background_graph`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Dark Background</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="By default, graph_view draws a fully-connected mesh between every pair of adjacent layers&#x27; neuron circles. That&#x27;s accurate for a genuinely dense layer (e.g. Linear), but misleading for a convolutional one - a Conv2d&#x27;s real connectivity is local and shared across spatial positions, not &quot;every input channel wired to every output channel.&quot; Setting show_neurons=False draws each layer as a single box instead, which is the more honest representation for a conv-heavy model.">

.. only:: html

  .. image:: /usage_examples/graph/images/thumb/sphx_glr_plot_residual_block_thumb.png
    :alt:

  :doc:`/usage_examples/graph/plot_residual_block`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Hiding Individual Neurons</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="By default, graph_view&#x27;s node/box size is fairly small - fine for a quick look, but often too small once you want a crisp, presentation- or paper-quality image. The main lever is node_size (in pixels, applies whether you&#x27;re using show_neurons=True circles or show_neurons=False boxes), paired with layer_spacing/``node_spacing`` so a bigger node size still has enough room around it. See also the Custom Node Size example for a more focused look at node_size alone.">

.. only:: html

  .. image:: /usage_examples/graph/images/thumb/sphx_glr_plot_higher_resolution_thumb.png
    :alt:

  :doc:`/usage_examples/graph/plot_higher_resolution`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Higher Resolution</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Visualization of an Inception-style block: four parallel branches (a plain Conv2d+BatchNorm2d, a 1x1-then-3x3 conv, a 1x1-then-5x5 conv, and a max-pool-then-1x1-conv) that all read the same input and merge into a shared projection layer.">

.. only:: html

  .. image:: /usage_examples/graph/images/thumb/sphx_glr_plot_inception_block_thumb.png
    :alt:

  :doc:`/usage_examples/graph/plot_inception_block`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Multi-Branch Merge (Inception-style)</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="Visualization of a model whose forward() takes two separate input tensors instead of one - an image branch (Conv2d + global pooling) and a tabular-vector branch (a small MLP) - merged by concatenation before a shared head. This is the pattern used by siamese networks and multi-modal architectures.">

.. only:: html

  .. image:: /usage_examples/graph/images/thumb/sphx_glr_plot_multi_input_thumb.png
    :alt:

  :doc:`/usage_examples/graph/plot_multi_input`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Multi-Input Model (Siamese-style)</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="A leaf layer&#x27;s forward() doesn&#x27;t always return a single tensor - nn.LSTM returns (output, (h_n, c_n)): the full sequence of hidden states, plus the final hidden and cell states. With show_dimension=True, every one of those output tensors&#x27; shapes is printed, not just the first, so a downstream layer that consumes h_n instead of output (as this model does) doesn&#x27;t leave its actual input shape unaccounted for.">

.. only:: html

  .. image:: /usage_examples/graph/images/thumb/sphx_glr_plot_multi_output_shapes_thumb.png
    :alt:

  :doc:`/usage_examples/graph/plot_multi_output_shapes`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Multi-Output Layer Shapes</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="A real, torchvision-provided architecture (not a toy model) - resnet18 has 8 residual blocks across 4 stages, with a projection shortcut (an extra Conv2d+BatchNorm2d in the skip path) at the first block of each of the last 3 stages, where the spatial size is downsampled and the channel count changes. Each residual skip connection is correctly routed above the diagram.">

.. only:: html

  .. image:: /usage_examples/graph/images/thumb/sphx_glr_plot_resnet18_thumb.png
    :alt:

  :doc:`/usage_examples/graph/plot_resnet18`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">ResNet-18</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="A small U-Net: two downsampling encoder stages, a bottleneck, and two upsampling decoder stages, with a skip connection concatenating each encoder stage&#x27;s output into its corresponding decoder stage. Both skip connections are genuine bypasses (around the pooling/bottleneck path) and are correctly routed above the diagram, nested since the two spans overlap.">

.. only:: html

  .. image:: /usage_examples/graph/images/thumb/sphx_glr_plot_unet_thumb.png
    :alt:

  :doc:`/usage_examples/graph/plot_unet`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">U-Net</div>
    </div>


.. raw:: html

    <div class="sphx-glr-thumbcontainer" tooltip="A small Vision Transformer: a Conv2d patch-embedding (splitting the image into non-overlapping patches and projecting each to a vector), a learned positional embedding added on, and a Transformer encoder over the resulting patch sequence.">

.. only:: html

  .. image:: /usage_examples/graph/images/thumb/sphx_glr_plot_vit_thumb.png
    :alt:

  :doc:`/usage_examples/graph/plot_vit`

.. raw:: html

      <div class="sphx-glr-thumbnail-title">Vision Transformer (ViT)</div>
    </div>


.. thumbnail-parent-div-close

.. raw:: html

    </div>


.. toctree::
   :hidden:

   /usage_examples/graph/plot_basic_dense
   /usage_examples/graph/plot_color_palettes_graph
   /usage_examples/graph/plot_custom_node_color
   /usage_examples/graph/plot_custom_node_size
   /usage_examples/graph/plot_custom_opacity_graph
   /usage_examples/graph/plot_custom_spacing_layers
   /usage_examples/graph/plot_dark_background_graph
   /usage_examples/graph/plot_residual_block
   /usage_examples/graph/plot_higher_resolution
   /usage_examples/graph/plot_inception_block
   /usage_examples/graph/plot_multi_input
   /usage_examples/graph/plot_multi_output_shapes
   /usage_examples/graph/plot_resnet18
   /usage_examples/graph/plot_unet
   /usage_examples/graph/plot_vit

