AttributeError: 'NoneType' object has no attribute 'shape' even though the tensor is not None
I'm working with a resnet-based model to generate some feature embeddings
feature = self.m_resnet(input)
when I print
print('feature:', feature)

I get output like,
tensor([[[-5.2228e-01, -2.6507e-01, -1.4583e+00,  ..., -1.1618e+00,
          -3.9355e-01, -6.7108e-01],
         [-5.0633e-01,  9.0730e-01, -7.6286e-01,  ..., -6.7644e-01,
          -6.4372e-01,  4.2130e-02],
         [ 1.3522e+00,  1.1739e+00,  1.1027e+00,  ...,  1.0143e+00,
           1.0382e+00,  5.5187e-01],
         ...,
         [ 4.6489e-01, -1.2791e-01,  1.1394e+00,  ..., -2.3228e-01,
          -4.3149e-01,  3.1564e-01],
         [ 1.0425e+00,  9.7971e-01, -3.5113e-01,  ...,  4.3813e-01,
           3.7757e-01,  3.0367e-01],
         [-9.2531e-01, -3.5561e-01, -1.9557e-01,  ...,  1.2157e-01,
          -4.4008e-01, -9.3977e-02]],

        [[-5.7037e-01, -2.3364e-01, -1.3321e+00,  ..., -1.2070e+00,
          -4.7131e-01, -5.4751e-01],
         [-2.8480e-01,  8.5945e-01, -5.6804e-01,  ..., -8.7505e-01,
          -5.9196e-01, -4.7775e-02],
         [ 1.4179e+00,  1.3121e+00,  1.1915e+00,  ...,  9.6185e-01,
           9.4094e-01,  6.2634e-01],
         ...,
         [ 4.7378e-01, -2.0151e-01,  1.0540e+00,  ..., -2.1641e-01,
          -4.2161e-01,  2.7364e-01],
         [ 1.0599e+00,  8.7958e-01, -1.3885e-01,  ...,  3.7642e-01,
           3.1348e-01,  2.2855e-01],
         [-8.3528e-01, -3.6043e-01, -4.1944e-02,  ...,  7.9550e-02,
          -3.3973e-01, -9.5777e-02]],

        [[-4.6509e-01, -3.1390e-01, -1.3608e+00,  ..., -1.1940e+00,
          -4.0954e-01, -6.8436e-01],
         [-3.8707e-01,  9.0035e-01, -8.3244e-01,  ..., -7.7861e-01,
          -6.3493e-01,  1.8479e-02],
         [ 1.3674e+00,  1.2385e+00,  1.1890e+00,  ...,  9.8861e-01,
           1.1610e+00,  6.2035e-01],
         ...,
         [ 5.5125e-01, -1.4365e-01,  1.2544e+00,  ..., -1.9578e-01,
          -5.9983e-01,  3.9633e-01],
         [ 1.0217e+00,  1.0091e+00, -3.5424e-01,  ...,  3.7400e-01,
           4.3592e-01,  2.3972e-01],
         [-8.4321e-01, -3.9549e-01, -1.7280e-01,  ...,  1.3756e-01,
          -5.2992e-01,  1.3601e-03]],

        [[-4.4482e-01, -3.2837e-01, -1.4795e+00,  ..., -1.2002e+00,
          -4.2828e-01, -7.6532e-01],
         [-4.5860e-01,  9.2647e-01, -8.4050e-01,  ..., -7.8178e-01,
          -6.4275e-01,  6.4469e-02],
         [ 1.3156e+00,  1.2003e+00,  1.2451e+00,  ...,  1.1086e+00,
           1.2272e+00,  7.0043e-01],
         ...,
         [ 5.1705e-01, -1.1838e-01,  1.1950e+00,  ..., -2.6755e-01,
          -6.0813e-01,  4.4875e-01],
         [ 1.0428e+00,  1.0369e+00, -4.6282e-01,  ...,  4.1102e-01,
           4.8060e-01,  2.7509e-01],
         [-8.9088e-01, -3.8145e-01, -3.3285e-01,  ..., -1.1576e-03,
          -5.0087e-01,  3.1878e-02]]], device='cuda:0',

but when I print the shape,
print('shape:', feature.shape)

I get error,AttributeError: 'NoneType' object has no attribute 'shape' on this print line.
According to above lines, feature is not None, then why I'm getting this error?

I don't see any other explanation than there is some code which runs between your first print command and your second and sets feature=None. Case in point:
&gt;&gt;&gt; feature = &quot;hello&quot;
&gt;&gt;&gt; print(feature)
hello
&gt;&gt;&gt; feature = None
&gt;&gt;&gt; print(feature.shape)
Traceback (most recent call last):
  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
AttributeError: 'NoneType' object has no attribute 'shape'

If feature was still a tensor (or str or whatever), the error message should reflect that, like so:
&gt;&gt;&gt; feature = &quot;hello&quot;
&gt;&gt;&gt; print(feature)
hello
&gt;&gt;&gt; print(feature.shape)
Traceback (most recent call last):
  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
AttributeError: 'str' object has no attribute 'shape'

If somehow feature was not in the scope and hadn't been defined at all, this should be the error message:
&gt;&gt;&gt; print(feature_undefined.shape)
Traceback (most recent call last):
  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
NameError: name 'feature_undefined' is not defined

