RuntimeError: size mismatch, m1: [8 x 11552], m2: [1800 x 128]

alber_mel: <p>Hi Community,</p>
<p>I am having this error below:<br>
ret = torch.addmm(bias, input, weight.t()) RuntimeError: size mismatch, m1: [8 x 11552], m2: [1800 x 128]<br>
I read about similar topics posted however It didn’t come out into a proper conclusion into resolving my error:</p>
<p>This is the input size input x forwarded into the model and processed:<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/d/e/dee88dbbdecd37e77c6e5c356da8fe3e9e4bd7d9.png" alt="image" data-base62-sha1="vNWf1wCiavV9Ve0PpF8v7uLMOtr" width="440" height="267"><br>
I am using alextnet and the raw image size is 224*224<br>
I flatten the input before forwarding using the below code<br>
x = data.view(data.shape[0], -1)</p>
<p>Thank you in advance <img src="https://discuss.pytorch.org/images/emoji/apple/pray.png?v=12" title=":pray:" class="emoji" alt=":pray:" loading="lazy" width="20" height="20"></p>

alber_mel: <p>While I am digging into this error further,  I found out that i am specifying in alexnext the following:<br>
hiddens = [8,8]<br>
flatten = 1800<br>
while actually I am passing 224 * 224 = 11552 which is why it is throwing this error,<br>
I resized the image to 32 * 32 and update the alextnet settings into:<br>
hiddens = [32,32]<br>
flatten = 1152<br>
<strong>and solved this error</strong><br>
However this is affecting my alexnet network if I passed the input as 32 * 32 , and then I should be customising the network which I dont want to at this stage,<br>
I am using this transformation and same image input 224.<br>
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),<br>
same as this blog post <a href="https://pytorch.org/hub/pytorch_vision_alexnet/" class="inline-onebox" rel="noopener nofollow ugc">AlexNet | PyTorch</a><br>
anyway around this to pass the image size without resizing the image ?</p>

ptrblck: <p>I’m not sure which implementation you are using and where you are passing <code>hiddens</code> and <code>flatten</code>, as the <code>torchvision</code> AlexNet implementation doesn’t support these arguments, if I’m not mistaken, and expects the inputs to have the spatial shape <code>224x224</code> as seen here:</p>
<pre><code class="lang-python">model = models.alexnet()
x = torch.randn(1, 3, 224, 224)
out = model(x)
print(out.shape)
&gt; torch.Size([1, 1000])
</code></pre>

