I am getting an error while adding transformer encoder layer with cnn layers above it, i am passing an input of size 2,50,768 for transformer layer

Poladi_Shanmukh: <p>model.apply(register_hook)<br>
70 # make a forward pass<br>
71 # print(x.shape)<br>
—&gt; 72 model(*x)<br>
74 # remove these hooks<br>
75 for h in hooks:</p>
<p>File C:\ProgramData\Anaconda3\lib\site-packages\torch\nn\modules\module.py:1190, in Module._call_impl(self, *input, **kwargs)<br>
1186 # If we don’t have any hooks, we want to skip the rest of the logic in<br>
1187 # this function, and just call forward.<br>
1188 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks<br>
1189         or _global_forward_hooks or _global_forward_pre_hooks):<br>
 → 1190     return forward_call(*input, **kwargs)<br>
1191 # Do not call functions when jit is used<br>
1192 full_backward_hooks, non_full_backward_hooks = [], []</p>
<p>File C:\ProgramData\Anaconda3\lib\site-packages\torch\nn\modules\transformer.py:280, in TransformerEncoder.forward(self, src, mask, src_key_padding_mask)<br>
277         src_key_padding_mask_for_layers = None<br>
279 for mod in self.layers:<br>
 → 280     output = mod(output, src_mask=mask, src_key_padding_mask=src_key_padding_mask_for_layers)<br>
282 if convert_to_nested:<br>
283     output = output.to_padded_tensor(0.)</p>
<p>File C:\ProgramData\Anaconda3\lib\site-packages\torch\nn\modules\module.py:1208, in Module._call_impl(self, *input, **kwargs)<br>
1205     bw_hook = hooks.BackwardHook(self, full_backward_hooks)<br>
1206     input = bw_hook.setup_input_hook(input)<br>
 → 1208 result = forward_call(<em>input, **kwargs)<br>
1209 if _global_forward_hooks or self._forward_hooks:<br>
1210     for hook in (</em>_global_forward_hooks.values(), *self._forward_hooks.values()):</p>
<p>File C:\ProgramData\Anaconda3\lib\site-packages\torch\nn\modules\transformer.py:538, in TransformerEncoderLayer.forward(self, src, src_mask, src_key_padding_mask)<br>
536     x = x + self._ff_block(self.norm2(x))<br>
537 else:<br>
 → 538     x = self.norm1(x + self._sa_block(x, src_mask, src_key_padding_mask))<br>
539     x = self.norm2(x + self._ff_block(x))<br>
541 return x</p>
<p>File C:\ProgramData\Anaconda3\lib\site-packages\torch\nn\modules\transformer.py:546, in TransformerEncoderLayer._sa_block(self, x, attn_mask, key_padding_mask)<br>
544 def _sa_block(self, x: Tensor,<br>
545               attn_mask: Optional[Tensor], key_padding_mask: Optional[Tensor]) → Tensor:<br>
 → 546     x = self.self_attn(x, x, x,<br>
547                        attn_mask=attn_mask,<br>
548                        key_padding_mask=key_padding_mask,<br>
549                        need_weights=False)[0]<br>
550     return self.dropout1(x)</p>
<p>File C:\ProgramData\Anaconda3\lib\site-packages\torch\nn\modules\module.py:1211, in Module._call_impl(self, <em>input, **kwargs)<br>
1209 if _global_forward_hooks or self._forward_hooks:<br>
1210     for hook in (</em>_global_forward_hooks.values(), *self._forward_hooks.values()):<br>
 → 1211         hook_result = hook(self, input, result)<br>
1212         if hook_result is not None:<br>
1213             result = hook_result</p>
<p>File ~\AppData\Roaming\Python\Python39\site-packages\torchsummary\torchsummary.py:22, in summary..register_hook..hook(module, input, output)<br>
20 summary[m_key][“input_shape”][0] = batch_size<br>
21 if isinstance(output, (list, tuple)):<br>
—&gt; 22     summary[m_key][“output_shape”] = [<br>
23         [-1] + list(o.size())[1:] for o in output<br>
24     ]<br>
25 else:<br>
26     summary[m_key][“output_shape”] = list(output.size())</p>
<p>File ~\AppData\Roaming\Python\Python39\site-packages\torchsummary\torchsummary.py:23, in (.0)<br>
20 summary[m_key][“input_shape”][0] = batch_size<br>
21 if isinstance(output, (list, tuple)):<br>
22     summary[m_key][“output_shape”] = [<br>
—&gt; 23         [-1] + list(o.size())[1:] for o in output<br>
24     ]<br>
25 else:<br>
26     summary[m_key][“output_shape”] = list(output.size())</p>
<p>AttributeError: ‘NoneType’ object has no attribute ‘size’</p>

Michal_Bogacz: <p>Hi,<br>
Can you send the code, that caused the error?</p>