Пример #1
0
 def test_batch_eval_neg_michalewicz(self, cuda=False):
     device = torch.device("cuda") if cuda else torch.device("cpu")
     for dtype in (torch.float, torch.double):
         X = torch.zeros(2, 10, device=device, dtype=dtype)
         res = neg_michalewicz(X)
         self.assertEqual(res.dtype, dtype)
         self.assertEqual(res.device.type, device.type)
         self.assertEqual(res.shape, torch.Size([2]))
Пример #2
0
 def test_batch_eval_neg_michalewicz(self, cuda=False):
     device = torch.device("cuda") if cuda else torch.device("cpu")
     for dtype in (torch.float, torch.double):
         X = torch.zeros(2, 10, device=device, dtype=dtype)
         res = neg_michalewicz(X)
         self.assertEqual(res.dtype, dtype)
         self.assertEqual(res.device.type, device.type)
         self.assertEqual(res.shape, torch.Size([2]))
Пример #3
0
 def test_neg_michalewicz_global_maximum(self, cuda=False):
     device = torch.device("cuda") if cuda else torch.device("cpu")
     for dtype in (torch.float, torch.double):
         X = torch.tensor(
             GLOBAL_MAXIMIZER, device=device, dtype=dtype, requires_grad=True
         )
         res = neg_michalewicz(X)
         res.backward()
         self.assertAlmostEqual(res.item(), GLOBAL_MAXIMUM, places=4)
         self.assertLess(X.grad.abs().max().item(), 1e-3)
Пример #4
0
 def test_neg_michalewicz_global_maximum(self, cuda=False):
     device = torch.device("cuda") if cuda else torch.device("cpu")
     for dtype in (torch.float, torch.double):
         X = torch.tensor(GLOBAL_MAXIMIZER,
                          device=device,
                          dtype=dtype,
                          requires_grad=True)
         res = neg_michalewicz(X)
         self.assertAlmostEqual(res.item(), GLOBAL_MAXIMUM, places=4)
         grad = torch.autograd.grad(res, X)[0]
         self.assertLess(grad.abs().max().item(), 1e-3)