alber_mel: <p>You are right ptrblck, I used a simple implementation before and I was testing the same dataset in a notebook and I was able to train within alexnet without any faltten and hiddens layers and with just a minor changes, however in the current project I am working it looks like a modified version of alexnet which is more complex and include a shared and private classes to support continual multitask learning.</p>
<p>This is the code I am working with, I am using two classes shared and private which I was modifying the values to be able to process the forwarded inputs , I appreciate your time and assistance.</p>
<p>class Shared(torch.nn.Module):</p>
<pre><code>def __init__(self,args):
    super(Shared, self).__init__()

    #added a new dataset
    if args.experiment == 'mydataset':
        hiddens = [64, 128, 256, 512, 512, 512]

    else:
        raise NotImplementedError

    self.conv1=torch.nn.Conv2d(self.ncha,hiddens[0],kernel_size=size//8)
    s=utils.compute_conv_output_size(size,size//8)
    s=s//2
    self.conv2=torch.nn.Conv2d(hiddens[0],hiddens[1],kernel_size=size//10)
    s=utils.compute_conv_output_size(s,size//10)
    s=s//2
    self.conv3=torch.nn.Conv2d(hiddens[1],hiddens[2],kernel_size=2)
    s=utils.compute_conv_output_size(s,2)
    s=s//2
    self.maxpool=torch.nn.MaxPool2d(2)
    self.relu=torch.nn.ReLU()

    self.drop1=torch.nn.Dropout(0.2)
    self.drop2=torch.nn.Dropout(0.5)
    self.fc1=torch.nn.Linear(hiddens[2]*s*s,hiddens[3])
    self.fc2=torch.nn.Linear(hiddens[3],hiddens[4])
    self.fc3=torch.nn.Linear(hiddens[4],hiddens[5])
    self.fc4=torch.nn.Linear(hiddens[5], self.latent_dim)


def forward(self, x_s):
    x_s = x_s.view_as(x_s)
    h = self.maxpool(self.drop1(self.relu(self.conv1(x_s))))
    h = self.maxpool(self.drop1(self.relu(self.conv2(h))))
    h = self.maxpool(self.drop2(self.relu(self.conv3(h))))
    h = h.view(x_s.size(0), -1)
    h = self.drop2(self.relu(self.fc1(h)))
    h = self.drop2(self.relu(self.fc2(h)))
    h = self.drop2(self.relu(self.fc3(h)))
    h = self.drop2(self.relu(self.fc4(h)))
    return h
</code></pre>
<p>class Private(torch.nn.Module):<br>
def <strong>init</strong>(self, args):<br>
super(Private, self).<strong>init</strong>()</p>
<pre><code>    if args.experiment == 'mydataset':
        hiddens = [32,32]
        flatten = 1152

    else:
        raise NotImplementedError


    self.task_out = torch.nn.ModuleList()
    for _ in range(self.num_tasks):
        self.conv = torch.nn.Sequential()
        self.conv.add_module('conv1',torch.nn.Conv2d(self.ncha, hiddens[0], kernel_size=self.size // 8))
        self.conv.add_module('relu1', torch.nn.ReLU(inplace=True))
        self.conv.add_module('drop1', torch.nn.Dropout(0.2))
        self.conv.add_module('maxpool1', torch.nn.MaxPool2d(2))
        self.conv.add_module('conv2', torch.nn.Conv2d(hiddens[0], hiddens[1], kernel_size=self.size // 10))
        self.conv.add_module('relu2', torch.nn.ReLU(inplace=True))
        self.conv.add_module('dropout2', torch.nn.Dropout(0.5))
        self.conv.add_module('maxpool2', torch.nn.MaxPool2d(2))
        self.task_out.append(self.conv)
        self.linear = torch.nn.Sequential()

        self.linear.add_module('linear1', torch.nn.Linear(flatten,self.latent_dim))
        self.linear.add_module('relu3', torch.nn.ReLU(inplace=True))
        self.task_out.append(self.linear)
</code></pre>
<p>class Net(torch.nn.Module):</p>
<pre><code>def __init__(self, args):
    super(Net, self).__init__()

    self.hidden1 = args.head_units
    self.hidden2 = args.head_units//2

    self.shared = Shared(args)
    self.private = Private(args)

    self.head = torch.nn.ModuleList()
    for i in range(self.num_tasks):
        self.head.append(
            torch.nn.Sequential(
                torch.nn.Linear(2*self.latent_dim, self.hidden1),
                torch.nn.ReLU(inplace=True),
                torch.nn.Dropout(),
                torch.nn.Linear(self.hidden1, self.hidden2),
                torch.nn.ReLU(inplace=True),
                torch.nn.Linear(self.hidden2, self.taskcla[i][1])
            ))</code></pre>

ptrblck: <p>Based on the error message and that you were using <code>flatten = 1800</code> before, I guess the shape mismatch is created in the <code>self.linear</code> module of the <code>Private</code> module.<br>
Assuming the <code>[8 x 11552]</code> shape represents the activations using the <code>224x224</code> inputs, set <code>flatten</code> to <code>11552</code> and rerun your script.</p>

alber_mel: <p>Hi ptrblck, well it is working <img src="https://discuss.pytorch.org/images/emoji/apple/partying_face.png?v=9" title=":partying_face:" class="emoji" alt=":partying_face:">  thank you for your suggestions and reply,<br>
like usual solving one issue and start working on another error  <img src="https://discuss.pytorch.org/images/emoji/apple/crazy_face.png?v=9" title=":crazy_face:" class="emoji" alt=":crazy_face:"><br>
I am getting one target out of bounds after running the script,<br>
line 2218, in nll_loss<br>
ret = torch._C._nn.nll_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index)<br>
IndexError: Target 16 is out of bounds.<br>
still I didnt debug it yet properly but I thought to update you that the size mismatch issue sorted out Thank you heaps.</p>

ptrblck: <p>Good to hear you’ve solved the first issue! <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>
<p>For the second one: check the shape of the model output and make sure it contains logits for at least 17 classes, since the target tries to index it with a value of 16 (<code>[0, 16]</code> make 17 valid classes).</p>

alber_mel: <p>Hi Ptrblck,</p>
<p>Sorry for the late reply, I was debugging the code and digging into the issue further. I tested couple of things, however it still not resolved.</p>
<p>1- I have the shape of the numberofclasses= 25<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/3/d/3df69cf5b6a80b7a213f81cc7b3821aa89551cc5.png" alt="image" data-base62-sha1="8Q9uuMD4PjN2PrJtMyqciJknEfr" width="310" height="75"><br>
2- The Classes of the dataset are from 0 to 24 numeric and also strings are 25 the labels of the dataset<br>
In the code this is how I am getting the value of y:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/6/4/64bb18e993a22c6f7f38e0f7ff791dfc8869b112.png" data-download-href="https://discuss.pytorch.org/uploads/default/64bb18e993a22c6f7f38e0f7ff791dfc8869b112" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/6/4/64bb18e993a22c6f7f38e0f7ff791dfc8869b112.png" alt="image" data-base62-sha1="en6BgYNFqZSmy6GPULtpqBniEBY" width="690" height="219" data-dominant-color="303234"><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">826×263 23 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><br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/d/c/dccc40f0af5d7158029377b4e81bb2d9437addad.png" alt="image" data-base62-sha1="vvgEBIE5xbZSgw1sUk3uO5eEuvP" width="415" height="39"></p>
<p>3- while debugging this is where it is throwing the error on task_loss:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/d/1/d147e9eb42888954c27fc0f900fd2fffd78edc96.png" data-download-href="https://discuss.pytorch.org/uploads/default/d147e9eb42888954c27fc0f900fd2fffd78edc96" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/d/1/d147e9eb42888954c27fc0f900fd2fffd78edc96_2_690x29.png" alt="image" data-base62-sha1="tRnRGbKDThBaAIgx6c8D1Pfa3r0" width="690" height="29" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/d/1/d147e9eb42888954c27fc0f900fd2fffd78edc96_2_690x29.png, https://discuss.pytorch.org/uploads/default/optimized/3X/d/1/d147e9eb42888954c27fc0f900fd2fffd78edc96_2_1035x43.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/d/1/d147e9eb42888954c27fc0f900fd2fffd78edc96.png 2x" data-dominant-color="313841"><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">1270×55 8.13 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><br>
y:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/5/7/57665f7db0180ef955538fe0a8f22ad6df532fd3.png" data-download-href="https://discuss.pytorch.org/uploads/default/57665f7db0180ef955538fe0a8f22ad6df532fd3" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/5/7/57665f7db0180ef955538fe0a8f22ad6df532fd3.png" alt="image" data-base62-sha1="ctaS8aWWvUW41daoeCfszLmuXeP" width="690" height="27" data-dominant-color="363735"><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">816×32 6.63 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><br>
I can see the index 16 is already there <img src="https://discuss.pytorch.org/images/emoji/apple/frowning.png?v=12" title=":frowning:" class="emoji" alt=":frowning:" loading="lazy" width="20" height="20"></p>
<p><img src="https://discuss.pytorch.org/uploads/default/original/3X/4/1/41f0758021811f475422b58cf9276556097fd6ab.png" alt="image" data-base62-sha1="9pkdqxr23bgFdUYxKrFy7mrb1hN" width="437" height="126"></p>
<p>Thank you in advance for your time and guidance.</p>

ptrblck: <p>Since you are working with 25 different classes, your model output should also contain logits for these 25 classes and thus should have the shape <code>[batch_size, 25]</code>. Could you check the model output shape before you pass it to the criterion?</p>

alber_mel: <p>Hi Ptrblck,<br>
Thank you for your reply<br>
I updated the ntasks:1  <span class="hashtag-raw">#number</span> of tasks<br>
which gave me the below:<br>
print(output.shape)<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/9/6/96486bac0e86ead5cf57e4126f984be5dc31389c.png" alt="image" data-base62-sha1="lrsMq48l8Tz5PvNlpSsQuMZXfAM" width="326" height="84"></p>
<p>Now after I did that I can see that its start forwarding, also I was mistaken by the number of classes because after that it crashed on the 25 class and was complaining outside the boundary.</p>
<p>However once i updated the num_classes: 26 the output shape is (8, 26) and I was able to pass all the tensors and awaiting to display the epoch counting <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"> finger crossed<br>
it looks promising, it took time for training however it started to work at least even not good accuracy etc …<br>
output shape torch.Size([8, 26])<br>
I still want to figure out why once I reduce the number of tasks it affected the shape of the output model?<br>
Thank you ptrblck without your assistance I couldn’t get this far cheers.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/e/4/e4e83a1b90449f2ae7952c85fbc311f16679a26f.png" data-download-href="https://discuss.pytorch.org/uploads/default/e4e83a1b90449f2ae7952c85fbc311f16679a26f" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/e/4/e4e83a1b90449f2ae7952c85fbc311f16679a26f.png" alt="image" data-base62-sha1="wF0pfzZk11hLX5EdwgH1kOV1o9p" width="690" height="265" data-dominant-color="2F2F2F"><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">829×319 15.5 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>

alber_mel: <p>Hi all,</p>
<p>I had the time to go ahead and continue my customisation for this project, once I set the task in the conf into ((ntasks: 1) the program completes successfully the training and validation. However, the learning rate is still very low as showing above,<br>
If I increase the learning rate (ntasks: 5) I am getting the below error on training task1:</p>
<blockquote>
<p>in nll_loss<br>
ret = torch._C._nn.nll_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index)<br>
IndexError: Target 17 is out of bounds.</p>
</blockquote>
<p>Thank you all in advance.</p>

ptrblck: <p>The error is raised in <code>nn.CrossEntropyLoss</code> or <code>nn.NLLLoss</code>, if you pass a target tensor containing an invalid index. The offending target has a value of <code>17</code>, which would indicate that your are working on a multi-class classification use case of at least <code>18</code> classes. If that’s correct, your model output should have the shape <code>[batch_size, nb_classes=18]</code> for a multi-class classification while the target would have the shape <code>[batch_size]</code> containing class indices in the range <code>[0, 17]</code>.<br>
Since this error is raised, your model output seems to have a wrong shape and I don’t know how the learning rate could chage this.</p>

alber_mel: <p>Hi Piotr,</p>
<p>Thank you for your reply.</p>
<p>That’s true, I am working on a multitask case that has<br>
<code>[batch_size, nb_classes=26]</code></p>
<p>my model output is :<br>
output shape torch.Size([8, 26])</p>
<p>I defined my classes range as:</p>
<blockquote>
<p>p.random.seed(self.seed)<br>
self.task_ids = [[0,1], [2,3], [4,5], [6,7], [8,9], [10,11], [12,13], [14,15], [16,17], [18,19], [20,21], [22,23], [24,25]]<br>
self.train_set = {}<br>
self.test_set = {}</p>
</blockquote>
<p>This is my target model:<br>
<code>targets[index] 1</code>  till <code>targets[index] 26</code></p>
<p>how to proceed and check my model output shape if it is right or wrong,<br>
I am still not sure why once I increase the number of tasks (not learning rate) it is failing, it might be just having a chance randomly to select more targets while on<code> ntasks = 1</code> it is using fewer targets in an index less than 17 ?</p>

ptrblck: <p>The provided output shape of <code>[8, 26]</code> doesn’t match the raised error, since the class index <code>17</code> is already invalid.</p>
<aside class="quote no-group" data-username="alber_mel" data-post="13" data-topic="109920">
<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/alber_mel/48/33595_2.png" class="avatar"> alber_mel:</div>
<blockquote>
<p>This is my target model:<br>
<code>targets[index] 1</code> till <code>targets[index] 26</code></p>
</blockquote>
</aside>
<p>Also this would indicate that you are working with <code>27</code> classes, since the target contains values in the range <code>[0, nb_classes-1]</code>, so even if your model output is <code>[8, 26]</code> you will run into a new error pointing towards index <code>26</code> being out of range.</p>