Newbie question ; How do I put this forward call into a model? to get model parameters saved

suegi: <p>x = random_hot<br>
x = x.unsqueeze(1)<br>
x = torch.nn.Embedding(vocab_size,100)(x)<br>
x = torch.nn.Flatten()(x)<br>
print(x.shape)<br>
x = x.unsqueeze(0)</p>
<p>for i in range(5):<br>
x = torch.nn.Conv1d(x.shape[1], 50, 100, stride=1)(x)<br>
x = torch.nn.ReLU()(x)<br>
x = torch.nn.Conv1d(x.shape[1], 50, 100, stride=2)(x)<br>
x = torch.nn.ReLU()(x)</p>
<p>x = torch.nn.Flatten()(x)</p>
<p>x = torch.nn.Linear(x.shape[1],512)(x)<br>
x = torch.nn.ReLU()(x)<br>
x = torch.nn.Linear(512,128)(x)<br>
x = torch.nn.Linear(128,1)(x)</p>
<p>This sends my input through the network without errors. I put this in the forward() call of a new nn.Module object.  I left the <strong>init</strong> empty. When I tried using an optimizer, it gives an error saying model.parameters() are empty.</p>
<p>I thought I may need to put all my layers explicitly in the <strong>init</strong> part of the module. For instance</p>
<p>self.conv1 = nn.Conv1d( …)<br>
self.conv2 = nn.Conv1d( …)<br>
…<br>
self.conv10 = nn.Conv1d( …)</p>
<p>Is this the case and if so, how would I encode the dynamic shape of x.shape[1] which my loop relies on? Is this even how I get model.parameters to be saved so I can use an optimizer?</p>

my3bikaht: <p>You need to create instances of all your layers in constructor. Then call these instances in forward pass.</p>
<pre><code class="lang-auto">def __init__(self):
    self.conv1 = nn.Conv2d(1,32,5)

def forward(self, x):
    x = self.conv1(x)
</code></pre>
<p>as for the dynamic shape you can use input with largest shape possible and pad it with zeroes</p>

suegi: <p>Ah… I rewrote it and can train my model now…</p>
<p>class CNNtext(nn.Module):<br>
def <strong>init</strong>(self, vocab_size):<br>
super(CNNtext, self).<strong>init</strong>()</p>
<pre><code>    self.embed = torch.nn.Embedding(vocab_size, 100)
    self.flat1 = torch.nn.Flatten()
    
    
    self.conv1 = torch.nn.Conv1d(1,50,100,stride=1)
    self.relu1 = torch.nn.ReLU()
    
    self.conv2 = torch.nn.Conv1d(50,50,100,stride=2)
    self.relu2 = torch.nn.ReLU()
    
    self.conv3 = torch.nn.Conv1d(50,50,100,stride=1)
    self.relu3 = torch.nn.ReLU()
    
    self.conv4 = torch.nn.Conv1d(50,50,100,stride=2)
    self.relu4 = torch.nn.ReLU()
    
    self.flatten2 = torch.nn.Flatten()
    
    self.linear1 = torch.nn.Linear(383850,512)
    self.relu5 = torch.nn.ReLU()
    self.output = torch.nn.Linear(512,1)


def forward(self,x):

    x = self.embed(x)
    x = self.flat1(x)
    x = x.unsqueeze(0)
    x = self.conv1(x)
    x = self.relu1(x)
    x = self.conv2(x)
    x = self.relu2(x)
    x = self.conv3(x)
    x = self.relu3(x)
    x = self.conv4(x)
    x = self.relu4(x)
    x = self.flatten2(x)
    x = self.linear1(x)
    x = self.relu5(x)
    x = self.output(x)
    
    return x
</code></pre>
<p>I had to build it step by step to find out what size to use for self.linear1… now I have just 2 questions</p>
<ol>
<li>
<p>How can I build self.linear1() without having to build the network up to see what size it expects?</p>
</li>
<li>
<p>How can I put these conv blocks in a loop. Am I suppose to build a 2nd model and call it within the main model? ( but doing so I would need to again know the answer for question 1) - or can I add a method in my main model to build the layer blocks?</p>
</li>
</ol>

my3bikaht: <ol>
<li>Math <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=10" title=":slight_smile:" class="emoji" alt=":slight_smile:"> for convolutional layers widh kernel=1 and stride=1 padding=0 will keep same wxh. kernel=3, stride=2, padding=1 will halve them.</li>
<li>You can use loops. Or create inner models and call them. Take a look at <a href="https://github.com/pytorch/vision/blob/main/torchvision/models/resnet.py" rel="noopener nofollow ugc">source code</a> of resnet, for example.</li>
</ol>

suegi: <p>TIL lazyLinear does the trick without the math <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=10" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>