I am trying to build Logistic matrix factorization.When I run it,the error says that :optimizer got an empty parameter list.I am unfamiliar to PyTorch and I don’t know what causes the error.Can you give me some suggestions?Thank you!
this is my code
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)
self.R = torch.tensor(R_test)
self.n_user = C.shape[0]
self.n_item = C.shape[1]
self.X = torch.randn([self.n_user, self.n_factor], requires_grad=True)
self.Y = torch.randn([self.n_item, self.n_factor], requires_grad=True)
self.user_biases = torch.randn((self.n_user, 1), requires_grad=True)
self.item_biases = torch.randn((self.n_item, 1), requires_grad=True)
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.dot(A) - (1+C).dot(torch.log((1+torch.exp(A))))
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, axis=1))
A = R * (R_hat_rank / self.n_item)
rank = torch.sum(A)/ R_sum
return rank.item()
''''''''''''''''
''''''''''''''''
''''''''''''''''
model=LMF(C,R_test,10)
learning_rate=0.01
optimizer=torch.optim.Adam(params = model.parameters() , lr = learning_rate)