Esempio n. 1
0
    def test__add_visible_units(self):
        sys.stdout.write(
            'GaussianBinaryRBM -> Performing add_visible_units test ...')
        sys.stdout.flush()
        localmodel = Model.GaussianBinaryRBM(number_visibles=1,
                                             number_hiddens=2,
                                             data=self.gbrbmData[:, 0].reshape(
                                                 2, 1),
                                             initial_weights='AUTO',
                                             initial_visible_bias='AUTO',
                                             initial_hidden_bias='AUTO',
                                             initial_sigma='AUTO',
                                             initial_visible_offsets='AUTO',
                                             initial_hidden_offsets='AUTO')
        localmodel._add_visible_units(num_new_visibles=1,
                                      position=1,
                                      initial_weights='AUTO',
                                      initial_bias='AUTO',
                                      initial_sigmas='AUTO',
                                      initial_offsets='AUTO',
                                      data=self.gbrbmData[:, 1].reshape(2, 1))
        assert numx.all(localmodel.bv == numx.mean(
            self.gbrbmData, axis=0).reshape(1, self.gbrbmData.shape[1]))
        assert numx.all(localmodel.ov == 0.0)
        assert numx.all(
            numx.abs(localmodel.bv_base - numx.mean(self.gbrbmData, axis=0).
                     reshape(1, self.gbrbmData.shape[1])) < self.epsilon)
        assert numx.all(
            numx.abs(localmodel.sigma - numx.clip(
                numx.std(self.gbrbmData, axis=0).reshape(
                    1, self.gbrbmData.shape[1]), 0.001,
                numx.finfo(localmodel.dtype).max)) < self.epsilon)

        localmodel = Model.GaussianBinaryRBM(number_visibles=1,
                                             number_hiddens=2,
                                             data=self.gbrbmData[:, 0].reshape(
                                                 2, 1),
                                             initial_weights='AUTO',
                                             initial_visible_bias='AUTO',
                                             initial_hidden_bias='AUTO',
                                             initial_sigma='AUTO',
                                             initial_visible_offsets='AUTO',
                                             initial_hidden_offsets='AUTO')
        localmodel._add_visible_units(num_new_visibles=1,
                                      position=1,
                                      initial_weights='AUTO',
                                      initial_bias='AUTO',
                                      initial_sigmas=1.0,
                                      initial_offsets=0.0,
                                      data=None)
        assert localmodel.bv[0, 0] == numx.mean(self.gbrbmData, axis=0)[0]
        assert localmodel.bv[0, 1] == 0.0
        assert numx.all(localmodel.ov == numx.zeros((1, 2)))
        assert localmodel.bv_base[0, 0] == numx.mean(self.gbrbmData, axis=0)[0]
        assert localmodel.bv_base[0, 1] == 0.0
        assert localmodel.sigma[0, 0] == numx.std(self.gbrbmData, axis=0)[0]
        assert localmodel.sigma[0, 1] == 1.0
        print(' successfully passed!')
        sys.stdout.flush()
Esempio n. 2
0
 def test__remove_visible_units(self):
     print('GaussianBinaryRBM -> Performing remove_visible_units test ...')
     sys.stdout.flush()
     localmodel = Model.GaussianBinaryRBM(number_visibles=1,
                                          number_hiddens=2,
                                          data=self.gbrbmData[:, 0].reshape(
                                              2, 1),
                                          initial_weights='AUTO',
                                          initial_visible_bias='AUTO',
                                          initial_hidden_bias='AUTO',
                                          initial_sigma='AUTO',
                                          initial_visible_offsets='AUTO',
                                          initial_hidden_offsets='AUTO')
     localmodel._remove_hidden_units([0])
     assert localmodel.bh.shape != [1, 1]
     assert localmodel.bv_base.shape != [1, 1]
     print('successfully passed!')
     sys.stdout.flush()
Esempio n. 3
0
 def test__add_hidden_units(self):
     print('GaussianBinaryRBM -> Performing add_hidden_units test ...')
     sys.stdout.flush()
     localmodel = Model.GaussianBinaryRBM(number_visibles=1,
                                          number_hiddens=2,
                                          data=self.gbrbmData[:, 0].reshape(
                                              2, 1),
                                          initial_weights='AUTO',
                                          initial_visible_bias='AUTO',
                                          initial_hidden_bias='AUTO',
                                          initial_sigma='AUTO',
                                          initial_visible_offsets='AUTO',
                                          initial_hidden_offsets='AUTO')
     localmodel._add_hidden_units(num_new_hiddens=1,
                                  position=1,
                                  initial_weights='AUTO',
                                  initial_bias='AUTO',
                                  initial_offsets=0.0)
     assert localmodel.bh[0, 1] == 0.0
     assert numx.all(localmodel.ov == numx.zeros((1, 2)))
     print('successfully passed!')
     sys.stdout.flush()
