Optimizer got an empty parameter list in a hand made code

scollazo: <p>Hi! I got the following error <em>ValueError: optimizer got an empty parameter list</em> when I run the code containing the following classes:</p>
<pre><code class="lang-auto">class Mass_and_v():
    def __init__(self, param):
        self.parameter = param
        
    # get_mass + v_circ_tot
    def __call__(self, x):
        ener_f = 56.0 # keV
        r, mass = rar.model(self.parameter[3:].detach().numpy(),ener_f)
        isnan = np.argwhere(np.isnan(mass))
        if (np.any(isnan)):
            np.argwhere(np.isnan(mass))[0][0]
        else:
            k = -1
        r_s = r[0:k]
        mass_s = mass[0:k]
        mass_spline = InterpolatedUnivariateSpline(r_s, mass_s, k=4)
        r_max = np.amax(r_s)
        if x &lt;= r_max:
            return np.sqrt(v_circ_b(x,self.parameter[0].detach().numpy())**2 + v_circ_MN(x,self.parameter[1:3].detach().numpy())**2 + G_u*mass_spline(x)/x)
        else:
            return np.sqrt(v_circ_b(x,self.parameter[0].detach().numpy())**2 + v_circ_MN(x,self.parameter[1:3].detach().numpy())**2 + G_u*mass_spline(r_max)/x)

class FitModel(nn.Module):
    def __init__(self):
        super(FitModel, self).__init__()
        self.nonlinear_stack = Mass_and_v(param_t)
        
    def forward(self, x):
        output = self.nonlinear_stack(x)
        return output
</code></pre>
<p>I got the error when the code comes to the line</p>
<pre><code class="lang-auto">optimizer = torch.optim.Adam(model.parameters(), lr=eta, eps=1e-08, weight_decay=0, amsgrad=False)
</code></pre>
<p>I hope someone can help me. Thank you so much in advance <img src="https://discuss.pytorch.org/images/emoji/apple/slightly_smiling_face.png?v=10" title=":slightly_smiling_face:" class="emoji" alt=":slightly_smiling_face:"></p>

ptrblck: <p><code>Mass_and_v</code> is not an <code>nn.Module</code> class and also uses numpy arrays internally, which Autograd won’t be able to track.<br>
Derive <code>Mass_and_v</code> from <code>nn.Module</code> and register the parameters as <code>nn.Parameter</code> in its <code>__init__</code>.</p>

scollazo: <p>Great! That was very useful. Thank you!<br>
So, let me see if I understand (I am kind of new in PyTorch), I need to do <code>Mass_and_v(nn.Module)</code> when I define that class. Also, I need to change the line <code>self.parameter = param</code> for <code>self.parameter = nn.Parameter(data=param, requires_grad=True)</code>. Is that right? Well, If I do this, now I get:</p>
<p><em>AttributeError: cannot assign parameters before Module.<strong>init</strong>() call</em></p>
<p>Apart from all of these, do you suggest that I have to code the <code>Mass_and_v</code> class all based in PyTorch, not using numpy?</p>

ptrblck: <p>The <code>AttributeError</code> is raised, as you’ve most likely forgotten to add the <code>super().__init__()</code> call into the <code>__init__</code> method.</p>
<aside class="quote no-group" data-username="scollazo" data-post="3" data-topic="137633">
<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/scollazo/48/44230_2.png" class="avatar"> scollazo:</div>
<blockquote>
<p>Apart from all of these, do you suggest that I have to code the <code>Mass_and_v</code> class all based in PyTorch, not using numpy?</p>
</blockquote>
</aside>
<p>Yes, I would try to avoid using other libraries if possible, since you would break the computation graph and PyTorch won’t be able to track these operations. If really necessary, you could write a custom <code>autograd.Function</code> and define the <code>forward</code> as well as <code>backward</code> methods manually.</p>

scollazo: <p>Great! I understand. Thank you so much for your help <img src="https://discuss.pytorch.org/images/emoji/apple/slightly_smiling_face.png?v=10" title=":slightly_smiling_face:" class="emoji" alt=":slightly_smiling_face:"></p>