示例#1
0
 def test_construct_base_samples(self, cuda=False):
     device = torch.device("cuda") if cuda else torch.device("cpu")
     test_shapes = [
         {
             "batch": [2],
             "output": [4, 3],
             "sample": [5]
         },
         {
             "batch": [1],
             "output": [5, 3],
             "sample": [5, 6]
         },
         {
             "batch": [2, 3],
             "output": [2, 3],
             "sample": [5]
         },
     ]
     for tshape in test_shapes:
         batch_shape = torch.Size(tshape["batch"])
         output_shape = torch.Size(tshape["output"])
         sample_shape = torch.Size(tshape["sample"])
         expected_shape = sample_shape + batch_shape + output_shape
         for dtype in (torch.float, torch.double):
             for qmc in (False, True):
                 for seed in (None, 1234):
                     samples = construct_base_samples(
                         batch_shape=batch_shape,
                         output_shape=output_shape,
                         sample_shape=sample_shape,
                         qmc=qmc,
                         seed=seed,
                         device=device,
                         dtype=dtype,
                     )
                     self.assertEqual(samples.shape, expected_shape)
                     self.assertEqual(samples.device.type, device.type)
                     self.assertEqual(samples.dtype, dtype)
     # check that warning is issued if dimensionality is too large
     with warnings.catch_warnings(record=True) as w:
         construct_base_samples(
             batch_shape=torch.Size(),
             output_shape=torch.Size([200, 6]),
             sample_shape=torch.Size([1]),
             qmc=True,
         )
         self.assertEqual(len(w), 1)
         self.assertTrue(issubclass(w[-1].category, SamplingWarning))
         exp_str = f"maximum supported by qmc ({SobolEngine.MAXDIM})"
         self.assertTrue(exp_str in str(w[-1].message))
示例#2
0
 def test_construct_base_samples(self):
     test_shapes = [
         {
             "batch": [2],
             "output": [4, 3],
             "sample": [5]
         },
         {
             "batch": [1],
             "output": [5, 3],
             "sample": [5, 6]
         },
         {
             "batch": [2, 3],
             "output": [2, 3],
             "sample": [5]
         },
     ]
     for tshape, qmc, seed, dtype in itertools.product(
             test_shapes, (False, True), (None, 1234),
         (torch.float, torch.double)):
         batch_shape = torch.Size(tshape["batch"])
         output_shape = torch.Size(tshape["output"])
         sample_shape = torch.Size(tshape["sample"])
         expected_shape = sample_shape + batch_shape + output_shape
         samples = construct_base_samples(
             batch_shape=batch_shape,
             output_shape=output_shape,
             sample_shape=sample_shape,
             qmc=qmc,
             seed=seed,
             device=self.device,
             dtype=dtype,
         )
         self.assertEqual(samples.shape, expected_shape)
         self.assertEqual(samples.device.type, self.device.type)
         self.assertEqual(samples.dtype, dtype)
     # check that warning is issued if dimensionality is too large
     with warnings.catch_warnings(record=True) as w, settings.debug(True):
         construct_base_samples(
             batch_shape=torch.Size(),
             output_shape=torch.Size([2000,
                                      11]),  # 200 * 11 = 22000 > 21201
             sample_shape=torch.Size([1]),
             qmc=True,
         )
         self.assertEqual(len(w), 1)
         self.assertTrue(issubclass(w[-1].category, SamplingWarning))
         exp_str = f"maximum supported by qmc ({SobolEngine.MAXDIM})"
         self.assertTrue(exp_str in str(w[-1].message))
示例#3
0
 def test_construct_base_samples(self, cuda=False):
     device = torch.device("cuda") if cuda else torch.device("cpu")
     test_shapes = [
         {"batch": [2], "output": [4, 3], "sample": [5]},
         {"batch": [1], "output": [5, 3], "sample": [5, 6]},
         {"batch": [2, 3], "output": [2, 3], "sample": [5]},
     ]
     for tshape in test_shapes:
         batch_shape = torch.Size(tshape["batch"])
         output_shape = torch.Size(tshape["output"])
         sample_shape = torch.Size(tshape["sample"])
         expected_shape = sample_shape + batch_shape + output_shape
         for dtype in (torch.float, torch.double):
             for qmc in (False, True):
                 for seed in (None, 1234):
                     samples = construct_base_samples(
                         batch_shape=batch_shape,
                         output_shape=output_shape,
                         sample_shape=sample_shape,
                         qmc=qmc,
                         seed=seed,
                         device=device,
                         dtype=dtype,
                     )
                     self.assertEqual(samples.shape, expected_shape)
                     self.assertEqual(samples.device.type, device.type)
                     self.assertEqual(samples.dtype, dtype)
     # check that warning is issued if dimensionality is too large
     with warnings.catch_warnings(record=True) as w:
         construct_base_samples(
             batch_shape=torch.Size(),
             output_shape=torch.Size([200, 6]),
             sample_shape=torch.Size([1]),
             qmc=True,
         )
         self.assertEqual(len(w), 1)
         self.assertTrue(issubclass(w[-1].category, SamplingWarning))
         exp_str = f"maximum supported by qmc ({SobolEngine.MAXDIM})"
         self.assertTrue(exp_str in str(w[-1].message))