示例#1
0
    def test_ObserveEmbeddingConvNet3D4C(self):
        batch_size = 32
        channels = 3
        output_dim = 128
        input_batch_shape = [batch_size, channels, 16, 16, 16]
        output_batch_shape_correct = [batch_size, output_dim]
        input_non_batch_shape = [channels, 16, 16, 16]
        output_non_batch_shape_correct = [1, output_dim]

        input_batch = Variable(util.Tensor(torch.Size(input_batch_shape)))
        input_non_batch = Variable(
            util.Tensor(torch.Size(input_non_batch_shape)))
        nn = ObserveEmbeddingConvNet3D4C(
            input_example_non_batch=input_non_batch, output_dim=output_dim)
        nn.configure()
        output_batch_shape = list(nn.forward(input_batch).size())
        output_non_batch_shape = list(
            nn.forward(input_non_batch.unsqueeze(0)).size())

        util.debug('batch_size', 'channels', 'output_dim', 'input_batch_shape',
                   'output_batch_shape', 'output_batch_shape_correct',
                   'input_non_batch_shape', 'output_non_batch_shape',
                   'output_non_batch_shape_correct')

        self.assertEqual(output_batch_shape, output_batch_shape_correct)
        self.assertEqual(output_non_batch_shape,
                         output_non_batch_shape_correct)
示例#2
0
    def test_dist_empirical(self):
        values = Variable(util.Tensor([1, 2, 3]))
        log_weights = Variable(util.Tensor([1, 2, 3]))
        dist_mean_correct = 2.5752103328704834
        dist_stddev_correct = 0.6514633893966675
        dist_expectation_sin_correct = 0.3921678960323334
        dist_map_sin_mean_correct = 0.3921678960323334
        dist_min_correct = 1
        dist_max_correct = 3
        # dist_sample_shape_correct = []

        dist = Empirical(values, log_weights)
        dist_empirical = Empirical([dist.sample() for i in range(empirical_samples)])
        dist_mean = float(dist.mean)
        dist_mean_empirical = float(dist_empirical.mean)
        dist_stddev = float(dist.stddev)
        dist_stddev_empirical = float(dist_empirical.stddev)
        dist_expectation_sin = float(dist.expectation(torch.sin))
        dist_map_sin_mean = float(dist.map(torch.sin).mean)
        dist_min = float(dist.min)
        dist_max = float(dist.max)
        dist_sample_shape = list(dist.sample().size())

        util.debug('dist_mean', 'dist_mean_empirical', 'dist_mean_correct', 'dist_stddev', 'dist_stddev_empirical', 'dist_stddev_correct', 'dist_expectation_sin', 'dist_expectation_sin_correct', 'dist_map_sin_mean', 'dist_map_sin_mean_correct', 'dist_min', 'dist_min_correct', 'dist_max', 'dist_max_correct')

        # self.assertEqual(dist_sample_shape, dist_sample_shape_correct)
        self.assertAlmostEqual(dist_mean, dist_mean_correct, places=1)
        self.assertAlmostEqual(dist_mean_empirical, dist_mean_correct, places=1)
        self.assertAlmostEqual(dist_stddev, dist_stddev_correct, places=1)
        self.assertAlmostEqual(dist_stddev_empirical, dist_stddev_correct, places=1)
        self.assertAlmostEqual(dist_expectation_sin, dist_expectation_sin_correct, places=1)
        self.assertAlmostEqual(dist_map_sin_mean, dist_map_sin_mean_correct, places=1)
        self.assertAlmostEqual(dist_min, dist_min_correct, places=1)
        self.assertAlmostEqual(dist_max, dist_max_correct, places=1)
示例#3
0
    def test_dist_empirical_save_load(self):
        file_name = os.path.join(tempfile.mkdtemp(), str(uuid.uuid4()))
        values = Variable(util.Tensor([1, 2, 3]))
        log_weights = Variable(util.Tensor([1, 2, 3]))
        dist_mean_correct = 2.5752103328704834
        dist_stddev_correct = 0.6514633893966675
        dist_expectation_sin_correct = 0.3921678960323334
        dist_map_sin_mean_correct = 0.3921678960323334

        dist_on_file = Empirical(values, log_weights)
        dist_on_file.save(file_name)
        dist = Distribution.load(file_name)
        os.remove(file_name)
        dist_empirical = Empirical([dist.sample() for i in range(empirical_samples)])
        dist_mean = float(dist.mean)
        dist_mean_empirical = float(dist_empirical.mean)
        dist_stddev = float(dist.stddev)
        dist_stddev_empirical = float(dist_empirical.stddev)
        dist_expectation_sin = float(dist.expectation(torch.sin))
        dist_map_sin_mean = float(dist.map(torch.sin).mean)

        util.debug('file_name', 'dist_mean', 'dist_mean_empirical', 'dist_mean_correct', 'dist_stddev', 'dist_stddev_empirical', 'dist_stddev_correct', 'dist_expectation_sin', 'dist_expectation_sin_correct', 'dist_map_sin_mean', 'dist_map_sin_mean_correct')

        self.assertAlmostEqual(dist_mean, dist_mean_correct, places=1)
        self.assertAlmostEqual(dist_mean_empirical, dist_mean_correct, places=1)
        self.assertAlmostEqual(dist_stddev, dist_stddev_correct, places=1)
        self.assertAlmostEqual(dist_stddev_empirical, dist_stddev_correct, places=1)
        self.assertAlmostEqual(dist_expectation_sin, dist_expectation_sin_correct, places=1)
        self.assertAlmostEqual(dist_map_sin_mean, dist_map_sin_mean_correct, places=1)
示例#4
0
def NDArray_to_Tensor(ndarray):
    if ndarray is None:
        # util.log_warning('NDArray_to_Tensor: empty NDArray received, returning empty tensor')
        return util.Tensor()
    else:
        b = ndarray._tab.Bytes
        o = flatbuffers.number_types.UOffsetTFlags.py_type(
            ndarray._tab.Offset(4))
        offset = ndarray._tab.Vector(o) if o != 0 else 0
        length = ndarray.DataLength()
        data_np = np.frombuffer(b,
                                offset=offset,
                                dtype=np.dtype('float64'),
                                count=length)

        o = flatbuffers.number_types.UOffsetTFlags.py_type(
            ndarray._tab.Offset(6))
        offset = ndarray._tab.Vector(o) if o != 0 else 0
        length = ndarray.ShapeLength()
        shape_np = np.frombuffer(b,
                                 offset=offset,
                                 dtype=np.dtype('int32'),
                                 count=length)

        # print('data:', data_np)
        # print('shape', shape_np)

        data = data_np.reshape(shape_np)
        return util.Tensor(data)