Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeError: 'list' object has no attribute 'grad_fn' #31

Open
caffelearn opened this issue May 27, 2019 · 5 comments
Open

AttributeError: 'list' object has no attribute 'grad_fn' #31

caffelearn opened this issue May 27, 2019 · 5 comments

Comments

@caffelearn
Copy link

File "C:\Users\admin\AppData\Local\conda\conda\envs\pytorch\lib\site-packages\torchviz\dot.py", line 38, in make_dot
output_nodes = (var.grad_fn,) if not isinstance(var, tuple) else tuple(v.grad_fn for v in var)

AttributeError: 'list' object has no attribute 'grad_fn'

use:
x= torch.randn(1, 3, 800, 800)
y = self.model.cpu()(x)
vis_graph = make_dot(y, params=dict(list(self.model.named_parameters()) ))

@szagoruyko
Copy link
Owner

does your model output a list?

@H-YunHui
Copy link

@caffelearn @szagoruyko
I also encountered this problem, did you solve it?

@Light--
Copy link

Light-- commented Jul 9, 2020

does your model output a list?

i also meet this problem and yes, my model output a list

could you help me? @szagoruyko

class Backbone(Module):
    def __init__(self, num_layers, drop_ratio, mode='ir'):
        super(Backbone, self).__init__()
        assert num_layers in [50, 100, 152], 'num_layers should be 50,100, or 152'
        assert mode in ['ir', 'ir_se'], 'mode should be ir or ir_se'
        blocks = get_blocks(num_layers)
        if mode == 'ir':
            unit_module = bottleneck_IR
        elif mode == 'ir_se':
            unit_module = bottleneck_IR_SE
        self.input_layer = Sequential(Conv2d(3, 64, (3, 3), 1, 1 ,bias=False), 
                                      BatchNorm2d(64), 
                                      PReLU(64))
        self.output_layer = Sequential(BatchNorm2d(512), 
                                       Dropout(drop_ratio),
                                       Flatten(),
                                       Linear(512 * 7 * 7, 512),
                                       BatchNorm1d(512))
        modules = []
        for block in blocks:
            for bottleneck in block:
                modules.append(
                    unit_module(bottleneck.in_channel,
                                bottleneck.depth,
                                bottleneck.stride))
        self.body = Sequential(*modules)

        # for MTL
        self.tower = nn.Sequential(
            nn.Dropout(),
            nn.Linear(512, 32),
            nn.ReLU(),
            nn.Linear(32, 2),
        )

        self.towers = nn.ModuleList([self.tower for _ in range(40)])

        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
            elif isinstance(m, nn.BatchNorm2d):
                nn.init.constant_(m.weight, 1)
                nn.init.constant_(m.bias, 0)

    def forward(self,x):
        x = self.input_layer(x)
        x = self.body(x)
        h_shared = self.output_layer(x)
        # for MTL
        out = [tower(h_shared) for tower in self.towers]
        return out

how do you solve this? @caffelearn @H-YunHui

@ghost
Copy link

ghost commented Jul 22, 2020

@Light-- I was able to resolve this by passing a tuple containing the output list's elements.

For example if your model has 3 outputs which you output as elements of a list called 'y', then the make_dot function would look like this:

vis_graph = make_dot((y[0], y[1], y[2]), params=dict(list(self.model.named_parameters()) ))

@Light--
Copy link

Light-- commented Jul 23, 2020

@chesharma

vis_graph = make_dot((y[0], y[1], y[2]), params=dict(list(self.model.named_parameters()) ))

Genius bro! 👍 👍 👍 How did you notice this problem and figure it out ?

# the output of my model is a list, and its length is 40, i used this and it worked out!
vis_graph = make_dot(tuple((y[i] for i in range(40))),)

thanks @chesharma

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants