Overwrite parameters of model with new values

ElToto: <p><strong>EDIT</strong> already solved it at the bottom<br>
I have a nn.Model that has multiple other nn.Models as attribute.<br>
I want to access the weights of the underlying models (they contain e.g. conv2d layer)</p>
<p>Dummy model here:</p>
<pre><code class="lang-auto">import torch
import torch.nn as nn

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.module_list = create_modules()

def create_modules():
        module_list = nn.ModuleList()
        for i in range(3):
            modules = nn.Sequential()
            if i == 0:
                modules.add_module('Conv2d', nn.Conv2d(in_channels=1,
                                                        out_channels=1,
                                                        kernel_size=3))
                modules.add_module('BatchNorm2d', nn.BatchNorm2d(1, momentum=0.03, eps=1E-4))
                modules.add_module('activation', nn.LeakyReLU(0.1, inplace=True))
        
            if i == 1:
                maxpool = nn.MaxPool2d(kernel_size=(2, 2))
                modules = maxpool
            if i == 2:
                modules.add_module('Conv2d', nn.Conv2d(in_channels=1,
                                        out_channels=1,
                                        kernel_size=3))
            module_list.append(modules)
        return module_list
        
model = Net()
for params in model.parameters():
    print(params)
</code></pre>
<p>This prints</p>
<pre><code class="lang-auto">Parameter containing:
tensor([[[[ 0.0781, -0.2562, -0.2447],
          [-0.2310, -0.2009, -0.1493],
          [ 0.1241,  0.0419, -0.2956]]]], requires_grad=True)
Parameter containing:
tensor([-0.1773], requires_grad=True)
Parameter containing:
tensor([1.], requires_grad=True)
Parameter containing:
tensor([0.], requires_grad=True)
Parameter containing:
tensor([[[[ 0.0511, -0.0956, -0.0024],
          [-0.0377,  0.1948, -0.1699],
          [-0.3166, -0.0658, -0.0656]]]], requires_grad=True)
Parameter containing:
tensor([0.2309], requires_grad=True)
</code></pre>
<p>As you can see the conv2d layers are randomally initialized.</p>
<p>Now I want to set the weights of these two layers as torch.ones()<br>
But this has to happen after the model is created.<br>
So in my dummy code after</p>
<pre><code>model = net()
</code></pre>
<p>For this I need to overwrite the parameters of my model with torch.ones()<br>
But I am unsure on how to do that.</p>
<p>After the overwrite</p>
<pre><code class="lang-auto">for param in model.parameters()
</code></pre>
<p>should show</p>
<pre><code class="lang-auto">[1,1,1],[1,1,1],[1,1,1]
</code></pre>
<p>for my conv layers.</p>
<p>So how do I access the weights of a layer that is inside a model?</p>
<p><strong>EDIT</strong> fixed it:<br>
Inplace operation can do it:</p>
<pre><code class="lang-auto">for param in model.parameters():
    param.data.fill_(1.0)
</code></pre>
<p>works</p>

Mughees: <p>There must be many ways but i guess you can use <code>net.state_dict()</code> for it as explained in toy example.</p>
<pre><code class="lang-auto">import torch
import torch.nn as nn
import torchvision

net = torchvision.models.resnet18(pretrained=True)

pretrained_dict = net.state_dict()
conv_weights = pretrained_dict['conv1.weight'] #64,3,7,7

new = torch.tensor((), dtype=torch.int32)
new = new.new_ones(conv_weights.shape)

pretrained_dict['conv1.weight'] = new


net.load_state_dict(pretrained_dict)
param = list(net.parameters())

print(param[0])
</code></pre>

mariosasko: <p>Note that <code>data</code> is deprecated in favour of <code>detach()</code>.</p>