Why my model is empty？

username: springtostring
cooked: <p>I am trying to build Logistic matrix factorization.When I train my model, I found that my model is empty.I am unfamiliar to PyTorch and I don’t know what causes the error.It really confuse me.Can you give me some suggestions?Thank you!<br>
this is my code</p>
<pre><code class="lang-auto">class LMF(nn.Module):
    def __init__(self, C, R_test , n_factor):
       '''
       :param C:
       :param R_test:
       :param n_factor:
       '''
       super(LMF, self).__init__() 
       self.n_factor = n_factor
       self.C = torch.tensor(C , dtype=torch.double)
       self.R = torch.tensor(R_test , dtype=torch.double)
       self.n_user = C.shape[0]
       self.n_item = C.shape[1]
       self.X = Parameter(torch.randn([self.n_user, self.n_factor], dtype=torch.double))
       self.Y = Parameter(torch.randn([self.n_item, self.n_factor], dtype=torch.double))
       self.user_biases = Parameter(torch.randn((self.n_user, 1), dtype=torch.double))
       self.item_biases = Parameter(torch.randn((self.n_item, 1), dtype=torch.double))

    def forward(self):

        A=torch.mm(self.X,self.Y.T)
        A=A+self.user_biases
        A=A+self.item_biases.T
        loss=self.C*(A) - (1+self.C)*(torch.log((1+torch.exp(A))))
        loss=loss.sum()
        return -loss

    def MPR(self):
        rank = 0.0
        R_sum = torch.sum(self.R)
        R_hat=torch.mm(self.X , self.Y.T)
        R_hat_rank = torch.argsort(torch.argsort(-R_hat, dim=1))
        print(self.R.size())
        print(R_hat_rank .size())
        A = self.R*(R_hat_rank / torch.tensor(self.n_item,dtype=torch.double))
        rank = torch.sum(A)/ R_sum
        return rank.item()
</code></pre>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/0/6/062be30ce9fdcadab3b2ae94cf54738510a8b7e9.png" data-download-href="https://discuss.pytorch.org/uploads/default/062be30ce9fdcadab3b2ae94cf54738510a8b7e9" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/0/6/062be30ce9fdcadab3b2ae94cf54738510a8b7e9.png" alt="image" data-base62-sha1="SATjfSfmctBM7VHLWgexdoucmR" width="690" height="80" data-dominant-color="F8F8F8"><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">1086×127 1.08 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>

username: ptrblck
cooked: <p><code>print(model)</code> will print the registered modules, which are not present in your current implementation.<br>
You could create the <code>extra_repr</code> method, which would print additional information about your custom module, similar to <a href="https://github.com/pytorch/pytorch/blob/18122facb9bffe65dbd8fc9d556839228d701b2b/torch/nn/modules/linear.py#L93-L96">this one</a>.</p>
<p>To print the parameters, you could use:</p>
<pre><code class="lang-python">for name, param in model.named_parameters():
    print(name, param)
</code></pre>
<p>Note that tensors will not be registered inside the module, and won’t be returned by <code>model.parameters()</code>, <code>model.buffers()</code>, or <code>model.state_dict()</code>.<br>
To register tensors as buffers, use <code>self.register_buffer(name, tensor)</code>.</p>

username: springtostring
cooked: <p>thank you very much!</p>