Esempio n. 1
0
    def test_ais_with_semirbm_dbn(self):
        dbn = DBN(RBM(5, 5))
        dbn.add_layer(SemiRBM(5, 5))

        ais = Estimator(dbn)
        ais.estimate_log_partition_function(100,
                                            np.arange(0, 1, 1E-3),
                                            layer=0)
        ais.estimate_log_partition_function(10, np.arange(0, 1, 1E-3), layer=1)

        dbn[0]._brf_logz = utils.logsumexp(dbn[0]._ulogprob_vis(
            utils.binary_numbers(dbn[0].X.shape[0])))
        dbn[1]._brf_logz = utils.logsumexp(dbn[1]._ulogprob_vis(
            utils.binary_numbers(dbn[1].X.shape[0])))

        samples = np.concatenate(
            [dbn.sample(25, 100, 20),
             np.matrix(np.random.rand(5, 25) > 0.5)], 1)

        Y = utils.binary_numbers(dbn[0].Y.shape[0])
        X = utils.binary_numbers(dbn[0].X.shape[0])

        logRy = dbn[1]._ulogprob_vis(Y)
        logQy = utils.logsumexp(dbn[0]._ulogprob(X, Y, all_pairs=True), 0)
        log_sum = utils.logsumexp(
            dbn[0]._clogprob_hid_vis(samples, Y, all_pairs=True) - logQy +
            logRy, 1)

        logPx = log_sum + dbn[0]._ulogprob_vis(samples) - dbn[1]._brf_logz
        logPx_ = ais.estimate_log_probability(samples)[0]

        self.assertTrue(np.abs(logPx_.mean() - logPx.mean()) < 0.1)
	def test_ais_with_dbn_sanity_check(self):
		dbn = DBN(RBM(5, 20))
		dbn.add_layer(RBM(20, 5))
		dbn.add_layer(RBM(5, 20))

		dbn[0].W = np.matrix(np.random.randn(5, 20))
		dbn[0].b = np.matrix(np.random.rand(5, 1) - 0.5)
		dbn[0].c = np.matrix(np.random.rand(20, 1) - 0.5)

		dbn[1].W = dbn[0].W.T
		dbn[1].b = dbn[0].c
		dbn[1].c = dbn[0].b

		dbn[2].W = dbn[0].W
		dbn[2].b = dbn[0].b
		dbn[2].c = dbn[0].c

		samples = dbn.sample(100)

		ais = Estimator(dbn[0])

		rbm_logz = ais.estimate_log_partition_function(100, np.sqrt(np.arange(0, 1, 0.001)))
		rbm_probs = ais.estimate_log_probability(samples)

		ais = Estimator(dbn)

		dbn_logz = ais.estimate_log_partition_function(100, np.sqrt(np.arange(0, 1, 0.001)))
		dbn_probs = ais.estimate_log_probability(samples)

		self.assertTrue(abs(dbn_logz - rbm_logz) / rbm_logz < 0.02)
		self.assertTrue((np.exp(dbn_probs) - np.exp(rbm_probs)).mean() < 0.02)
Esempio n. 3
0
    def test_ais_with_dbn_sanity_check(self):
        dbn = DBN(RBM(5, 20))
        dbn.add_layer(RBM(20, 5))
        dbn.add_layer(RBM(5, 20))

        dbn[0].W = np.matrix(np.random.randn(5, 20))
        dbn[0].b = np.matrix(np.random.rand(5, 1) - 0.5)
        dbn[0].c = np.matrix(np.random.rand(20, 1) - 0.5)

        dbn[1].W = dbn[0].W.T
        dbn[1].b = dbn[0].c
        dbn[1].c = dbn[0].b

        dbn[2].W = dbn[0].W
        dbn[2].b = dbn[0].b
        dbn[2].c = dbn[0].c

        samples = dbn.sample(100)

        ais = Estimator(dbn[0])

        rbm_logz = ais.estimate_log_partition_function(
            100, np.sqrt(np.arange(0, 1, 0.001)))
        rbm_probs = ais.estimate_log_probability(samples)

        ais = Estimator(dbn)

        dbn_logz = ais.estimate_log_partition_function(
            100, np.sqrt(np.arange(0, 1, 0.001)))
        dbn_probs = ais.estimate_log_probability(samples)

        self.assertTrue(abs(dbn_logz - rbm_logz) / rbm_logz < 0.02)
        self.assertTrue((np.exp(dbn_probs) - np.exp(rbm_probs)).mean() < 0.02)
	def test_ais_with_semirbm_dbn(self):
		dbn = DBN(RBM(5, 5))
		dbn.add_layer(SemiRBM(5, 5))
		
		ais = Estimator(dbn)
		ais.estimate_log_partition_function(100, np.arange(0, 1, 1E-3), layer=0)
		ais.estimate_log_partition_function(10, np.arange(0, 1, 1E-3), layer=1)

		dbn[0]._brf_logz = utils.logsumexp(dbn[0]._ulogprob_vis(utils.binary_numbers(dbn[0].X.shape[0])))
		dbn[1]._brf_logz = utils.logsumexp(dbn[1]._ulogprob_vis(utils.binary_numbers(dbn[1].X.shape[0])))

		samples = np.concatenate([dbn.sample(25, 100, 20), np.matrix(np.random.rand(5, 25) > 0.5)], 1)

		Y = utils.binary_numbers(dbn[0].Y.shape[0])
		X = utils.binary_numbers(dbn[0].X.shape[0])

		logRy = dbn[1]._ulogprob_vis(Y)
		logQy = utils.logsumexp(dbn[0]._ulogprob(X, Y, all_pairs=True), 0)
		log_sum = utils.logsumexp(dbn[0]._clogprob_hid_vis(samples, Y, all_pairs=True) - logQy + logRy, 1)

		logPx = log_sum + dbn[0]._ulogprob_vis(samples) - dbn[1]._brf_logz
		logPx_ = ais.estimate_log_probability(samples)[0]

		self.assertTrue(np.abs(logPx_.mean() - logPx.mean()) < 0.1)