zoukankan      html  css  js  c++  java
  • 使用pytorchviz进行模型可视化出现 'NoneType' object has no attribute 'grad_fn'

    问题

    最近学习pytorch, 原来用kreas重现的模型改为用pytorch实现训练,因为这样给模型的操作更加细致, 对模型的掌控更好。

    当我写好一个模型 出现了这个问题

    使用pytorchviz进行模型可视化出现r如下错误

    raceback (most recent call last):
      File "/home/jiwu/Documents/AttRCNN-CNNs/pyt_train.py", line 174, in <module>
        g = make_dot(y)
      File "/home/jiwu/.virtualenvs/jiwu/lib/python3.6/site-packages/torchviz/dot.py", line 37, in make_dot
        output_nodes = (var.grad_fn,) if not isinstance(var, tuple) else tuple(v.grad_fn for v in var)
    AttributeError: 'NoneType' object has no attribute 'grad_fn'
    

    找了很久没发现原因在哪里, 翻pytorchviz的issue, 一直google没找到原因,最后才发现是只是在foward 函数 我没有return x。 所以出现了这个问题。

        def forward(self, x):
            print (x.shape)
            x1 = self.conv1(x)
            x2 = self.conv2(x)
            print (x2.shape)
            x3 = self.conv3(x)
            x4 = self.conv4(x)
            print (x1.shape, x2.shape, x3.shape, x4.shape)
    
            x = torch.cat((x1, x2, x3, x4), dim = 1)
            print (x.shape)
            x = x.view(x.size(0), -1)
            print(x.shape)
            x = self.line1(x)
            x = self.line2(x)
            x = self.line3(x)
            return x
    
  • 相关阅读:
    学习笔记之C++ How to Program(part)
    Makefile相关资料
    [ZZ]指针Guide
    [ZZ]make命令与makefile文件的写法
    SQLite数据库简介及资源下载
    程序员天生码农命?
    Application标签中有三个事件运行顺序
    Flex 项目案例集锦
    flex简介
    Flex连接数据库三种方法
  • 原文地址:https://www.cnblogs.com/qq874455953/p/12458062.html
Copyright © 2011-2022 走看看