コード例 #1
0
    def test_storage(self):
        path = "association_test_2"
        store.create_path(path)
        self.assertTrue(os.path.isdir('/'.join([store.data_dir, path])))

        path = "association_test_3"
        store.move_to(path)
        self.assertTrue(os.getcwd() == '/'.join([store.data_dir, path]))

        store.move_to_root()
        self.assertTrue(os.getcwd() == store.root_dir)

        store.move_to(path)
        rbm = RBM.RBM()
        store.store_object(rbm)

        rbm2 = store.retrieve_object(str(rbm))

        self.assertTrue(str(rbm) == str(rbm2))

        dbn = DBN.DBN()
        store.store_object(dbn)

        dbn2 = store.retrieve_object(str(dbn))
        self.assertTrue(str(dbn) == str(dbn2))
        store.move_to_root()
コード例 #2
0
    def test_set_up(self):
        tr = rbm_config.TrainParam(learning_rate=0.05,
                                   momentum_type=rbm_config.CLASSICAL,
                                   momentum=0.5,
                                   weight_decay=0,
                                   sparsity_constraint=False,
                                   sparsity_target=0.1**9,
                                   sparsity_cost=10**8,
                                   sparsity_decay=0.9,
                                   epochs=5)

        config = rbm_config.RBMConfig()
        config.v_n = 3
        config.h_n = 2
        config.v_unit = rbm_units.GaussianVisibleUnit
        config.progress_logger = rbm_logger.ProgressLogger()
        config.train_params = tr
        np_rand = np.random.RandomState(123)

        # Weights
        W = np_rand.uniform(low=-1. / 10, high=-1. / 10,
                            size=(3, 2)).astype(np.float32)
        vb = np.array([-0.1, 0, 0.1], dtype=np.float32)
        hb = np.array([0.01, -0.01], dtype=np.float32)
        Wt = theano.shared(W, name='W')
        vbt = theano.shared(vb, name='vbias')
        hbt = theano.shared(hb, name='hbias')
        g_rbm = rbm.RBM(config, W=Wt, h_bias=hbt, v_bias=vbt)
        self.assertTrue(g_rbm)
        self.assertTrue(isinstance(g_rbm.v_unit,
                                   rbm_units.GaussianVisibleUnit))
        self.assertTrue(isinstance(g_rbm.h_unit, rbm_units.RBMUnit))
        self.assertTrue(
            np.count_nonzero(g_rbm.W.get_value(borrow=True) - W) == 0)
        self.assertTrue(
            np.count_nonzero(g_rbm.v_bias.get_value(borrow=True) - vb) == 0)
        self.assertTrue(
            np.count_nonzero(g_rbm.h_bias.get_value(borrow=True) - hb) == 0)

        x = sklearn.preprocessing.scale(
            np.array([[200.0, 188., 7.], [150.0, 128., 0.], [250.0, 98., 3.]],
                     dtype=theano.config.floatX))
        v = theano.shared(x)
        _, _, h = g_rbm.sample_h_given_v(v)
        _, _, vs = g_rbm.sample_v_given_h(h)
        _, _, hs = g_rbm.sample_h_given_v(vs)
        dw = T.dot(v.T, h) - T.dot(vs.T, hs)
        dv = T.sum(v - vs, axis=0)
        dh = T.sum(h - hs, axis=0)
        gr = g_rbm.get_partial_derivatives(v, None)['gradients']
        gdw, gdv, gdh = gr[0], gr[1], gr[2]
        print gdw, gdv, gdh
        compute_derivative = theano.function([], [dw, dv, dh, gdw, gdv, gdh])
        for i in xrange(20):
            a, b, c, d, e, f = compute_derivative()
            # print a, b, c
            print d, e, f
