Mat1 and mat2 shapes cannot be multiplied!

GHALI_Ahmed: <p>Hi every one,<br>
can I help me please?<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/c/b/cb0ec77f4d0776ce04e5c1b7916dc5759c9cfeff.png" data-download-href="https://discuss.pytorch.org/uploads/default/cb0ec77f4d0776ce04e5c1b7916dc5759c9cfeff" title="photo"><img src="https://discuss.pytorch.org/uploads/default/original/3X/c/b/cb0ec77f4d0776ce04e5c1b7916dc5759c9cfeff.png" alt="photo" data-base62-sha1="sYkADI5jKpgE0wpuAxl8oLggnFl" width="578" height="500" data-dominant-color="3A3B3B"><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">photo</span><span class="informations">946×818 43.1 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>

azhanmohammed: <p>Your first linear layers expects an input feature size of 2704, while your input has a shape of 6656x26. You can change your model’s 3rd layer’s input feature size to 26, and it should work.</p>

GHALI_Ahmed: <p>hi Mohammed,<br>
but why when i use this “model_x” bellow,  it’s work !!!<br>
because the upper “model” is the same with “model_x”<br>
why “model_x”-down work and don’t for “model”-up !!!<br>
than’k alot<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/4/a/4a954b78b0577afb515f9ad0c1d2e5856c741fa8.png" data-download-href="https://discuss.pytorch.org/uploads/default/4a954b78b0577afb515f9ad0c1d2e5856c741fa8" title="photo_2"><img src="https://discuss.pytorch.org/uploads/default/original/3X/4/a/4a954b78b0577afb515f9ad0c1d2e5856c741fa8.png" alt="photo_2" data-base62-sha1="aDNc7nLGRFrfsPKzRTO9AA0ZyNG" width="690" height="490" data-dominant-color="252625"><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">photo_2</span><span class="informations">726×516 24.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>

GHALI_Ahmed: <p>Hi again,<br>
This is the architecture for the two models:<br>
model : not work<br>
model_x : work for the same “x” entry [16,1,64,64]<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/4/4/44935312a1548b6448dfe5694079e20563fe18f8.png" data-download-href="https://discuss.pytorch.org/uploads/default/44935312a1548b6448dfe5694079e20563fe18f8" title="photo_3"><img src="https://discuss.pytorch.org/uploads/default/original/3X/4/4/44935312a1548b6448dfe5694079e20563fe18f8.png" alt="photo_3" data-base62-sha1="9ME6M55zBM2Myt48gmbT38ITfXG" width="690" height="338" data-dominant-color="3E3E3E"><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">photo_3</span><span class="informations">906×444 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>

azhanmohammed: <p>The difference lies between the forward call of the two models, if you look at the forward call of model_x, it has a flatten layer which flattens the convolution output to have a shape (N, 2704) where N is your batch size and 2704 is the output from the convolution layers. The forward call in model does not have the flatten layer, since we only copy the layers from model_x while creating model. So the output in that case won’t be flattened out, and hence you get an issue with sizes of matrices. I hope it makes things more clear.</p>

GHALI_Ahmed: <p>thank you, and that make sense, but:<br>
1: How can copy the layers from model_x to create new model without issues?<br>
2: I want just delete some layer from the model_x.<br>
thank’s again</p>

azhanmohammed: <aside class="quote no-group" data-username="GHALI_Ahmed" data-post="6" data-topic="159289">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ghali_ahmed/48/29965_2.png" class="avatar"> GHALI_Ahmed:</div>
<blockquote>
<p>1: How can copy the layers from model_x to create new model without issues?</p>
</blockquote>
</aside>
<p><a href="https://discuss.pytorch.org/t/how-can-i-replace-the-forward-method-of-a-predefined-torchvision-model-with-my-customized-forward-function/54224/8">This answer</a> here discusses in detail about how to create your own custom forward call using a pre-defined model.</p>
<aside class="quote no-group" data-username="GHALI_Ahmed" data-post="6" data-topic="159289">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ghali_ahmed/48/29965_2.png" class="avatar"> GHALI_Ahmed:</div>
<blockquote>
<p>2: I want just delete some layer from the model_x.</p>
</blockquote>
</aside>
<p><a href="https://discuss.pytorch.org/t/how-to-remove-the-intermediate-layers-of-model/85068/4">This post</a> here discusses about removing intermediate layers from a model.</p>
<p>Hope these are helpful.</p>

GHALI_Ahmed: <p>I want to thank you very much!<br>
via your posts that you’re include in the last reponse, i found this beautiful post:</p><aside class="quote quote-modified" data-post="11" data-topic="54224">
  <div class="title">
    <div class="quote-controls"></div>
    <img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/c/ba9def/48.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/how-can-i-replace-the-forward-method-of-a-predefined-torchvision-model-with-my-customized-forward-function/54224/11">How can I replace the forward method of a predefined torchvision model with my customized forward function?</a> 
  </div>
  <blockquote>
    An alternative to <a class="mention" href="/u/ptrblck">@ptrblck</a> 's solution is this: 
import torchvision.models as models
import torch


def new_forward(self, x):
    x = self.conv1(x)
    x = self.bn1(x)
    x = self.relu(x)
    x = self.maxpool(x)

    x = self.layer1(x)
    x = self.layer2(x)
    x = self.layer3(x)
    x = self.layer4(x)
    return x


# define a resnet instance
resent = models.resnet18()

# add new_forward function to the resnet instance as a class method
bound_method = new_forward.__get__(resent, resent.__clas…
  </blockquote>
</aside>

<p>have a nice day</p>