I have solved this problem by using nn.Parameter like follow:
class TorchFM(nn.Module):
    def __init__(self, input_dim):
        super(TorchFM, self).__init__()
        w0 = torch.zeros(1).double()
        w = torch.zeros(input_dim).double()
        v = torch.empty(input_dim, 10).double()
        nn.init.xavier_normal_(v)

        self.w0 = nn.Parameter(w0)
        self.w = nn.Parameter(w)
        self.v = nn.Parameter(v)

    def forward(self, x):
        torch.set_grad_enabled(True)
        linear_terms = torch.add(self.w0, torch.sum(torch.mul(self.w, x)))
        pair_interactions = 0.5 * torch.sum(torch.sub(torch.pow(torch.matmul(x, self.v), 2),
                                                      torch.matmul(torch.pow(x, 2), torch.pow(self.v, 2))))

        out = torch.add(linear_terms, pair_interactions)
        return out