### Setup Development Environment with uv Source: https://github.com/mert-kurttutan/torchview/blob/main/README.md Installs development and torch dependencies using uv. Recommended for managing Python environments and packages. ```bash uv sync --group dev --group torch uv run prek install ``` -------------------------------- ### Setup Development Environment with pip/venv Source: https://github.com/mert-kurttutan/torchview/blob/main/README.md Installs development tools and dependencies using pip and venv. This is a standard Python virtual environment setup. ```bash python -m venv .venv source .venv/bin/activate pip install -e . # Install dev/test tools as needed (for example: pytest, prek, etc.) pip install pytest prek torch torchvision torchtext prek install ``` -------------------------------- ### Install Torchview from GitHub and Other Packages Source: https://github.com/mert-kurttutan/torchview/blob/main/docs/tutorial/notebook/example_huge_models.ipynb Installs torchview directly from its GitHub repository and also installs graphviz, transformers, and sentencepiece. Use the '-q' flag for quiet installation. ```python ! pip install -q git+https://github.com/mert-kurttutan/torchview.git@merge-output ! pip install -q -U graphviz ! pip install -q transformers ! pip install -q sentencepiece ``` -------------------------------- ### Install Torchview Source: https://github.com/mert-kurttutan/torchview/blob/main/README.md Installs the torchview library using uv or pip. Alternatively, install from the repository for the latest version. ```Bash # uv uv add torchview # pip pip install torchview # or if you want most up-to-date version, install directly from repo # uv uv add git+https://github.com/mert-kurttutan/torchview.git # pip pip install git+https://github.com/mert-kurttutan/torchview.git ``` -------------------------------- ### Serve MkDocs Documentation Locally Source: https://github.com/mert-kurttutan/torchview/blob/main/docs/developer/docs.md Starts a local development server with live reloading for immediate feedback on documentation changes. ```bash mkdocs serve ``` -------------------------------- ### Install torchview and graphviz Source: https://github.com/mert-kurttutan/torchview/blob/main/docs/tutorial/notebook/example_vision.ipynb Installs the necessary libraries for using torchview. Run these commands in your environment. ```bash ! pip install -q torchview ! pip install -q -U graphviz ``` -------------------------------- ### Install and Upgrade Setuptools Source: https://github.com/mert-kurttutan/torchview/blob/main/docs/tutorial/notebook/example_huge_models.ipynb Installs or upgrades the setuptools package. This is a common first step for managing Python packages. ```python ! pip install setuptools --upgrade ``` -------------------------------- ### Install Graphviz Source: https://github.com/mert-kurttutan/torchview/blob/main/README.md Installs the graphviz library using uv or pip. Ensure graphviz is installed on your system for the Python interface to function correctly. ```Bash # uv uv add graphviz # pip pip install graphviz ``` -------------------------------- ### Install Graphviz on Windows Source: https://github.com/mert-kurttutan/torchview/blob/main/README.md Installs the graphviz package using Chocolatey on Windows. ```Bash choco install graphviz ``` -------------------------------- ### Install Graphviz Source: https://github.com/mert-kurttutan/torchview/blob/main/docs/index.md Install the graphviz library and its system dependencies before installing torchview. This is necessary for the Python interface to function correctly. ```Bash pip install graphviz ``` ```Bash apt-get install graphviz ``` ```Bash choco install graphviz ``` ```Bash brew install graphviz ``` -------------------------------- ### Install Graphviz on macOS Source: https://github.com/mert-kurttutan/torchview/blob/main/README.md Installs the graphviz package using Homebrew on macOS. ```Bash brew install graphviz ``` -------------------------------- ### Install Torchview Source: https://github.com/mert-kurttutan/torchview/blob/main/docs/index.md Install torchview using pip, conda, or directly from the repository for the latest version. ```Bash pip install torchview ``` ```Bash conda install -c conda-forge torchview ``` ```Bash pip install git+https://github.com/mert-kurttutan/torchview.git ``` -------------------------------- ### Install Graphviz on Debian/Ubuntu Source: https://github.com/mert-kurttutan/torchview/blob/main/README.md Installs the graphviz system package on Debian-based Linux distributions. ```Bash apt-get install graphviz ``` -------------------------------- ### Install Torchview via Conda Source: https://github.com/mert-kurttutan/torchview/blob/main/README.md Installs the torchview library using conda from the conda-forge channel. ```Bash conda install -c conda-forge torchview ``` -------------------------------- ### Import Torchview and PyTorch Modules Source: https://github.com/mert-kurttutan/torchview/blob/main/docs/tutorial/example_introduction.md Import the draw_graph function from torchview, and necessary modules from torch and graphviz. This setup is required before drawing any model graphs. ```python from torchview import draw_graph from torch import nn import torch import graphviz # when running on VSCode run the below command ``` -------------------------------- ### Define a simple MLP model Source: https://github.com/mert-kurttutan/torchview/blob/main/docs/tutorial/notebook/example_introduction.ipynb Defines a Multi Layer Perceptron (MLP) model with linear layers and ReLU activation. This serves as a basic example for visualization. ```python class MLP(nn.Module): """Multi Layer Perceptron with inplace option. Make sure inplace=true and false has the same visual graph""" def __init__(self, inplace: bool = True) -> None: super().__init__() self.layers = nn.Sequential( nn.Linear(128, 128), nn.ReLU(inplace), nn.Linear(128, 128), ) def forward(self, x: torch.Tensor) -> torch.Tensor: x = self.layers(x) return x ``` -------------------------------- ### Draw DenseNet block graph Source: https://github.com/mert-kurttutan/torchview/blob/main/docs/tutorial/notebook/example_vision.ipynb Visualizes a specific DenseNet block. Note that this example uses a sub-component of DenseNet. ```python model_graph3 = draw_graph(densenet._DenseBlock(3, 3, 1, 2, 0.2), input_size=(1,3,32,32)) ``` -------------------------------- ### Create New MkDocs Project Source: https://github.com/mert-kurttutan/torchview/blob/main/docs/developer/docs.md Use this command to initialize a new documentation project in a specified directory. ```bash mkdocs new [dir-name] ``` -------------------------------- ### Build MkDocs Documentation Site Source: https://github.com/mert-kurttutan/torchview/blob/main/docs/developer/docs.md Generates the static files for the documentation site, ready for deployment. ```bash mkdocs build ``` -------------------------------- ### Display MkDocs Help Message Source: https://github.com/mert-kurttutan/torchview/blob/main/docs/developer/docs.md Prints a help message detailing available commands and options for MkDocs. ```bash mkdocs -h ``` -------------------------------- ### Instantiate T5 Model Configuration Source: https://github.com/mert-kurttutan/torchview/blob/main/docs/tutorial/notebook/example_huge_models.ipynb Creates a T5Config object from the previously loaded dictionary. ```python from transformers import T5Config t5_3b_config = T5Config(**t5_3b_dict) ``` -------------------------------- ### Import necessary libraries Source: https://github.com/mert-kurttutan/torchview/blob/main/docs/tutorial/notebook/example_vision.ipynb Imports torchview, torchvision models, and graphviz. Sets the graphviz output format to PNG for VSCode compatibility. ```python from torchview import draw_graph from torchvision.models import resnet18, GoogLeNet, densenet, vit_b_16 import graphviz # when running on VSCode run the below command # svg format on vscode does not give desired result graphviz.set_jupyter_format('png') ``` -------------------------------- ### Import necessary libraries and configure Graphviz Source: https://github.com/mert-kurttutan/torchview/blob/main/docs/tutorial/notebook/example_introduction.ipynb Imports torchview, PyTorch modules, and configures graphviz for PNG output in Jupyter environments. ```python from torchview import draw_graph from torch import nn import torch import graphviz # when running on VSCode run the below command # svg format on vscode does not give desired result graphviz.set_jupyter_format('png') ``` -------------------------------- ### Load and Prepare BERT-like Model Input Source: https://github.com/mert-kurttutan/torchview/blob/main/docs/tutorial/notebook/example_text.ipynb Loads a pre-trained RoBERTa encoder from torchtext and prepares a sample input batch for visualization. This involves tokenization and tensor conversion. ```python xlmr_base = torchtext.models.ROBERTA_BASE_ENCODER model = xlmr_base.get_model(load_weights=False) transform = xlmr_base.transform() input_batch = ["Hello world", "How are you!"] model_input = torchtext.functional.to_tensor( transform(input_batch), padding_value=1) ``` -------------------------------- ### MkDocs Project Structure Source: https://github.com/mert-kurttutan/torchview/blob/main/docs/developer/docs.md Illustrates the typical file and directory layout for an MkDocs project. ```text mkdocs.yml # The configuration file. docs/ index.md # The documentation homepage. ... # Other markdown pages, images and other files. ``` -------------------------------- ### Load T5 Model Configuration from Hugging Face Source: https://github.com/mert-kurttutan/torchview/blob/main/docs/tutorial/notebook/example_huge_models.ipynb Fetches the configuration JSON for the 't5-3b' model from Hugging Face and parses it. ```python import requests, json t5_3b_dict = json.loads(requests.get('https://huggingface.co/t5-3b/resolve/main/config.json').text) t5_3b_dict ``` -------------------------------- ### Load T5 Tokenizer and Model Source: https://github.com/mert-kurttutan/torchview/blob/main/docs/tutorial/notebook/example_huge_models.ipynb Loads the T5 tokenizer and model using the configuration. It also encodes a sample sentence to prepare input IDs. ```python from transformers import T5Tokenizer, T5Model, T5Config tokenizer = T5Tokenizer.from_pretrained('t5-3b') model = T5Model(t5_3b_config) input_ids = tokenizer.encode("Hello, my dog is cute", return_tensors="pt") # Batch size 1 input_data = {'input_ids':input_ids, 'decoder_input_ids':input_ids} ``` -------------------------------- ### Import Libraries and Configure Graphviz Source: https://github.com/mert-kurttutan/torchview/blob/main/docs/tutorial/notebook/example_text.ipynb Imports required libraries and sets the Graphviz output format for Jupyter environments. This ensures compatibility with VSCode's rendering. ```python from torchview import draw_graph import torchtext from torch import nn import torch import graphviz # when running on VSCode run the below command # svg format on vscode does not give desired result graphviz.set_jupyter_format('png') ``` -------------------------------- ### Draw Graph for BERT-like Model with Expanded Nested Modules Source: https://github.com/mert-kurttutan/torchview/blob/main/docs/tutorial/notebook/example_text.ipynb Visualizes the RoBERTa model graph with nested modules expanded using torchview. This allows for a more detailed view of the internal structure. ```python model_graph4 = draw_graph( model, model_input, graph_name='Roberta', depth=4, expand_nested=True, ) ``` -------------------------------- ### Draw PyTorch Model Graph Source: https://github.com/mert-kurttutan/torchview/blob/main/docs/index.md Visualize a PyTorch model using draw_graph. Specify the model, input size, and device. Using device='meta' prevents memory consumption. ```Python from torchview import draw_graph model = MLP() batch_size = 2 # device='meta' -> no memory is consumed for visualization model_graph = draw_graph(model, input_size=(batch_size, 128), device='meta') model_graph.visual_graph ``` -------------------------------- ### Run Tests with uv Source: https://github.com/mert-kurttutan/torchview/blob/main/README.md Executes tests using pytest via uv. Supports different modes like overwriting and suppressing output. ```bash # uv uv run pytest uv run pytest --overwrite uv run pytest --no-output ``` -------------------------------- ### Draw GoogLeNet graph Source: https://github.com/mert-kurttutan/torchview/blob/main/docs/tutorial/notebook/example_vision.ipynb Visualizes the computation graph for a GoogLeNet model. `init_weights=False` is used here. ```python model_graph3 = draw_graph(GoogLeNet(init_weights=False), input_size=(1,3,32,32)) ``` -------------------------------- ### Draw Graph for BERT-like Model Source: https://github.com/mert-kurttutan/torchview/blob/main/docs/tutorial/notebook/example_text.ipynb Visualizes the RoBERTa model graph with a specified depth using torchview. This provides a high-level overview of the model's architecture. ```python model_graph3 = draw_graph( model, model_input, graph_name='Roberta', depth=4, ) ``` -------------------------------- ### Visualize ResNet with Nested Modules Expanded Source: https://github.com/mert-kurttutan/torchview/blob/main/docs/index.md This snippet visualizes a ResNet model, supporting skip connections and nested modules. Set `expand_nested=True` to visualize nested modules. ```python import torchvision model_graph = draw_graph(resnet18(), input_size=(1,3,32,32), expand_nested=True) model_graph.visual_graph ``` -------------------------------- ### Draw graph for SimpleRNN model with rolling enabled Source: https://github.com/mert-kurttutan/torchview/blob/main/docs/tutorial/notebook/example_introduction.ipynb Generates a visual graph for the SimpleRNN model, enabling the 'roll' feature to represent recursive structures more compactly. ```python model_graph_2 = draw_graph( SimpleRNN(), input_size=(2, 3), graph_name='RecursiveNet', roll=True ) ``` -------------------------------- ### Draw Graph for Unrolled RNN Source: https://github.com/mert-kurttutan/torchview/blob/main/docs/tutorial/notebook/example_text.ipynb Visualizes the SimpleRNN model as an unrolled graph using torchview. This shows each time step of the RNN explicitly. ```python model_graph_2 = draw_graph( SimpleRNN(), input_size=(2, 3), graph_name='RecursiveNet', roll=False ) ``` -------------------------------- ### Draw Vision Transformer (ViT) graph Source: https://github.com/mert-kurttutan/torchview/blob/main/docs/tutorial/notebook/example_vision.ipynb Visualizes the computation graph for a Vision Transformer (ViT) base model with patch size 16. The input size is set for typical image dimensions. ```python model_graph4 = draw_graph(vit_b_16(), input_size=(1, 3, 224, 224)) ``` -------------------------------- ### Draw Graph for Rolled RNN Source: https://github.com/mert-kurttutan/torchview/blob/main/docs/tutorial/notebook/example_text.ipynb Visualizes the SimpleRNN model as a rolled graph using torchview. This is useful for understanding the sequential nature of the RNN. ```python model_graph_1 = draw_graph( SimpleRNN(), input_size=(2, 3), graph_name='RecursiveNet', roll=True ) ``` -------------------------------- ### Generate Graph for T5 Model Source: https://github.com/mert-kurttutan/torchview/blob/main/docs/tutorial/notebook/example_huge_models.ipynb Generates a visual graph of the T5 model using torchview. It specifies the model, input data, graph name, device, depth, and output merging. ```python from torchview import draw_graph model_graph_1 = draw_graph( model, input_data=input_data, graph_name='T5_11B', device='meta', depth=2, merge_outputs=True, ) ``` -------------------------------- ### Define a Simple RNN model Source: https://github.com/mert-kurttutan/torchview/blob/main/docs/tutorial/notebook/example_introduction.ipynb Defines a Simple RNN model using LSTMCell, LeakyReLU, and a Linear projection layer. This model demonstrates recursive module handling. ```python class SimpleRNN(nn.Module): """Simple RNN""" def __init__(self, inplace: bool = True) -> None: super().__init__() self.hid_dim = 2 self.input_dim = 3 self.max_length = 4 self.lstm = nn.LSTMCell(self.input_dim, self.hid_dim) self.activation = nn.LeakyReLU(inplace=inplace) self.projection = nn.Linear(self.hid_dim, self.input_dim) def forward(self, token_embedding: torch.Tensor) -> torch.Tensor: b_size = token_embedding.size()[0] hx = torch.randn(b_size, self.hid_dim, device=token_embedding.device) cx = torch.randn(b_size, self.hid_dim, device=token_embedding.device) for _ in range(self.max_length): hx, cx = self.lstm(token_embedding, (hx, cx)) hx = self.activation(hx) return hx ``` -------------------------------- ### Display Vision Transformer (ViT) graph Source: https://github.com/mert-kurttutan/torchview/blob/main/docs/tutorial/notebook/example_vision.ipynb Renders the computation graph for the Vision Transformer model. ```python model_graph4.visual_graph ``` -------------------------------- ### Draw graph for MLP model Source: https://github.com/mert-kurttutan/torchview/blob/main/docs/tutorial/notebook/example_introduction.ipynb Generates a visual graph representation of the MLP model using torchview. Specifies input size and graph name. ```python model_graph_1 = draw_graph( MLP(), input_size=(2, 128), graph_name='MLP', hide_inner_tensors=False, hide_module_functions=False, ) ``` -------------------------------- ### Display expanded ResNet18 graph Source: https://github.com/mert-kurttutan/torchview/blob/main/docs/tutorial/notebook/example_vision.ipynb Renders the computation graph for ResNet18 with nested modules expanded. ```python model_graph2.visual_graph ``` -------------------------------- ### Display ResNet18 graph Source: https://github.com/mert-kurttutan/torchview/blob/main/docs/tutorial/notebook/example_vision.ipynb Renders the previously generated computation graph for ResNet18. ```python model_graph1.visual_graph ``` -------------------------------- ### Visualize Recursive Networks with Roll Source: https://github.com/mert-kurttutan/torchview/blob/main/docs/index.md Use this snippet to visualize recursive networks. Set `roll=True` to display the rolled version of the network. ```python from torchview import draw_graph model_graph = draw_graph( SimpleRNN(), input_size=(2, 3), graph_name='RecursiveNet', roll=True ) model_graph.visual_graph ``` -------------------------------- ### Display DenseNet block graph Source: https://github.com/mert-kurttutan/torchview/blob/main/docs/tutorial/notebook/example_vision.ipynb Renders the computation graph for the specified DenseNet block. ```python model_graph3.visual_graph ``` -------------------------------- ### Draw ResNet18 graph with expanded nested modules Source: https://github.com/mert-kurttutan/torchview/blob/main/docs/tutorial/notebook/example_vision.ipynb Visualizes the ResNet18 computation graph, expanding any nested modules to show more detail. Use `expand_nested=True` for this. ```python model_graph2 = draw_graph(resnet18(), input_size=(1,3,32,32), expand_nested=True) ``` -------------------------------- ### Run Tests with pip/venv Source: https://github.com/mert-kurttutan/torchview/blob/main/README.md Executes tests using pytest within a pip/venv environment. Allows for different test execution options. ```bash # pip / venv pytest pytest --overwrite pytest --no-output ``` -------------------------------- ### Access the visual graph of the SimpleRNN model Source: https://github.com/mert-kurttutan/torchview/blob/main/docs/tutorial/notebook/example_introduction.ipynb Retrieves the visual graph object for the SimpleRNN model after enabling the rolling mechanism. ```python model_graph_2.visual_graph ``` -------------------------------- ### Draw ResNet18 graph Source: https://github.com/mert-kurttutan/torchview/blob/main/docs/tutorial/notebook/example_vision.ipynb Visualizes the computation graph for a ResNet18 model with a specified input size. This is a basic graph visualization. ```python model_graph1 = draw_graph(resnet18(), input_size=(1,3,32,32)) ``` -------------------------------- ### Resize the graph of the SimpleRNN model Source: https://github.com/mert-kurttutan/torchview/blob/main/docs/tutorial/notebook/example_introduction.ipynb Rescales the visual graph of the SimpleRNN model by a factor of 0.5, making it smaller for easier viewing or integration. ```python model_graph_2.resize_graph(scale=0.5) ``` -------------------------------- ### Draw PyTorch Model Graph Source: https://github.com/mert-kurttutan/torchview/blob/main/README.md Use this function to generate a visual representation of your PyTorch model. It can display module hierarchy, tensor flow, and shapes. Configure output details like graph name, depth, and file saving options. ```python def draw_graph( model: nn.Module, input_data: INPUT_DATA_TYPE | None = None, input_size: INPUT_SIZE_TYPE | None = None, graph_name: str = 'model', depth: int | float = 3, device: torch.device | str | None = None, dtypes: list[torch.dtype] | None = None, mode: str | None = None, strict: bool = True, expand_nested: bool = False, graph_dir: str | None = None, hide_module_functions: bool = True, hide_inner_tensors: bool = True, roll: bool = False, show_shapes: bool = True, save_graph: bool = False, filename: str | None = None, directory: str = '.', **kwargs: Any, ) -> ComputationGraph: '''Returns visual representation of the input Pytorch Module with ComputationGraph object. ComputationGraph object contains: 1) Root nodes (usually tensor node for input tensors) which connect to all the other nodes of computation graph of pytorch module recorded during forward propagation. 2) graphviz.Digraph object that contains visual representation of computation graph of pytorch module. This graph visual shows modules/ module hierarchy, torch_functions, shapes and tensors recorded during forward prop, for examples see documentation, and colab notebooks. Args: model (nn.Module): Pytorch model to represent visually. input_data (data structure containing torch.Tensor): input for forward method of model. Wrap it in a list for multiple args or in a dict or kwargs input_size (Sequence of Sizes): Shape of input data as a List/Tuple/torch.Size (dtypes must match model input, default is FloatTensors). Default: None graph_name (str): Name for graphviz.Digraph object. Also default name graphviz file of Graph Visualization Default: 'model' depth (int): Upper limit for depth of nodes to be shown in visualization. Depth is measured how far is module/tensor inside the module hierarchy. For instance, main module has depth=0, whereas submodule of main module has depth=1, and so on. Default: 3 device (str or torch.device): Device to place and input tensors. Defaults to gpu if cuda is seen by pytorch, otherwise to cpu. Default: None dtypes (list of torch.dtype): Uses dtypes to set the types of input tensor if input size is given. mode (str): Mode of model to use for forward prop. Defaults to Eval mode if not given Default: None strict (bool): if true, graphviz visual does not allow multiple edges between nodes. Mutiple edge occurs e.g. when there are tensors from module node to module node and hiding those tensors Default: True expand_nested(bool): if true shows nested modules with dashed borders graph_dir (str): Sets the direction of visual graph 'TB' -> Top to Bottom 'LR' -> Left to Right 'BT' -> Bottom to Top 'RL' -> Right to Left Default: None -> TB hide_module_function (bool): Determines whether to hide module torch_functions. Some modules consist only of torch_functions (no submodule), e.g. nn.Conv2d. True => Dont include module functions in graphviz False => Include modules function in graphviz Default: True hide_inner_tensors (bool): Inner tensor is all the tensors of computation graph but input and output tensors True => Does not show inner tensors in graphviz False => Shows inner tensors in graphviz Default: True roll (bool): If true, rolls recursive modules. Default: False show_shapes (bool): True => Show shape of tensor, input, and output False => Dont show Default: True save_graph (bool): True => Saves output file of graphviz graph False => Does not save Default: False filename (str): name of the file to store dot syntax representation and image file of graphviz graph. Defaults to graph_name directory (str): directory in which to store graphviz output files. Default: . Returns: ComputationGraph object that contains visualization of the input pytorch model in the form of graphviz Digraph object ''' ``` -------------------------------- ### Draw PyTorch Model Graph Source: https://github.com/mert-kurttutan/torchview/blob/main/docs/index.md Use this function to generate a visual representation of your PyTorch model. It can display module hierarchy, shapes, and tensors. Configure options like depth, graph direction, and what to hide (module functions, inner tensors) for clarity. ```python def draw_graph( model: nn.Module, input_data: INPUT_DATA_TYPE | None = None, input_size: INPUT_SIZE_TYPE | None = None, graph_name: str = 'model', depth: int | float = 3, device: torch.device | str | None = None, dtypes: list[torch.dtype] | None = None, mode: str | None = None, strict: bool = True, expand_nested: bool = False, graph_dir: str | None = None, hide_module_functions: bool = True, hide_inner_tensors: bool = True, roll: bool = False, show_shapes: bool = True, save_graph: bool = False, filename: str | None = None, directory: str = '.', **kwargs: Any, ) -> ComputationGraph: '''Returns visual representation of the input Pytorch Module with ComputationGraph object. ComputationGraph object contains: 1) Root nodes (usually tensor node for input tensors) which connect to all the other nodes of computation graph of pytorch module recorded during forward propagation. 2) graphviz.Digraph object that contains visual representation of computation graph of pytorch module. This graph visual shows modules/ module hierarchy, torch_functions, shapes and tensors recorded during forward prop, for examples see documentation, and colab notebooks. Args: model (nn.Module): Pytorch model to represent visually. input_data (data structure containing torch.Tensor): input for forward method of model. Wrap it in a list for multiple args or in a dict or kwargs input_size (Sequence of Sizes): Shape of input data as a List/Tuple/torch.Size (dtypes must match model input, default is FloatTensors). Default: None graph_name (str): Name for graphviz.Digraph object. Also default name graphviz file of Graph Visualization Default: 'model' depth (int): Upper limit for depth of nodes to be shown in visualization. Depth is measured how far is module/tensor inside the module hierarchy. For instance, main module has depth=0, whereas submodule of main module has depth=1, and so on. Default: 3 device (str or torch.device): Device to place and input tensors. Defaults to gpu if cuda is seen by pytorch, otherwise to cpu. Default: None dtypes (list of torch.dtype): Uses dtypes to set the types of input tensor if input size is given. mode (str): Mode of model to use for forward prop. Defaults to Eval mode if not given Default: None strict (bool): if true, graphviz visual does not allow multiple edges between nodes. Mutiple edge occurs e.g. when there are tensors from module node to module node and hiding those tensors Default: True expand_nested(bool): if true shows nested modules with dashed borders graph_dir (str): Sets the direction of visual graph 'TB' -> Top to Bottom 'LR' -> Left to Right 'BT' -> Bottom to Top 'RL' -> Right to Left Default: None -> TB hide_module_function (bool): Determines whether to hide module torch_functions. Some modules consist only of torch_functions (no submodule), e.g. nn.Conv2d. True => Dont include module functions in graphviz False => Include modules function in graphviz Default: True hide_inner_tensors (bool): Inner tensor is all the tensors of computation graph but input and output tensors True => Does not show inner tensors in graphviz False => Shows inner tensors in graphviz Default: True roll (bool): If true, rolls recursive modules. Default: False show_shapes (bool): True => Show shape of tensor, input, and output False => Dont show Default: True save_graph (bool): True => Saves output file of graphviz graph False => Does not save Default: False filename (str): name of the file to store dot syntax representation and image file of graphviz graph. Defaults to graph_name directory (str): directory in which to store graphviz output files. Default: . Returns: ComputationGraph object that contains visualization of the input pytorch model in the form of graphviz Digraph object ''' ``` -------------------------------- ### draw_graph Source: https://github.com/mert-kurttutan/torchview/blob/main/README.md Generates and returns a visual representation of a PyTorch model's computation graph. This function can optionally save the graph to a file and offers various customization options for display and detail level. ```APIDOC ## draw_graph ### Description Returns a visual representation of the input PyTorch Module with a ComputationGraph object. The ComputationGraph object contains root nodes and a graphviz.Digraph object representing the computation graph. ### Method Signature ```python def draw_graph( model: nn.Module, input_data: INPUT_DATA_TYPE | None = None, input_size: INPUT_SIZE_TYPE | None = None, graph_name: str = 'model', depth: int | float = 3, device: torch.device | str | None = None, dtypes: list[torch.dtype] | None = None, mode: str | None = None, strict: bool = True, expand_nested: bool = False, graph_dir: str | None = None, hide_module_functions: bool = True, hide_inner_tensors: bool = True, roll: bool = False, show_shapes: bool = True, save_graph: bool = False, filename: str | None = None, directory: str = '.', **kwargs: Any, ) -> ComputationGraph: ``` ### Parameters #### `model` (nn.Module) - Required PyTorch model to represent visually. #### `input_data` (data structure containing torch.Tensor | None) - Optional Input for the forward method of the model. Wrap it in a list for multiple args or in a dict or kwargs. #### `input_size` (Sequence of Sizes | None) - Optional Shape of input data as a List/Tuple/torch.Size (dtypes must match model input, default is FloatTensors). #### `graph_name` (str) - Optional Name for the graphviz.Digraph object. Also the default name for the graphviz file of the Graph Visualization. Default: 'model' #### `depth` (int | float) - Optional Upper limit for the depth of nodes to be shown in the visualization. Depth is measured by how far a module/tensor is inside the module hierarchy. Default: 3 #### `device` (torch.device | str | None) - Optional Device to place and input tensors. Defaults to GPU if CUDA is seen by PyTorch, otherwise to CPU. Default: None #### `dtypes` (list[torch.dtype] | None) - Optional Uses dtypes to set the types of input tensor if input size is given. #### `mode` (str | None) - Optional Mode of the model to use for forward prop. Defaults to Eval mode if not given. Default: None #### `strict` (bool) - Optional If true, graphviz visual does not allow multiple edges between nodes. Multiple edges occur e.g. when there are tensors from module node to module node and hiding those tensors. Default: True #### `expand_nested` (bool) - Optional If true, shows nested modules with dashed borders. #### `graph_dir` (str | None) - Optional Sets the direction of the visual graph ('TB', 'LR', 'BT', 'RL'). Default: None -> TB #### `hide_module_functions` (bool) - Optional Determines whether to hide module torch_functions. Some modules consist only of torch_functions (no submodule), e.g. nn.Conv2d. True => Don't include module functions in graphviz False => Include modules function in graphviz Default: True #### `hide_inner_tensors` (bool) - Optional Inner tensor is all the tensors of the computation graph but input and output tensors. True => Does not show inner tensors in graphviz False => Shows inner tensors in graphviz Default: True #### `roll` (bool) - Optional If true, rolls recursive modules. Default: False #### `show_shapes` (bool) - Optional True => Show shape of tensor, input, and output False => Don't show Default: True #### `save_graph` (bool) - Optional True => Saves output file of graphviz graph False => Does not save Default: False #### `filename` (str | None) - Optional Name of the file to store dot syntax representation and image file of graphviz graph. Defaults to `graph_name`. #### `directory` (str) - Optional Directory in which to store graphviz output files. Default: '.' #### `**kwargs` (Any) Additional keyword arguments. ### Returns `ComputationGraph` object that contains visualization of the input pytorch model in the form of a graphviz Digraph object. ``` -------------------------------- ### Set Jupyter Graphviz Format Source: https://github.com/mert-kurttutan/torchview/blob/main/docs/index.md Adjust the graphviz rendering format to PNG for VSCode users experiencing SVG cropping issues. This setting is not typically needed in JupyterLab or Google Colab. ```Python import graphviz graphviz.set_jupyter_format('png') ``` -------------------------------- ### Access the visual graph of the MLP model Source: https://github.com/mert-kurttutan/torchview/blob/main/docs/tutorial/notebook/example_introduction.ipynb Retrieves the visual graph object for the MLP model, which can then be displayed or further manipulated. ```python model_graph_1.visual_graph ``` -------------------------------- ### Show Hidden Tensors and Functionals in MLP Source: https://github.com/mert-kurttutan/torchview/blob/main/docs/index.md This snippet visualizes a Multi-Layer Perceptron (MLP) and shows inner tensors and functionals. Set `hide_inner_tensors=False` and `hide_module_functions=False` to display them. ```python # Show inner tensors and Functionals model_graph = draw_graph( MLP(), input_size=(2, 128), graph_name='MLP', hide_inner_tensors=False, hide_module_functions=False, ) model_graph.visual_graph ``` -------------------------------- ### Handling F.linear and F.embedding in Older PyTorch Versions Source: https://github.com/mert-kurttutan/torchview/blob/main/docs/developer/torch_function_notes.md This code snippet addresses compatibility issues with F.linear and F.embedding in PyTorch versions prior to 1.10. It ensures that these functions return a `RecorderTensor` subclass instead of `NotImplemented` or a base `torch.Tensor`. ```python # This is necessary for torch version < 1.10 if func in [F.linear, F.embedding]: out = nn.parameter.Parameter.__torch_function__( func, types, args, kwargs).as_subclass(RecorderTensor) else: # use original torch_function; otherwise, # it leads to infinite recursive call of torch_function out = super().__torch_function__(func, types, args, kwargs) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.