'Conv2d' object has no attribute 'weight' when debugging on Visual Studio 2017

take0212: <p>Dear all.</p>
<p>When I run my code in debug mode on Visual Studio 2017, I get the following error message.</p>
<blockquote>
<p>torch.nn.modules.module.ModuleAttributeError: ‘Conv2d’ object has no attribute ‘weight’</p>
</blockquote>
<p>This error did not occur, when the code was run in release mode on Visual Studio 2017.<br>
This error did not occur, when the code was run at the command prompt.</p>
<p>module.py<br>
Line 778</p>
<blockquote>
<pre><code>    raise ModuleAttributeError("'{}' object has no attribute '{}'".format(
        type(self).__name__, name))
</code></pre>
</blockquote>
<p>My environment is as follows.</p>
<blockquote>
<p>Windows 10.<br>
Visual Studio 2017 Professional.</p>
</blockquote>
<p>nvcc -V</p>
<blockquote>
<p>nvcc: NVIDIA (R) Cuda compiler driver<br>
Copyright (c) 2005-2019 NVIDIA Corporation<br>
Built on Fri_Feb__8_19:08:26_Pacific_Standard_Time_2019<br>
Cuda compilation tools, release 10.1, V10.1.105</p>
</blockquote>
<p>C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\include</p>
<blockquote>
<p><span class="hashtag-raw">#define</span> CUDNN_MAJOR 7<br>
<span class="hashtag-raw">#define</span> CUDNN_MINOR 6<br>
<span class="hashtag-raw">#define</span> CUDNN_PATCHLEVEL 4</p>
</blockquote>
<p>python -V</p>
<blockquote>
<p>Python 3.7.1</p>
</blockquote>
<p>python -c “import torch;print(torch.<strong>version</strong>);”</p>
<blockquote>
<p>1.7.1+cu101</p>
</blockquote>
<p>python -c “import torch;print(torch.cuda.is_available())”</p>
<blockquote>
<p>True</p>
</blockquote>
<p>python -c “import torch;print(torch.cuda.get_device_name());”</p>
<blockquote>
<p>GeForce GTX 1080 Ti</p>
</blockquote>
<p>python -c “import torch;print(torch.cuda.get_device_capability());”</p>
<blockquote>
<p>(6, 1)</p>
</blockquote>
<p>Call history:</p>
<blockquote>
<pre><code>    Module.__getattr__ line 779 Python symbols have been read.
    Module.__getattr__ line 779 Python symbol loaded.
    Module.register_parameter line 315 Python symbol loaded.
    Module.__setattr__ line 796 Python symbols have been read.
   _ConvNd.__init__ line 78 Python symbols have been loaded.
    Conv2d.__init__ line 412 Python symbols have been loaded.
</code></pre>
</blockquote>
<p>watch:</p>
<blockquote>
<pre><code>                      self.__dict__       {'_backward_hooks': OrderedDict([]), '_buffers': OrderedDict([]), '_forward_hooks': OrderedDict([]), '_forward_pre_hooks': OrderedDict([]), '_load_state_dict_pre_hooks': OrderedDict([]), '_modules': OrderedDict([]), '_non_persistent_buffers_set': {}, '_parameters': OrderedDict([]), '_reversed_padding_r...ted_twice': (1, 1, 1, 1), '_state_dict_hooks': OrderedDict([]), 'dilation': (1, 1), 'groups': 1, 'in_channels': 3, 'kernel_size': (3, 3), ...}              dict
                     items()                
                        ['training']           True       bool
                        ['_parameters']  OrderedDict([]) OrderedDict
                        ['_buffers']         OrderedDict([]) OrderedDict
                        ['_non_persistent_buffers_set']   {}           set
                        ['_backward_hooks']       OrderedDict([]) OrderedDict
                        ['_forward_hooks']          OrderedDict([]) OrderedDict
                        ['_forward_pre_hooks']   OrderedDict([]) OrderedDict
                        ['_state_dict_hooks']       OrderedDict([]) OrderedDict
                        ['_load_state_dict_pre_hooks']    OrderedDict([]) OrderedDict
                        ['_modules']       OrderedDict([]) OrderedDict
                        ['in_channels']   3            int
                        ['out_channels'] 64          int
                     ['kernel_size']    (3, 3)     tuple
                     ['stride']              (1, 1)     tuple
                     ['padding']          (1, 1)     tuple
                     ['dilation']           (1, 1)     tuple
                        ['transposed']    False     bool
                     ['output_padding']           (0, 0)     tuple
                        ['groups']            1            int
                        ['padding_mode']            'zeros'    str
</code></pre>
</blockquote>
<p>I confirmed more.</p>
<p>conv.py<br>
Conv2d.<strong>init</strong><br>
Line 412</p>
<blockquote>
<pre><code>    super(Conv2d, self).__init__(
        in_channels, out_channels, kernel_size, stride, padding, dilation,
        False, _pair(0), groups, bias, padding_mode)
</code></pre>
</blockquote>
<p>conv.py<br>
_ConvNd.<strong>init</strong><br>
Line 78</p>
<blockquote>
<pre><code>        self.weight = Parameter(torch.Tensor(
            out_channels, in_channels // groups, *kernel_size))
</code></pre>
</blockquote>
<p>module.py<br>
Module.<strong>setattr</strong><br>
Line 796</p>
<blockquote>
<pre><code>        self.register_parameter(name, value)
</code></pre>
</blockquote>
<p>module.py<br>
Module.register_parameter<br>
Line 315</p>
<blockquote>
<pre><code>    elif hasattr(self, name) and name not in self._parameters:
        raise KeyError("attribute '{}' already exists".format(name))
</code></pre>
</blockquote>
<p>module.py<br>
Module.<strong>getattr</strong><br>
Line 779</p>
<blockquote>
<pre><code>    raise ModuleAttributeError("'{}' object has no attribute '{}'".format(
        type(self).__name__, name))
</code></pre>
</blockquote>
<p>Module.register_parameter method was calling hasattr method.<br>
But <strong>getattr</strong> method was called.<br>
So the error happened.</p>
<p>How can I fix it?</p>

take0212: <p>I found that I can continue debugging the code even when the error occurs.<br>
I think <strong>hasattr</strong> method is catching the error.</p>