Use pytorch to make the x_train go through the torch model

username: Matheus_Silva_de_Pau
cooked: <p>I created a torch model from a previously trained model and I need the x_train (numpy) to go through the torch model to create a feature, I created a forward for this and turned the numpy into a tensor, but it’s giving this error as the tensor is already It’s in 4D, can anyone help me?</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/c/6/c6fbf57a7d40ed3872ec0684233a482726520207.png" data-download-href="https://discuss.pytorch.org/uploads/default/c6fbf57a7d40ed3872ec0684233a482726520207" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/c/6/c6fbf57a7d40ed3872ec0684233a482726520207.png" alt="image" data-base62-sha1="soimetaLshbV7jQltMm4NgGEVqT" width="690" height="289" data-dominant-color="F6F5F7"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">873×366 17.6 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

username: ptrblck
cooked: <p>The error message claims that a batchnorm layer receives a 3D tensor, so <code>print</code> the shapes of the activations in your <code>forward</code> method as well as from the input and make sure they are 4D where required.</p>

username: Matheus_Silva_de_Pau
cooked: <p>I created a torch model for this, and when I run the feature_x_train with the original torch model (without deleting layers), this code from the image above runs smoothly. When I use the command in the image below to delete layers, this problem occurs.</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/9/5/95f26d9030c0b55810042354e6e2116dfe360bfd.png" data-download-href="https://discuss.pytorch.org/uploads/default/95f26d9030c0b55810042354e6e2116dfe360bfd" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/9/5/95f26d9030c0b55810042354e6e2116dfe360bfd.png" alt="image" data-base62-sha1="louxDX5fy0dIm85z0vbbP9hme17" width="690" height="274" data-dominant-color="F5F5F5"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">883×351 14.3 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>torch model original:</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/c/b/cba9f8ff479bf3a46660f8bcf02903be496fe67a.png" data-download-href="https://discuss.pytorch.org/uploads/default/cba9f8ff479bf3a46660f8bcf02903be496fe67a" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/c/b/cba9f8ff479bf3a46660f8bcf02903be496fe67a.png" alt="image" data-base62-sha1="t3H5ECSqQdHhrabdu0bfYLRe5Vo" width="690" height="429" data-dominant-color="F4F4F4"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">762×474 23.4 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

username: ptrblck
cooked: <p>I guess the original model might be some functional operations in its <code>forward</code> so wrapping the submodules into an <code>nn.Sequential</code> container might not work directly.<br>
Check the model definition, i.e. how the modules are used in the <code>forward</code> and make sure your new <code>torch_model</code> is not missing operations. The cleaner way to add new layers might be to derive a new class from the original one, initialize the new layers in the <code>__init__</code> and use them in the <code>forward</code>.</p>