I’m trying to train this:
class model(torch.nn.Module): def __init__(self, D_in, H): super(model,self).__init__() self.transfer = torch.nn.Sigmoid() self.lin1 = torch.nn.Linear(D_in, H) self.lin2 = torch.nn.Linear(H, 1) def forward(self, x1, x2): x1 = self.lin1(x1) x1 = self.transfer(x1) x1 = self.lin2(x1) x2 = self.lin1(x2) x2 = self.transfer(x2) x2 = self.lin2(x2) x1 = Variable(torch.Tensor([[1e0, x1, x2], [x1, 4e0, 0e0], [x2, 0e0, 2e0]])) x1, eigvec = torch.eig(x1,eigenvectors=False) x1 = torch.min(x1[:,0]) # Get the lowest eigenvalue of the matrix, just the real part. return x1
The forward pass works fine, but when I try to do loss.backward() y get:
RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn
I guess I got many errors, from the way I built the matrix to diagonalize to that I cannot use torch.eig in this context. So my question is. Is there any way to train a model like this is pytorch?
Thank you very much!