Is there any way to get nn.TransformerDecoder tutorial?

Teddy_Salas: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> respected si,r i am looking for a brief and detailed tutorial for nn.transformerdecder? can you help me find it?</p>

ptrblck: <p>Please don’t tag users as it could discourage others to post a valid answer and don’t double post the same question. <a href="https://pytorch.org/tutorials/beginner/transformer_tutorial.html">This tutorial</a> might be a good starter.</p>

Teddy_Salas: <p>thank you sir, i only saw nn.transformerencoder in that tutorial.also can you help me find a way to  load cnn model without defining an nn.Module class,is there anyway to do this? when i asked chat gpt it direct me to load into resnet18.</p>

Andrew_Holmes: <p>Hey, If you’re looking for a vanilla CNN, you would have to build that using PyTorch’s torch.nn module. If you’d like a pre-trained (that is a model already trained on a collection of images) then you would have to load a model like ResNet. This <a href="https://pytorch.org/vision/stable/models.html" rel="noopener nofollow ugc">link</a> offers a useful guide to loading pre-trained models. It gives you of suite of models and their weights for their respective tasks. If you need a CNN to be fine-tuned for a specific task, for example classifying planes, you may be able to fine tune a model using a dataset already in torchvision’s <a href="https://pytorch.org/vision/stable/datasets.html" rel="noopener nofollow ugc">Datasets</a> or that’s open source online.<br>
Usually it’s easier to use a pre-trained model to then fine-tune because you don’t have to worry too much about training data and overfitting, but you could always train from scratch.</p>

Teddy_Salas: <aside class="quote no-group" data-username="Teddy_Salas" data-post="3" data-topic="186507">
<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/teddy_salas/48/64758_2.png" class="avatar"> Teddy_Salas:</div>
<blockquote>
<p>to load cnn model without defining an nn.Module class</p>
</blockquote>
</aside>
<p><a class="mention" href="/u/andrew_holmes">@Andrew_Holmes</a> thank you i will inform you the updates after few hours.</p>

Teddy_Salas: <p><a class="mention" href="/u/andrew_holmes">@Andrew_Holmes</a> i used this code to load a pretrained model model = torchvision.models.resnet18(pretrained=True)</p>
<p>model.load_state_dict(torch.load(‘/content/aI_genrtd_img_dectrx1.pth’))  but it showing an error:----------------------------------------------------------------------------<br>
RuntimeError                              Traceback (most recent call last)<br>
 in &lt;cell line: 2&gt;()<br>
1 model = torchvision.models.resnet18(pretrained=True)<br>
----&gt; 2 model.load_state_dict(torch.load(‘/content/aI_genrtd_img_dectrx1.pth’))</p>
<p>/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py in load_state_dict(self, state_dict, strict)<br>
2039<br>
2040         if len(error_msgs) &gt; 0:<br>
 → 2041             raise RuntimeError(‘Error(s) in loading state_dict for {}:\n\t{}’.format(<br>
2042                                self.<strong>class</strong>.<strong>name</strong>, “\n\t”.join(error_msgs)))<br>
2043         return _IncompatibleKeys(missing_keys, unexpected_keys)</p>
<p>RuntimeError: Error(s) in loading state_dict for ResNet:<br>
Missing key(s) in state_dict: “conv1.weight”, “bn1.weight”, “bn1.bias”, “bn1.running_mean”, “bn1.running_var”, “layer1.0.conv1.weight”, “layer1.0.bn1.weight”, “layer1.0.bn1.bias”, “layer1.0.bn1.running_mean”, “layer1.0.bn1.running_var”, “layer1.0.conv2.weight”, “layer1.0.bn2.weight”, “layer1.0.bn2.bias”, “layer1.0.bn2.running_mean”, “layer1.0.bn2.running_var”, “layer1.1.conv1.weight”, “layer1.1.bn1.weight”, “layer1.1.bn1.bias”, “layer1.1.bn1.running_mean”, “layer1.1.bn1.running_var”, “layer1.1.conv2.weight”, “layer1.1.bn2.weight”, “layer1.1.bn2.bias”, “layer1.1.bn2.running_mean”, “layer1.1.bn2.running_var”, “layer2.0.conv1.weight”, “layer2.0.bn1.weight”, “layer2.0.bn1.bias”, “layer2.0.bn1.running_mean”, “layer2.0.bn1.running_var”, “layer2.0.conv2.weight”, “layer2.0.bn2.weight”, “layer2.0.bn2.bias”, “layer2.0.bn2.running_mean”, “layer2.0.bn2.running_var”, “layer2.0.downsample.0.weight”, “layer2.0.downsample.1.weight”, “layer2.0.downsample.1.bias”, “layer2.0.downsample.1.running_mean”, “layer2.0.downsample.1.running_var”, “layer2.1.conv1.weight”, “layer2.1.bn1.weight”, “layer2.1.bn1.bias”, “layer2.1.bn1.running_mean”, “layer2.1.bn1.running_var”, “layer2.1.conv2.weight”, “layer2.1.bn2.weight”, “layer2.1.bn2.bias”, “layer2.1.bn2.running_mean”, “layer2.1.bn2.running_var”, “layer3.0.conv1.weight”, “layer3.0.bn1.weight”, “layer3.0.bn1.bias”, “layer3.0.bn1.running_mean”, “layer3.0.bn1.running_var”, “layer3.0.conv2.weight”, “layer3.0.bn2.weight”, “layer3.0.bn2.bias”, "layer3.0.bn2…<br>
Unexpected key(s) in state_dict: “Conv_block1.0.weight”, “Conv_block1.0.bias”, “Conv_block1.1.weight”, “Conv_block1.1.bias”, “Conv_block1.1.running_mean”, “Conv_block1.1.running_var”, “Conv_block1.1.num_batches_tracked”, “Conv_block1.4.weight”, “Conv_block1.4.bias”, “Conv_block1.5.weight”, “Conv_block1.5.bias”, “Conv_block1.5.running_mean”, “Conv_block1.5.running_var”, “Conv_block1.5.num_batches_tracked”, “Conv_block2.0.weight”, “Conv_block2.0.bias”, “Conv_block2.1.weight”, “Conv_block2.1.bias”, “Conv_block2.1.running_mean”, “Conv_block2.1.running_var”, “Conv_block2.1.num_batches_tracked”, “Conv_block2.4.weight”, “Conv_block2.4.bias”, “Conv_block2.5.weight”, “Conv_block2.5.bias”, “Conv_block2.5.running_mean”, “Conv_block2.5.running_var”, “Conv_block2.5.num_batches_tracked”, “Conv_block3.0.weight”, “Conv_block3.0.bias”, “Conv_block3.1.weight”, “Conv_block3.1.bias”, “Conv_block3.1.running_mean”, “Conv_block3.1.running_var”, “Conv_block3.1.num_batches_tracked”, “Conv_block3.4.weight”, “Conv_block3.4.bias”, “Conv_block3.5.weight”, “Conv_block3.5.bias”, “Conv_block3.5.running_mean”, “Conv_block3.5.running_var”, “Conv_block3.5.num_batches_tracked”, “Conv_block4.0.weight”, “Conv_block4.0.bias”, “Conv_block4.1.weight”, “Conv_block4.1.bias”, “Conv_block4.1.running_mean”, “Conv_block4.1.running_var”, “Conv_block4.1.num_batches_tracked”, “Conv_block4.4.weight”, “Conv_block4.4.bias”, “Conv_block4.5.weight”, “Conv_block4.5.bias”, “Conv_block4.5.running_mean”, "Conv_block4.5.running_va…</p>

