szagoruyko / pytorchviz

A small package to create visualizations of PyTorch execution graphs
MIT License
3.22k stars 279 forks source link

PNG Generation #73

Open karkir0003 opened 2 years ago

karkir0003 commented 2 years ago

I'm trying to use pytorchviz to visualize a simple neural net trained with pytorch. I used the following code

make_dot(torch.from_numpy(pred), params=dict(model.named_parameters()), show_attrs=True, show_saved=True).render("my_model_viz.png").

I confirmed that I'm able to get pred properly through the training and prediction pipelines. I see my_model_viz.png, but when I try to open it, I'm not able to see the visualization. Can someone please help me resolve this issue

karkir0003 commented 2 years ago

@szagoruyko @willprice

albanD commented 2 years ago

Hi,

The from_numpy() call looks very suspicious here! If pred is a numpy array, it cannot have autograd info associated with it.

karkir0003 commented 2 years ago

so the .from_numpy() bascially converts numpy array into torch.tensor

karkir0003 commented 2 years ago

how can I remedy this issue? @albanD

karkir0003 commented 2 years ago

so when I do predict on the model, am I to return the prediction as a torch tensor?

karkir0003 commented 2 years ago

Also, one other question I have is this:

Suppose that my model architecture is of the form:

import torch
import torch.nn as nn
from torch.autograd import Variable

class DLModel(nn.Module):
    def __init__(self, layer_list):
        """
        Function to initialize Deep Learning model
        given a user specified layer list

        Args:
            layer_list (list): list of nn.Module layers from parser.py
        """
        super().__init__()
        self.model = nn.Sequential(*layer_list)

    def forward(self, x: torch.Tensor):
        pred = self.model(x) #apply model on input x
        return pred

How can I use pytorchviz to visualize the model architecture where we want to see each layer/module in nn.Sequential? @albanD @szagoruyko

albanD commented 2 years ago

so when I do predict on the model, am I to return the prediction as a torch tensor?

PyTorch autograd and training in general will only work with torch Tensors. So yes, you will have to use Tensor to be able to train this model and to visualize it as well.

How can I use pytorchviz to visualize the model architecture where we want to see each layer/module in nn.Sequential?

If you want nn.Module as well, I would recommend torchrecorder: https://torchrecorder.readthedocs.io/en/latest/