{"id":47576,"date":"2026-11-23T22:06:10","date_gmt":"2026-11-23T22:06:10","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=47576"},"modified":"2026-11-23T22:06:10","modified_gmt":"2026-11-23T22:06:10","slug":"pytorch-lab-8-pytorch-install-and-setup-tensorflow-hidden-layer","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/pytorch-lab-8-pytorch-install-and-setup-tensorflow-hidden-layer\/","title":{"rendered":"PyTorch Lab &#8211; 8 &#8211; Pytorch Install And Setup Tensorflow Hidden Layer"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">This lab sets up HiddenLayer, a small Python library that draws neural network graphs and plots training metrics, along with the Graphviz bindings it uses to render diagrams. &quot;TensorFlow Hidden Layer&quot; in the title refers to the <code>hiddenlayer<\/code> package, not to a hidden layer inside a TensorFlow model. Below the notebook you will find a working setup for current Python versions, a test that checks the install, a first PyTorch model diagram, and fixes for the errors you are most likely to hit.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/devops-school\/9b06af9f8a7977ad8b16af7fabcc8db7.js\"><\/script>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">What the notebook above installs and why<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The notebook above runs three <code>pip install<\/code> commands. Each package does a different job, and only two of them matter if you use PyTorch:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Package<\/th><th>Purpose<\/th><th>Needed for PyTorch visualization?<\/th><\/tr><\/thead><tbody><tr><td><code>hiddenlayer<\/code><\/td><td>Builds a graph of a model&#039;s layers and draws training-progress charts in Jupyter<\/td><td>Yes<\/td><\/tr><tr><td><code>graphviz<\/code><\/td><td>Python bindings that pass the graph to the Graphviz <code>dot<\/code> program for rendering<\/td><td>Yes, and the system Graphviz binaries are also required<\/td><\/tr><tr><td><code>tensorflow<\/code><\/td><td>Lets HiddenLayer draw TensorFlow graphs<\/td><td>No, only if you also visualize TensorFlow models<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The captured output shows TensorFlow 1.13.1 on Python 3.7 in an Anaconda environment. Both versions are years past end of life. Current PyTorch and TensorFlow releases do not support Python 3.7, so if you copy that environment exactly, the install will either fail or give you outdated wheels. The notebook also never installs PyTorch itself, even though this is a PyTorch lab.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A supported Python 3 release that current PyTorch wheels are built for. Check the PyTorch install selector for the current list.<\/li>\n\n\n<li>Jupyter Notebook or JupyterLab, because HiddenLayer displays graphs inline as SVG.<\/li>\n\n\n<li>The Graphviz system package, which provides the <code>dot<\/code> executable. <code>pip install graphviz<\/code> does <strong>not<\/strong> install it.<\/li>\n\n\n<li>A virtual environment or conda environment just for this lab, so these packages don&#039;t conflict with other projects.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Corrected setup for current environments<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">First install the Graphviz binaries with your operating system&#039;s package manager. Then create an isolated environment and install the Python packages in it.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\"># 1. System Graphviz (pick one)<\/span>\nsudo apt-get install -y graphviz        <span class=\"hljs-comment\"># Debian\/Ubuntu<\/span>\nbrew install graphviz                   <span class=\"hljs-comment\"># macOS<\/span>\n<span class=\"hljs-comment\"># Windows: install Graphviz and add its bin folder to PATH<\/span>\n\n<span class=\"hljs-comment\"># 2. Isolated Python environment<\/span>\npython3 -m venv ~\/venvs\/pytorch-lab\nsource ~\/venvs\/pytorch-lab\/bin\/activate\n\n<span class=\"hljs-comment\"># 3. Python packages<\/span>\npip install --upgrade pip\npip install torch graphviz hiddenlayer jupyter\n\n<span class=\"hljs-comment\"># Optional: only if you also want to draw TensorFlow graphs<\/span>\n<span class=\"hljs-comment\"># pip install tensorflow<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">If you use conda, <code>conda install -c conda-forge graphviz python-graphviz<\/code> installs both the binaries and the Python bindings into the environment in one step.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you install from inside a notebook cell, use the <code>%pip<\/code> magic instead of <code>!pip<\/code>. <code>%pip<\/code> always installs into the interpreter that runs the current kernel. <code>!pip<\/code> runs whichever <code>pip<\/code> comes first on the shell PATH, which may belong to a different Python.<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">%pip install torch graphviz hiddenlayer<\/code><\/span><\/pre>\n\n\n<p class=\"wp-block-paragraph\"><strong>Note:<\/strong> After you install packages from inside a notebook, restart the kernel before you import them. If you don&#039;t, Python may keep using the old modules that are already loaded in memory.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Verify the installation<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The notebook above ends with an empty cell and never checks that anything works. Run this cell to confirm that each package imports and that the Python bindings can find the <code>dot<\/code> executable:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">import torch\nimport graphviz\nimport hiddenlayer <span class=\"hljs-keyword\">as<\/span> hl\n\n<span class=\"hljs-keyword\">print<\/span>(<span class=\"hljs-string\">\"PyTorch:\"<\/span>, torch.__version__)\n<span class=\"hljs-keyword\">print<\/span>(<span class=\"hljs-string\">\"Graphviz dot:\"<\/span>, graphviz.version())  <span class=\"hljs-comment\"># calls the dot binary<\/span>\n<span class=\"hljs-keyword\">print<\/span>(<span class=\"hljs-string\">\"HiddenLayer imported OK\"<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\"><code>graphviz.version()<\/code> returns a tuple such as <code>(2, 43, 0)<\/code>. If it raises <code>ExecutableNotFound<\/code>, the Python package is installed but the system Graphviz binaries are missing or not on PATH.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Draw your first PyTorch model graph<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">HiddenLayer traces the model with a dummy input tensor and builds a graph of the operations. The dummy input must have the shape the model expects, including the batch dimension.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">import torch\nimport torch.nn <span class=\"hljs-keyword\">as<\/span> nn\nimport hiddenlayer <span class=\"hljs-keyword\">as<\/span> hl\n\nmodel = nn.Sequential(\n    nn.Linear(<span class=\"hljs-number\">784<\/span>, <span class=\"hljs-number\">128<\/span>),\n    nn.ReLU(),\n    nn.Linear(<span class=\"hljs-number\">128<\/span>, <span class=\"hljs-number\">64<\/span>),\n    nn.ReLU(),\n    nn.Linear(<span class=\"hljs-number\">64<\/span>, <span class=\"hljs-number\">10<\/span>),\n)\n\n<span class=\"hljs-comment\"># Trace the model with a dummy batch of one flattened 28x28 image<\/span>\ngraph = hl.build_graph(model, torch.zeros(&#91;<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">784<\/span>]))\n\n<span class=\"hljs-comment\"># Write the diagram to disk (model_graph.png)<\/span>\ngraph.save(<span class=\"hljs-string\">\"model_graph\"<\/span>, format=<span class=\"hljs-string\">\"png\"<\/span>)\n\n<span class=\"hljs-comment\"># As the last expression in a cell, the graph renders inline in Jupyter<\/span>\ngraph<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">You should see a top-to-bottom diagram of Linear and Relu nodes with tensor shapes on the edges. HiddenLayer merges common layer sequences into single blocks to keep large networks readable.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common errors and fixes<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Error<\/th><th>Cause<\/th><th>Fix<\/th><\/tr><\/thead><tbody><tr><td><code>ExecutableNotFound: failed to execute &#039;dot&#039;<\/code><\/td><td>Only the Python <code>graphviz<\/code> package is installed<\/td><td>Install the system Graphviz package and make sure <code>dot -V<\/code> works in a terminal<\/td><\/tr><tr><td><code>ModuleNotFoundError: No module named &#039;hiddenlayer&#039;<\/code> right after <code>!pip install<\/code><\/td><td><code>pip<\/code> installed the package into a different Python than the kernel uses<\/td><td>Use <code>%pip install<\/code>, then restart the kernel<\/td><\/tr><tr><td><code>AttributeError<\/code> about a <code>torch.onnx<\/code> function such as <code>_optimize_trace<\/code><\/td><td>HiddenLayer calls private PyTorch internals that newer PyTorch releases removed<\/td><td>Use an older PyTorch in a dedicated environment, or switch to one of the alternatives below<\/td><\/tr><tr><td>Shape mismatch inside <code>build_graph<\/code><\/td><td>The dummy input shape doesn&#039;t match the model&#039;s first layer<\/td><td>Pass a tensor with the same shape as one real batch, for example <code>torch.zeros([1, 3, 224, 224])<\/code> for an image CNN<\/td><\/tr><tr><td>No TensorFlow wheel available for your Python version<\/td><td>The Python version is too new or too old for current TensorFlow builds<\/td><td>Skip TensorFlow if you only need PyTorch, or use a Python version that TensorFlow supports<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note:<\/strong> HiddenLayer has not been actively maintained for several years. It works well for learning on older PyTorch versions. For long-lived projects, use one of the maintained tools below.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Maintained alternatives for visualizing PyTorch models<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Each of these tools works with current PyTorch and covers the same need as HiddenLayer:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\"># pip install torchinfo torchviz tensorboard<\/span>\nimport torch\nfrom torchinfo import summary\nfrom torchviz import make_dot\nfrom torch.utils.tensorboard import SummaryWriter\n\nx = torch.zeros(&#91;<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">784<\/span>])\n\n<span class=\"hljs-comment\"># 1. Text summary of layers, output shapes and parameter counts<\/span>\nsummary(model, input_size=(<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">784<\/span>))\n\n<span class=\"hljs-comment\"># 2. Autograd graph rendered with Graphviz<\/span>\nmake_dot(model(x), params=dict(model.named_parameters())).render(<span class=\"hljs-string\">\"autograd_graph\"<\/span>, format=<span class=\"hljs-string\">\"png\"<\/span>)\n\n<span class=\"hljs-comment\"># 3. Interactive graph in TensorBoard (view with: tensorboard --logdir runs)<\/span>\nwriter = SummaryWriter(<span class=\"hljs-string\">\"runs\/lab8\"<\/span>)\nwriter.add_graph(model, x)\nwriter.close()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<ul class=\"wp-block-list\">\n<li><strong>torchinfo<\/strong> prints a Keras-style summary. Use it to check shapes and parameter counts quickly.<\/li>\n\n\n<li><strong>torchviz<\/strong> draws the backward (autograd) graph. Like HiddenLayer, it needs the system Graphviz binaries.<\/li>\n\n\n<li><strong>TensorBoard<\/strong> needs only the <code>tensorboard<\/code> package, not TensorFlow. It also tracks loss and accuracy curves, which replaces HiddenLayer&#039;s <code>History<\/code> and <code>Canvas<\/code> training plots.<\/li>\n\n\n<li><strong>Netron<\/strong> opens exported ONNX or saved model files in a desktop or browser viewer.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Frequently asked questions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Do I need TensorFlow to use HiddenLayer with PyTorch?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">No. TensorFlow is only needed if you want HiddenLayer to draw TensorFlow graphs. For PyTorch models, you need <code>torch<\/code>, <code>hiddenlayer<\/code>, <code>graphviz<\/code> and the system Graphviz binaries.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Why does pip install graphviz still give a &#039;dot&#039; not found error?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>graphviz<\/code> pip package contains only Python bindings. It calls the <code>dot<\/code> program, which comes from the Graphviz system package. Install that with apt, brew, conda or the Windows installer, and make sure its bin folder is on PATH.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is the difference between !pip and %pip in Jupyter?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>!pip<\/code> runs a shell command, so it may install into a different Python than the one your kernel uses. <code>%pip<\/code> always targets the kernel&#039;s own environment, which is why it is the recommended way to install packages from a notebook.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Does HiddenLayer work with the latest PyTorch?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Often it does not, because it relies on private <code>torch.onnx<\/code> functions that newer PyTorch releases changed or removed. For current PyTorch, use torchinfo, torchviz or TensorBoard&#039;s <code>add_graph<\/code> instead.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Can TensorBoard show a PyTorch model graph without installing TensorFlow?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. Install the <code>tensorboard<\/code> package, log the graph with <code>torch.utils.tensorboard.SummaryWriter.add_graph<\/code>, and run <code>tensorboard --logdir runs<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Related tutorials<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/category\/artificial-intelligence\/machine-learning\/\">More Machine Learning tutorials<\/a><\/li>\n\n\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/staff-mlops-engineer-role-blueprint-responsibilities-skills-kpis-and-career-path\/\">Staff MLOps Engineer: Role Blueprint, Responsibilities, Skills, KPIs, and Career Path<\/a><\/li>\n\n\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/lead-machine-learning-engineer-role-blueprint-responsibilities-skills-kpis-and-career-path\/\">Lead Machine Learning Engineer: Role Blueprint, Responsibilities, Skills, KPIs, and Career Path<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>This lab sets up HiddenLayer, a small Python library that draws neural network graphs and plots training metrics, along with the Graphviz bindings it uses to render&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[6512],"tags":[8159,7364],"class_list":["post-47576","post","type-post","status-publish","format-standard","hentry","category-machine-learning","tag-neural-networks","tag-pytorch"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/47576","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=47576"}],"version-history":[{"count":2,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/47576\/revisions"}],"predecessor-version":[{"id":80172,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/47576\/revisions\/80172"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=47576"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=47576"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=47576"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}