ptrblck: <p>You are still running into the same error as described <a href="https://discuss.pytorch.org/t/when-i-load-a-model-i-am-seeing-this-error/186222/28">here</a> and as already explained, you won’t be able to load the <code>state_dict</code> of your custom module into a resnet. ChatGPT is still wrong and you should load the <code>state_dict</code> into an object of your custom model instead.</p>

Teddy_Salas: <p><a class="mention" href="/u/ptrblck">@ptrblck</a>  So, is there no way to load and train the pretrained model on another page without defining the nn.Module class?, now i am doing upload data run the original model the i will load the p retrained model then training it, now i am asking how can i load and train the model without run into original code?, for example if i want to load and train on another computer with don’t have any source code of the model.</p>

ptrblck: <p>You need to provide the source code and create a model instance before loading the <code>state_dict</code>. There are approaches to script the model and to <code>torch.jit.load</code> it, but I would not recommend this approach and would rather stick to the recommended approach.</p>

Teddy_Salas: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> you mean provide source code and load the model?</p>

Andrew_Holmes: <p>I’m not fully comprehending what you’re trying to do, but please correct me if I’m wrong. I believe you’re trying to load weights into a pre-trained model from a .pth file. However, this simply won’t work if the state_dict you’re trying to load doesn’t match the state_dict of the pre-trained model. If you specifically need to use those weights, you would have to define a vanilla model using the torch.nn library that has the same Modules the state_dict of the weights you’re trying to load uses, otherwise you’ll get the error above.</p>
<p>But, if want to use the weights of the pre-trained model, I would retrain it on the data responsible for the weights you’re trying to load from the .pth file. For example if the weights in the .pth was from a model for classifying dogs, I would load ResNet then retrain it on a dataset for dog image classification.</p>
<p>It’s hard to fully understand your intentions, so if you could clarify that, maybe I could provide and example that could help.</p>