Esempio n. 4
0
 def test___init__(self):
     sys.stdout.write('GaussianBinaryRBM -> Performing init test ...')
     sys.stdout.flush()
     localmodel = Model.GaussianBinaryRBM(number_visibles=2,
                                          number_hiddens=2,
                                          data=None,
                                          initial_weights='AUTO',
                                          initial_visible_bias='AUTO',
                                          initial_hidden_bias='AUTO',
                                          initial_sigma='AUTO',
                                          initial_visible_offsets='AUTO',
                                          initial_hidden_offsets='AUTO')
     assert numx.all(localmodel.bv == numx.array([[0, 0]]))
     localmodel = Model.GaussianBinaryRBM(
         number_visibles=2,
         number_hiddens=2,
         data=None,
         initial_weights='AUTO',
         initial_visible_bias='INVERSE_SIGMOID',
         initial_hidden_bias='AUTO',
         initial_sigma='AUTO',
         initial_visible_offsets='AUTO',
         initial_hidden_offsets='AUTO')
     assert numx.all(localmodel.bv == numx.array([[0, 0]]))
     localmodel = Model.GaussianBinaryRBM(number_visibles=2,
                                          number_hiddens=2,
                                          data=numx.array([[0.5, 0.1],
                                                           [0.9, 0.4]]),
                                          initial_weights='AUTO',
                                          initial_visible_bias='AUTO',
                                          initial_hidden_bias='AUTO',
                                          initial_sigma='AUTO',
                                          initial_visible_offsets='AUTO',
                                          initial_hidden_offsets='AUTO')
     assert numx.all(localmodel.bv == numx.mean(
         numx.array([[0.5, 0.1], [0.9, 0.4]]), axis=0))
     localmodel = Model.GaussianBinaryRBM(
         number_visibles=2,
         number_hiddens=2,
         data=numx.array([[0.5, 0.1], [0.9, 0.4]]),
         initial_weights='AUTO',
         initial_visible_bias='INVERSE_SIGMOID',
         initial_hidden_bias='AUTO',
         initial_sigma='AUTO',
         initial_visible_offsets='AUTO',
         initial_hidden_offsets='AUTO')
     assert numx.all(localmodel.bv == numx.mean(
         numx.array([[0.5, 0.1], [0.9, 0.4]]), axis=0))
     assert numx.all(localmodel._data_mean == numx.mean(
         numx.array([[0.5, 0.1], [0.9, 0.4]]), axis=0))
     assert numx.all(localmodel._data_std == numx.clip(
         numx.std(numx.array([[0.5, 0.1], [0.9, 0.4]]), axis=0), 0.001,
         numx.finfo(localmodel.dtype).max))
     assert numx.all(localmodel.sigma == numx.clip(
         numx.std(numx.array([[0.5, 0.1], [0.9, 0.4]]), axis=0), 0.001,
         numx.finfo(localmodel.dtype).max))
     assert numx.all(localmodel._data_mean == numx.clip(
         numx.mean(numx.array([[0.5, 0.1], [0.9, 0.4]]), axis=0), 0.001,
         numx.finfo(localmodel.dtype).max))
     print(' successfully passed!')
     sys.stdout.flush()
