Esempio n. 1
0
    def __init__(self, rbm_layers, do_pretrain=True, rbm_num_epochs=list([10]), rbm_batch_size=list([10]), model_name='dbn',
                 rbm_learning_rate=list([0.01]), rbm_names=list(['']), rbm_gibbs_k=list([1]), rbm_gauss_visible=False,
                 rbm_stddev=0.1, finetune_learning_rate=0.01, momentum=0.9, finetune_num_epochs=10,
                 finetune_batch_size=10, finetune_dropout=1, finetune_opt='gradient_descent', verbose=1,
                 finetune_act_func='relu', finetune_loss_func='mean_squared', main_dir='dbn/', dataset='mnist'):

        """
        :param rbm_layers: list containing the hidden units for each layer
        :param do_pretrain: whether to do unsupervised pretraining of the network
        :param rbm_num_epochs: number of epochs to train each rbm
        :param rbm_batch_size: batch size each rbm
        :param rbm_learning_rate: learning rate each rbm
        :param rbm_names: model name for each rbm
        :param rbm_gibbs_k: number of gibbs sampling steps for each rbm
        :param rbm_gauss_visible: whether the input layer should have gaussian units
        :param rbm_stddev: standard deviation for the gaussian layer
        :param finetune_dropout: dropout parameter
        :param verbose: Level of verbosity. 0 - silent, 1 - print accuracy. int, default 0
        """
        model.Model.__init__(self, model_name, main_dir)

        self._initialize_training_parameters(loss_func=finetune_loss_func, learning_rate=finetune_learning_rate,
                                             num_epochs=finetune_num_epochs, batch_size=finetune_batch_size,
                                             dropout=finetune_dropout, dataset=dataset, opt=finetune_opt,
                                             momentum=momentum)

        self.do_pretrain = do_pretrain
        self.layers = rbm_layers
        self.finetune_act_func = finetune_act_func
        self.verbose = verbose

        self.W_pretrain = None
        self.bh_pretrain = None
        self.bv_pretrain = None

        self.W_vars = None
        self.b_vars = None

        self.rbms = []

        for l, layer in enumerate(rbm_layers):

            if l == 0 and rbm_gauss_visible:

                # Gaussian visible units

                self.rbms.append(rbm.RBM(
                    visible_unit_type='gauss', stddev=rbm_stddev,
                    model_name=rbm_names[l] + str(l), num_hidden=rbm_layers[l],
                    main_dir=self.main_dir, learning_rate=rbm_learning_rate[l], gibbs_sampling_steps=rbm_gibbs_k[l],
                    verbose=self.verbose, num_epochs=rbm_num_epochs[l], batch_size=rbm_batch_size[l]))

            else:

                # Binary RBMs

                self.rbms.append(rbm.RBM(
                    model_name=rbm_names[l] + str(l), num_hidden=rbm_layers[l],
                    main_dir=self.main_dir, learning_rate=rbm_learning_rate[l], gibbs_sampling_steps=rbm_gibbs_k[l],
                    verbose=self.verbose, num_epochs=rbm_num_epochs[l], batch_size=rbm_batch_size[l]))
Esempio n. 2
0
    def __init__(self,
                 layers,
                 do_pretrain=True,
                 rbm_num_epochs=list([10]),
                 rbm_batch_size=list([10]),
                 rbm_learning_rate=list([0.01]),
                 rbm_names=list(['']),
                 rbm_gibbs_k=list([1]),
                 gauss_visible=False,
                 stddev=0.1,
                 learning_rate=0.01,
                 momentum=0.7,
                 num_epochs=10,
                 batch_size=10,
                 dropout=1,
                 opt='gradient_descent',
                 loss_func='mean_squared',
                 model_name='deepae',
                 main_dir='deepae/',
                 dataset='mnist',
                 verbose=1):
        """
        :param layers: list containing the hidden units for each layer
        :param do_pretrain: whether to do unsupervised pretraining of the network
        :param rbm_num_epochs: number of epochs to train each rbm
        :param rbm_batch_size: batch size each rbm
        :param rbm_learning_rate: learning rate each rbm
        :param rbm_names: model name for each rbm
        :param rbm_gibbs_k: number of gibbs sampling steps for each rbm
        :param gauss_visible: whether the input layer should have gaussian units
        :param stddev: standard deviation for the gaussian layer
        :param dropout: dropout parameter
        :param verbose: Level of verbosity. 0 - silent, 1 - print accuracy. int, default 0
        """
        model.Model.__init__(self, model_name, main_dir)

        self._initialize_training_parameters(loss_func, learning_rate,
                                             num_epochs, batch_size, dataset,
                                             opt, momentum)

        self.layers = layers
        self.n_layers = len(layers)

        # RBM parameters
        self.do_pretrain = do_pretrain
        self.rbm_num_epochs = rbm_num_epochs
        self.rbm_batch_size = rbm_batch_size
        self.rbm_learning_rate = rbm_learning_rate
        self.rbm_names = rbm_names
        self.rbm_gibbs_k = rbm_gibbs_k

        # Stacked RBM parameters
        self.gauss_visible = gauss_visible
        self.stddev = stddev
        self.dropout = dropout
        self.verbose = verbose

        self.W_pretrain = None
        self.bh_pretrain = None
        self.bv_pretrain = None

        self.W_vars = None
        self.W_vars_t = None
        self.bh_vars = None
        self.bv_vars = None

        self.encode = None

        if self.do_pretrain:

            self.rbms = []

            for l in range(self.n_layers):

                if l == 0 and self.gauss_visible:

                    # Gaussian visible units

                    self.rbms.append(
                        rbm.RBM(visible_unit_type='gauss',
                                stddev=self.stddev,
                                model_name=self.rbm_names[l] + str(l),
                                num_hidden=self.layers[l],
                                main_dir=self.main_dir,
                                learning_rate=self.rbm_learning_rate[l],
                                verbose=self.verbose,
                                num_epochs=self.rbm_num_epochs[l],
                                batch_size=self.rbm_batch_size[l]))

                else:

                    # Binary RBMs

                    self.rbms.append(
                        rbm.RBM(model_name=self.rbm_names[l] + str(l),
                                num_hidden=self.layers[l],
                                main_dir=self.main_dir,
                                learning_rate=self.rbm_learning_rate[l],
                                verbose=self.verbose,
                                num_epochs=self.rbm_num_epochs[l],
                                batch_size=self.rbm_batch_size[l]))
Esempio n. 3
0
            else:
                return None

        trX = load_from_np(FLAGS.train_dataset)
        vlX = load_from_np(FLAGS.valid_dataset)
        teX = load_from_np(FLAGS.test_dataset)

    else:
        trX, vlX, teX, width, height = None, None, None, None, None

    # Create the object
    r = rbm.RBM(num_hidden=FLAGS.num_hidden,
                main_dir=FLAGS.main_dir,
                visible_unit_type=FLAGS.visible_unit_type,
                learning_rate=FLAGS.learning_rate,
                num_epochs=FLAGS.num_epochs,
                batch_size=FLAGS.batch_size,
                stddev=FLAGS.stddev,
                verbose=FLAGS.verbose,
                gibbs_sampling_steps=FLAGS.gibbs_sampling_steps,
                model_name=FLAGS.model_name)

    # Fit the model
    print('Start training...')
    r.build_model(trX.shape[1])
    r.fit(trX, teX, restore_previous_model=FLAGS.restore_previous_model)

    # Encode the training data and store it
    if FLAGS.encode_train:
        print('Transforming training data...')
        r.transform(trX, name='train', save=FLAGS.encode_train)