コード例 #3
0
    def test_digits(self):
        tr = rbm_config.TrainParam(learning_rate=0.05,
                                   momentum_type=rbm_config.CLASSICAL,
                                   momentum=0.5,
                                   weight_decay=0,
                                   sparsity_constraint=False,
                                   sparsity_target=0.1**9,
                                   sparsity_cost=10**8,
                                   sparsity_decay=0.9,
                                   epochs=5)

        config = rbm_config.RBMConfig()
        config.v_n = 784
        config.h_n = 100
        config.v_unit = rbm_units.GaussianVisibleUnit
        config.h_unit = rbm_units.ReLUnit
        config.progress_logger = rbm_logger.ProgressLogger()
        config.train_params = tr
        np_rand = np.random.RandomState(123)

        # Weights
        W = np_rand.uniform(low=-1. / 10, high=1. / 10,
                            size=(784, 100)).astype(np.float32)
        vb = np.zeros(784, dtype=np.float32)
        hb = np.array(100, dtype=np.float32)
        Wt = theano.shared(W, name='W')
        vbt = theano.shared(vb, name='vbias')
        hbt = theano.shared(hb, name='hbias')
        g_rbm = rbm.RBM(config, W=Wt, h_bias=hbt, v_bias=vbt)
        self.assertTrue(g_rbm)
        self.assertTrue(isinstance(g_rbm.v_unit,
                                   rbm_units.GaussianVisibleUnit))
        self.assertTrue(isinstance(g_rbm.h_unit, rbm_units.RBMUnit))
        self.assertTrue(
            np.count_nonzero(g_rbm.W.get_value(borrow=True) - W) == 0)
        self.assertTrue(
            np.count_nonzero(g_rbm.v_bias.get_value(borrow=True) - vb) == 0)
        self.assertTrue(
            np.count_nonzero(g_rbm.h_bias.get_value(borrow=True) - hb) == 0)

        tr, vl, te = mnist_loader.load_digits(n=[5, 10, 10],
                                              pre={'scale': True})
        v = tr[0]

        # print 'inputs:'
        # table = ss.itemfreq(v.get_value(borrow=True))
        # x = [pt[0] for pt in table]
        # y = [pt[1] for pt in table]
        # plt.plot(x, y)
        # plt.show()

        # v = theano.shared(x)
        _, _, h = g_rbm.sample_h_given_v(v)
        _, _, vs = g_rbm.sample_v_given_h(h)
        _, _, hs = g_rbm.sample_h_given_v(vs)
        dw = T.dot(v.T, h) - T.dot(vs.T, hs)
        dv = T.sum(v - vs, axis=0)
        dh = T.sum(h - hs, axis=0)
        gr = g_rbm.get_partial_derivatives(v, None)['gradients']
        gdw, gdv, gdh = gr[0], gr[1], gr[2]
        print gdw, gdv, gdh
        compute_derivative = theano.function([], [dw, dv, dh, gdw, gdv, gdh])
        for i in xrange(1):
            a, b, c, d, e, f = compute_derivative()
            # print a, b, c
            print 'unfold'
            print a[0], b[1:5], c[1:5]

            print 'rbm'
            print d[0], e[1:5], f[1:5]
コード例 #4
0
    def test_kanades_grrbm(self):
        nvis = 625
        nhid = 1000
        train_n = 10000
        batch_n = 20

        tr = rbm_config.TrainParam(learning_rate=0.05,
                                   momentum_type=rbm_config.CLASSICAL,
                                   momentum=0.5,
                                   weight_decay=0,
                                   sparsity_constraint=False,
                                   sparsity_target=0.1**9,
                                   sparsity_cost=10**8,
                                   sparsity_decay=0.9,
                                   epochs=5,
                                   batch_size=batch_n)

        config = rbm_config.RBMConfig()
        config.v_n = nvis
        config.h_n = nhid
        config.v_unit = rbm_units.GaussianVisibleUnit
        config.h_unit = rbm_units.ReLUnit
        config.progress_logger = rbm_logger.ProgressLogger()
        config.train_params = tr
        np_rand = np.random.RandomState(123)

        # Weights
        W = np_rand.normal(0, 0.01, size=(nvis, nhid)).astype(np.float32)
        vb = np.zeros(nvis, dtype=np.float32)
        hb = np.zeros(nhid, dtype=np.float32)
        Wt = theano.shared(W, name='W')
        vbt = theano.shared(vb, name='vbias')
        hbt = theano.shared(hb, name='hbias')
        g_rbm = rbm.RBM(config, W=Wt, h_bias=hbt, v_bias=vbt)
        self.assertTrue(g_rbm)
        self.assertTrue(isinstance(g_rbm.v_unit,
                                   rbm_units.GaussianVisibleUnit))
        self.assertTrue(isinstance(g_rbm.h_unit, rbm_units.RBMUnit))
        self.assertTrue(
            np.count_nonzero(g_rbm.W.get_value(borrow=True) - W) == 0)
        self.assertTrue(
            np.count_nonzero(g_rbm.v_bias.get_value(borrow=True) - vb) == 0)
        self.assertTrue(
            np.count_nonzero(g_rbm.h_bias.get_value(borrow=True) - hb) == 0)

        tr, vl, te = k_loader.load_kanade(n=batch_n, pre={'scale': True})
        v = tr[0]

        vv = v.get_value(borrow=True).ravel()
        table = ss.itemfreq(vv)
        print table
        x = [pt[0] for pt in table]
        y = [pt[1] for pt in table]
        plt.plot(x, y)
        plt.show()

        # v = theano.shared(x)
        h = g_rbm.h_unit.activate(g_rbm.h_unit.scale(T.dot(v, W) + hb))

        _, _, h = g_rbm.sample_h_given_v(v)
        _, _, vs = g_rbm.sample_v_given_h(h)
        _, _, hs = g_rbm.sample_h_given_v(vs)

        dw = (T.dot(v.T, h) - T.dot(vs.T, hs)) / batch_n
        dv = T.mean(v - vs, axis=0)
        dh = T.mean(h - hs, axis=0)
        gr = g_rbm.get_partial_derivatives(v, None)['gradients']
        gdw, gdv, gdh = gr[0], gr[1], gr[2]
        print gdw, gdv, gdh
        compute_derivative = theano.function([], [dw, dv, dh, gdw, gdv, gdh])
        for i in xrange(1):
            a, b, c, d, e, f = compute_derivative()
            # print a, b, c
            print 'unfold'
            print a[0][0:5], b[1:5], c[1:5]

            print 'rbm'
            print d[0][0:5], e[1:5], f[1:5]