Esempio n. 5
0
class Test_GaussianBinaryRBM(unittest.TestCase):
    gbrbmData = numx.array([[0.57459607, 0.57689834],
                            [-0.60788602, -0.57941004]])
    gbrbmw = numx.array([[0.12179488, 2.95950177], [
        0.20318085,
        -28.62372894,
    ]])
    gbrbmbv = numx.array([[-19.24399659, -13.26258696]])
    gbrbmbh = numx.array([[-0.11155958, 57.02097584]])
    gbrbm = Model.GaussianBinaryRBM(2, 2, gbrbmData, gbrbmw, gbrbmbv, gbrbmbh,
                                    1.0, 0.0, 0.0)

    gbrbmTruelogZ = 59.6749019726
    gbrbmTrueLL = -1.7328699078
    gbrbmBestLLPossible = -1.732867951

    epsilon = 0.00001

    def test___init__(self):
        sys.stdout.write('GaussianBinaryRBM -> Performing init test ...')
        sys.stdout.flush()
        localmodel = Model.GaussianBinaryRBM(number_visibles=2,
                                             number_hiddens=2,
                                             data=None,
                                             initial_weights='AUTO',
                                             initial_visible_bias='AUTO',
                                             initial_hidden_bias='AUTO',
                                             initial_sigma='AUTO',
                                             initial_visible_offsets='AUTO',
                                             initial_hidden_offsets='AUTO')
        assert numx.all(localmodel.bv == numx.array([[0, 0]]))
        localmodel = Model.GaussianBinaryRBM(
            number_visibles=2,
            number_hiddens=2,
            data=None,
            initial_weights='AUTO',
            initial_visible_bias='INVERSE_SIGMOID',
            initial_hidden_bias='AUTO',
            initial_sigma='AUTO',
            initial_visible_offsets='AUTO',
            initial_hidden_offsets='AUTO')
        assert numx.all(localmodel.bv == numx.array([[0, 0]]))
        localmodel = Model.GaussianBinaryRBM(number_visibles=2,
                                             number_hiddens=2,
                                             data=numx.array([[0.5, 0.1],
                                                              [0.9, 0.4]]),
                                             initial_weights='AUTO',
                                             initial_visible_bias='AUTO',
                                             initial_hidden_bias='AUTO',
                                             initial_sigma='AUTO',
                                             initial_visible_offsets='AUTO',
                                             initial_hidden_offsets='AUTO')
        assert numx.all(localmodel.bv == numx.mean(
            numx.array([[0.5, 0.1], [0.9, 0.4]]), axis=0))
        localmodel = Model.GaussianBinaryRBM(
            number_visibles=2,
            number_hiddens=2,
            data=numx.array([[0.5, 0.1], [0.9, 0.4]]),
            initial_weights='AUTO',
            initial_visible_bias='INVERSE_SIGMOID',
            initial_hidden_bias='AUTO',
            initial_sigma='AUTO',
            initial_visible_offsets='AUTO',
            initial_hidden_offsets='AUTO')
        assert numx.all(localmodel.bv == numx.mean(
            numx.array([[0.5, 0.1], [0.9, 0.4]]), axis=0))
        assert numx.all(localmodel._data_mean == numx.mean(
            numx.array([[0.5, 0.1], [0.9, 0.4]]), axis=0))
        assert numx.all(localmodel._data_std == numx.clip(
            numx.std(numx.array([[0.5, 0.1], [0.9, 0.4]]), axis=0), 0.001,
            numx.finfo(localmodel.dtype).max))
        assert numx.all(localmodel.sigma == numx.clip(
            numx.std(numx.array([[0.5, 0.1], [0.9, 0.4]]), axis=0), 0.001,
            numx.finfo(localmodel.dtype).max))
        assert numx.all(localmodel._data_mean == numx.clip(
            numx.mean(numx.array([[0.5, 0.1], [0.9, 0.4]]), axis=0), 0.001,
            numx.finfo(localmodel.dtype).max))
        print(' successfully passed!')
        sys.stdout.flush()

    def test__add_visible_units(self):
        sys.stdout.write(
            'GaussianBinaryRBM -> Performing add_visible_units test ...')
        sys.stdout.flush()
        localmodel = Model.GaussianBinaryRBM(number_visibles=1,
                                             number_hiddens=2,
                                             data=self.gbrbmData[:, 0].reshape(
                                                 2, 1),
                                             initial_weights='AUTO',
                                             initial_visible_bias='AUTO',
                                             initial_hidden_bias='AUTO',
                                             initial_sigma='AUTO',
                                             initial_visible_offsets='AUTO',
                                             initial_hidden_offsets='AUTO')
        localmodel._add_visible_units(num_new_visibles=1,
                                      position=1,
                                      initial_weights='AUTO',
                                      initial_bias='AUTO',
                                      initial_sigmas='AUTO',
                                      initial_offsets='AUTO',
                                      data=self.gbrbmData[:, 1].reshape(2, 1))
        assert numx.all(localmodel.bv == numx.mean(
            self.gbrbmData, axis=0).reshape(1, self.gbrbmData.shape[1]))
        assert numx.all(localmodel.ov == 0.0)
        assert numx.all(
            numx.abs(localmodel.bv_base - numx.mean(self.gbrbmData, axis=0).
                     reshape(1, self.gbrbmData.shape[1])) < self.epsilon)
        assert numx.all(
            numx.abs(localmodel.sigma - numx.clip(
                numx.std(self.gbrbmData, axis=0).reshape(
                    1, self.gbrbmData.shape[1]), 0.001,
                numx.finfo(localmodel.dtype).max)) < self.epsilon)

        localmodel = Model.GaussianBinaryRBM(number_visibles=1,
                                             number_hiddens=2,
                                             data=self.gbrbmData[:, 0].reshape(
                                                 2, 1),
                                             initial_weights='AUTO',
                                             initial_visible_bias='AUTO',
                                             initial_hidden_bias='AUTO',
                                             initial_sigma='AUTO',
                                             initial_visible_offsets='AUTO',
                                             initial_hidden_offsets='AUTO')
        localmodel._add_visible_units(num_new_visibles=1,
                                      position=1,
                                      initial_weights='AUTO',
                                      initial_bias='AUTO',
                                      initial_sigmas=1.0,
                                      initial_offsets=0.0,
                                      data=None)
        assert localmodel.bv[0, 0] == numx.mean(self.gbrbmData, axis=0)[0]
        assert localmodel.bv[0, 1] == 0.0
        assert numx.all(localmodel.ov == numx.zeros((1, 2)))
        assert localmodel.bv_base[0, 0] == numx.mean(self.gbrbmData, axis=0)[0]
        assert localmodel.bv_base[0, 1] == 0.0
        assert localmodel.sigma[0, 0] == numx.std(self.gbrbmData, axis=0)[0]
        assert localmodel.sigma[0, 1] == 1.0
        print(' successfully passed!')
        sys.stdout.flush()

    def test__add_hidden_units(self):
        sys.stdout.write(
            'GaussianBinaryRBM -> Performing add_hidden_units test ...')
        sys.stdout.flush()
        localmodel = Model.GaussianBinaryRBM(number_visibles=1,
                                             number_hiddens=2,
                                             data=self.gbrbmData[:, 0].reshape(
                                                 2, 1),
                                             initial_weights='AUTO',
                                             initial_visible_bias='AUTO',
                                             initial_hidden_bias='AUTO',
                                             initial_sigma='AUTO',
                                             initial_visible_offsets='AUTO',
                                             initial_hidden_offsets='AUTO')
        localmodel._add_hidden_units(num_new_hiddens=1,
                                     position=1,
                                     initial_weights='AUTO',
                                     initial_bias='AUTO',
                                     initial_offsets=0.0)
        assert localmodel.bh[0, 1] == 0.0
        assert numx.all(localmodel.ov == numx.zeros((1, 2)))
        print(' successfully passed!')
        sys.stdout.flush()

    def test__remove_visible_units(self):
        sys.stdout.write(
            'GaussianBinaryRBM -> Performing remove_visible_units test ...')
        sys.stdout.flush()
        localmodel = Model.GaussianBinaryRBM(number_visibles=1,
                                             number_hiddens=2,
                                             data=self.gbrbmData[:, 0].reshape(
                                                 2, 1),
                                             initial_weights='AUTO',
                                             initial_visible_bias='AUTO',
                                             initial_hidden_bias='AUTO',
                                             initial_sigma='AUTO',
                                             initial_visible_offsets='AUTO',
                                             initial_hidden_offsets='AUTO')
        localmodel._remove_hidden_units([0])
        assert localmodel.bh.shape != [1, 1]
        assert localmodel.bv_base.shape != [1, 1]
        print(' successfully passed!')
        sys.stdout.flush()

    def test__calculate_weight_gradient(self):
        sys.stdout.write(
            'GaussianBinaryRBM -> Performing calculate_weight_gradient test ...'
        )
        sys.stdout.flush()
        deltaW = self.gbrbm._calculate_weight_gradient(
            numx.array([[0.98, -0.56], [-0.3, 0.8]]),
            numx.array([[0, 1], [1, 1]]))
        target = numx.array([[-0.3, 0.68], [0.8, 0.24]])
        assert numx.all(numx.abs(target - deltaW < self.epsilon))
        print(' successfully passed!')
        sys.stdout.flush()

    def test__calculate_visible_bias_gradient(self):
        sys.stdout.write(
            'GaussianBinaryRBM -> Performing calculate_visible_bias_gradient test ...'
        )
        sys.stdout.flush()
        deltaB = self.gbrbm._calculate_visible_bias_gradient(
            numx.array([[0.98, -0.56], [-0.3, 0.8]]))
        target = numx.array([[39.16799318, 26.76517392]])
        assert numx.all(numx.abs(target - deltaB < self.epsilon))
        print(' successfully passed!')
        sys.stdout.flush()

    def test_sample_v(self):
        sys.stdout.write('GaussianBinaryRBM -> Performing sample_v test ...')
        sys.stdout.flush()
        numx.random.seed(42)
        samples = self.gbrbm.sample_v(numx.zeros((10000, 2)))
        assert numx.abs(numx.mean(samples)) < 0.01
        assert numx.abs(1.0 - numx.std(samples)) < 0.01
        print(' successfully passed!')
        sys.stdout.flush()

    def test_probability_v_given_h(self):
        sys.stdout.write(
            'GaussianBinaryRBM -> Performing probability_v_given_h test ...')
        sys.stdout.flush()
        probs = self.gbrbm.probability_v_given_h(
            numx.array([[0, 0], [1, 0], [0, 1], [1, 1]]))
        target = numx.array([[-19.24399659, -13.26258696],
                             [-19.12220171, -13.05940611],
                             [-16.28449482, -41.8863159],
                             [-16.16269994, -41.68313505]])
        assert numx.all(numx.abs(target - probs < self.epsilon))
        print(' successfully passed!')
        sys.stdout.flush()

    def test_probability_h_given_v(self):
        sys.stdout.write(
            'GaussianBinaryRBM -> Performing probability_h_given_v test ...')
        sys.stdout.flush()
        probs = self.gbrbm.probability_h_given_v(
            numx.array([[-19.24399659, -13.26258696],
                        [-19.12220171, -13.05940611],
                        [-16.28449482, -41.8863159],
                        [-16.16269994, -41.68313505]]))
        target = numx.array([[5.76548674e-03, 1], [6.09624677e-03, 1],
                             [2.47805937e-05, 1], [2.62109132e-05, 1]])
        assert numx.all(numx.abs(target - probs < self.epsilon))
        print(' successfully passed!')
        sys.stdout.flush()

    def test_energy(self):
        sys.stdout.write('GaussianBinaryRBM -> Performing energy test ...')
        sys.stdout.flush()
        energy = self.gbrbm.energy(numx.array([[0.98, -0.56], [-0.3, 0.8]]),
                                   numx.array([[0, 1], [1, 1]]))
        target = numx.array([[209.23230099], [245.06709061]])
        assert numx.all(numx.abs(target - energy < self.epsilon))
        print(' successfully passed!')
        sys.stdout.flush()

    def test_unnormalized_log_probability_v(self):
        sys.stdout.write(
            'GaussianBinaryRBM -> Performing unnormalized_log_probability_v test ...'
        )
        sys.stdout.flush()
        probs = self.gbrbm.unnormalized_log_probability_v(
            numx.array([[0.98, -0.56], [-0.3, 0.8]]))
        target = numx.array([[-208.59074139], [-244.38114066]])
        assert numx.all(numx.abs(target - probs) < self.epsilon)
        print(' successfully passed!')
        sys.stdout.flush()

    def test_unnormalized_log_probability_h(self):
        sys.stdout.write(
            'GaussianBinaryRBM -> Performing unnormalized_log_probability_h test ...'
        )
        sys.stdout.flush()
        probs = self.gbrbm.unnormalized_log_probability_h(
            numx.array([[0, 1], [1, 1]]))
        target = numx.array([[795.5691597], [784.99179299]])
        assert numx.all(numx.abs(target - probs) < self.epsilon)
        print(' successfully passed!')
        sys.stdout.flush()

    def test__base_log_partition(self):
        sys.stdout.write(
            'GaussianBinaryRBM -> Performing base_log_partition test ...')
        sys.stdout.flush()
        base = self.gbrbm._base_log_partition()
        target = 58.4243290171
        assert numx.all(numx.abs(target - base) < self.epsilon)
        print(' successfully passed!')
        sys.stdout.flush()