Teddy_Salas: <p><a class="mention" href="/u/andrew_holmes">@Andrew_Holmes</a>  I wanted to load pretrained model i trained earlier, and resume training. currently i am doing, i run original model from source code then i define model = tinyvgg1(input=3,hidden=76,output=2).to(device),<br>
saved_model_path = “/content/aI_genrtd_img_dectrx1.pth”</p>
<p>model.load_state_dict(torch.load(saved_model_path))</p>
<p>model.to(device) this how i load a model, before execute this code i need to run orginal model class tinyvgg1(nn.Module):<br>
def <strong>init</strong>(self,hidden,input,output):<br>
super().<strong>init</strong>()<br>
self.Conv_block1 = nn.Sequential(<br>
nn.Conv2d(<br>
in_channels= input,<br>
out_channels=hidden,<br>
kernel_size=3,<br>
stride=1,<br>
padding=1<br>
),<br>
nn.BatchNorm2d(hidden),<br>
nn.ReLU(),<br>
nn.Dropout(0.2),<br>
nn.Conv2d(<br>
in_channels = hidden,<br>
out_channels =hidden,<br>
kernel_size=3,<br>
stride=1,<br>
padding=1<br>
),<br>
nn.BatchNorm2d(hidden),<br>
nn.ReLU(),<br>
nn.MaxPool2d(kernel_size=2,stride=2)<br>
)<br>
self.Conv_block2 = nn.Sequential(<br>
nn.Conv2d(<br>
in_channels=hidden,<br>
out_channels=hidden,<br>
kernel_size=3,<br>
stride=1,<br>
padding=1<br>
),<br>
nn.BatchNorm2d(hidden),<br>
nn.ReLU(),<br>
nn.Dropout(0.2),<br>
nn.Conv2d(<br>
in_channels=hidden,<br>
out_channels=hidden,<br>
kernel_size=3,<br>
stride=1,<br>
padding=1<br>
),</p>
<pre><code>    nn.BatchNorm2d(hidden),
    nn.ReLU(),
    nn.MaxPool2d(kernel_size=2,stride=2)
)
self.Conv_block3 = nn.Sequential(
    nn.Conv2d(
        in_channels=hidden,
        out_channels=hidden,
        kernel_size=3,
        stride=1,
        padding=1
    ),
    nn.BatchNorm2d(hidden),
    nn.ReLU(),
    nn.Dropout(0.2),
    nn.Conv2d(
        in_channels=hidden,
        out_channels=hidden,
        kernel_size=3,
        stride=1,
        padding=1
    ),
    nn.BatchNorm2d(hidden),
    nn.ReLU(),
    nn.MaxPool2d(kernel_size=2,stride=2)
)
self.Conv_block4 = nn.Sequential(
    nn.Conv2d(
        in_channels=hidden,
        out_channels=hidden,
        kernel_size=3,
        stride=1,
        padding=1
    ),
    nn.BatchNorm2d(hidden),
    nn.ReLU(),
    nn.Dropout(0.2),
    nn.Conv2d(
        in_channels=hidden,
        out_channels=hidden,
        kernel_size=3,
        stride=1,
        padding=1
    ),
    nn.BatchNorm2d(hidden),
    nn.ReLU(),
    nn.MaxPool2d(kernel_size=2,stride=2)
)

self.classifier = nn.Sequential(
    nn.Flatten(),
    nn.Linear(
        in_features = hidden*14*14,
        out_features = output
    )
)
</code></pre>
<p>def forward(self,x):<br>
x = self.Conv_block1(x)<br>
<span class="hashtag-raw">#print</span>(x.shape)<br>
x = self.Conv_block2(x)<br>
<span class="hashtag-raw">#print</span>(x.shape)<br>
x = self.Conv_block3(x)<br>
<span class="hashtag-raw">#print</span>(x.shape)<br>
x = self.Conv_block4(x)<br>
<span class="hashtag-raw">#print</span>(x.shape)<br>
x = self.classifier(x)<br>
return x<br>
return self.Conv_block4(self.Conv_block3(self.Conv_block2(self.Conv_block1(x))))<br>
i meant this one from source code.  my need is i need to load the model without run the model class tinyvgg1(nn.Module)(above mentioned model).is that possible for example  , i am opening a another computer don’t have any source code, i have train_model.pth with me . can i upload the .pth model and training it without running any source code, is this possible?</p>
<p>exmaple like this:- saved_model_path = “/content/aI_genrtd_img_dectrx1.pth”</p>
<p>model.load_state_dict(torch.load(saved_model_path))</p>
<p>model.to(device)  load the model and training it.</p>

Andrew_Holmes: <p>Okay now I understand!</p>
<p>Yes, you would need the tinyvgg1 source code in order to use those weights on a different computer. There’s two ways you could go around this:</p>
<ol>
<li>
<p>save it to a GitHub repository.<br>
I’d make a GitHub repo for the tinyvgg1 model then push the source code and any dependencies to that repo. Then, you could clone the repo from a different machine and load the weights into a vanilla tinyvgg1 as long as you have access to the .pth file for the weights. It’s common practice to use GitHub for projects and GitHub actually hosts the PyTorch library in you ever wondered.</p>
</li>
<li>
<p>save the entire model instead.<br>
You could just save the entire model then load it using torch.save() and torch.load(). The only difference is you don’t pass the state_dict of the model and instead pass the entire model itself. You can see a better explanation <a href="https://pytorch.org/tutorials/beginner/saving_loading_models.html" rel="noopener nofollow ugc">here</a> under the Save/Load Entire Model section. There’s some reasons this isn’t usually done, but for your need it could help.</p>
</li>
</ol>

Teddy_Salas: <p><a class="mention" href="/u/andrew_holmes">@Andrew_Holmes</a> i understand thank you</p>