Пример #1
0
def load_rbm1(args):
    if os.path.isdir(args.rbm1_dirpath):
        print("\nLoading RBM #1 ...\n\n")
        rbm1 = BernoulliRBM.load_model(args.rbm1_dirpath)
        return rbm1
    else:
        print("model could not be found!")
Пример #2
0
def make_rbm2(Q, args):
    if os.path.isdir(args.rbm2_dirpath):
        print("\nLoading RBM #2 ...\n\n")
        rbm2 = BernoulliRBM.load_model(args.rbm2_dirpath)
    else:
        print("\nTraining RBM #2 ...\n\n")

        epochs = args.epochs[1]
        n_every = args.increase_n_gibbs_steps_every

        n_gibbs_steps = np.arange(args.n_gibbs_steps[1],
                                  args.n_gibbs_steps[1] + epochs / n_every)
        learning_rate = args.lr[1] / np.arange(1, 1 + epochs / n_every)
        n_gibbs_steps = np.repeat(n_gibbs_steps, n_every)
        learning_rate = np.repeat(learning_rate, n_every)

        rbm2 = BernoulliRBM(n_visible=args.n_hiddens[0],
                            n_hidden=args.n_hiddens[1],
                            W_init=0.005,
                            vb_init=0.,
                            hb_init=0.,
                            n_gibbs_steps=n_gibbs_steps,
                            learning_rate=learning_rate,
                            momentum=[0.5] * 5 + [0.9],
                            max_epoch=max(args.epochs[1], n_every),
                            batch_size=args.batch_size[1],
                            l2=args.l2[1],
                            sample_h_states=True,
                            sample_v_states=True,
                            sparsity_cost=0.,
                            dbm_last=True,  # !!!
                            metrics_config=dict(
                                msre=True,
                                pll=True,
                                train_metrics_every_iter=500,
                            ),
                            verbose=True,
                            display_filters=0,
                            display_hidden_activations=24,
                            random_seed=args.random_seed[1],
                            dtype='float32',
                            tf_saver_params=dict(max_to_keep=1),
                            model_path=args.rbm2_dirpath)
        rbm2.fit(Q)
    return rbm2
Пример #3
0
def init_rbm(args):  # initializes standard RBM
    #     if os.path.isdir(args.model_dirpath):
    #         print ("\nLoading model ...\n\n")
    #         rbm = BernoulliRBM.load_model(args.model_dirpath)
    #     else:
    #     print ("\nTraining model ...\n\n")
    rbm = BernoulliRBM(
        n_visible=args.n_visible,
        n_hidden=args.n_hidden,
        W_init=args.w_init,
        vb_init=args.vb_init,
        hb_init=args.hb_init,
        prune=args.prune,
        freeze_weights=args.freeze_weights,
        filter_shape=args.filter_shape,
        n_gibbs_steps=args.n_gibbs_steps,
        learning_rate=args.lr,
        #                        momentum=np.geomspace(0.5, 0.9, 8),
        momentum=args.momentum,
        max_epoch=args.epochs,
        batch_size=args.batch_size,
        l2=args.l2,
        sample_v_states=args.sample_v_states,
        sample_h_states=True,
        dropout=args.dropout,
        sparsity_target=args.sparsity_target,
        sparsity_cost=args.sparsity_cost,
        sparsity_damping=args.sparsity_damping,
        metrics_config=dict(
            msre=False,
            pll=False,
            feg=False,
            train_metrics_every_iter=10,
            val_metrics_every_epoch=1,
            feg_every_epoch=4,
            n_batches_for_feg=10,
        ),
        verbose=True,
        random_seed=args.random_seed,
        dtype=args.dtype,
        tf_saver_params=dict(max_to_keep=1),
        model_path=args.model_dirpath)
    rbm.init()
    return rbm
Пример #4
0
def make_rbm1(X, args):
    if os.path.isdir(args.rbm1_dirpath):
        print("\nLoading RBM #1 ...\n\n")
        rbm1 = BernoulliRBM.load_model(args.rbm1_dirpath)
    else:
        print("\nTraining RBM #1 ...\n\n")
        rbm1 = BernoulliRBM(n_visible=784,
                            n_hidden=args.n_hiddens[0],
                            W_init=0.001,
                            vb_init=0.,
                            hb_init=0.,
                            n_gibbs_steps=args.n_gibbs_steps[0],
                            learning_rate=args.lr[0],
                            momentum=[0.5] * 5 + [0.9],
                            max_epoch=args.epochs[0],
                            batch_size=args.batch_size[0],
                            l2=args.l2[0],
                            sample_h_states=True,
                            sample_v_states=True,
                            sparsity_cost=0.,
                            dbm_first=True,  # !!!
                            metrics_config=dict(
                                msre=True,
                                pll=True,
                                train_metrics_every_iter=500,
                            ),
                            verbose=True,
                            display_filters=30,
                            display_hidden_activations=24,
                            v_shape=(28, 28),
                            random_seed=args.random_seed[0],
                            dtype='float32',
                            tf_saver_params=dict(max_to_keep=1),
                            model_path=args.rbm1_dirpath)
        rbm1.fit(X)
    return rbm1
Пример #5
0
def init_rbm2(args):
    if os.path.isdir(args.rbm2_dirpath):
        print("\nLoading RBM #2 ...\n\n")
        rbm2 = BernoulliRBM.load_model(args.rbm2_dirpath)
    else:
        print("\nInitializing RBM #2 ...\n\n")
        epochs = args.epochs[1]
        n_every = args.increase_n_gibbs_steps_every

        n_gibbs_steps = np.arange(args.n_gibbs_steps[1],
                                  args.n_gibbs_steps[1] + epochs / n_every)
        learning_rate = args.lr[1] / np.arange(1, 1 + epochs / n_every)
        n_gibbs_steps = np.repeat(n_gibbs_steps, n_every)
        learning_rate = np.repeat(learning_rate, n_every)

        rbm2 = BernoulliRBM(
            n_visible=args.n_hidden[0],
            n_hidden=args.n_hidden[1],
            W_init=args.w_init[1],
            vb_init=args.vb_init[1],
            hb_init=args.hb_init[1],
            n_gibbs_steps=n_gibbs_steps,
            learning_rate=learning_rate,
            momentum=args.momentum,
            max_epoch=max(args.epochs[1], n_every),
            batch_size=args.batch_size[1],
            l2=args.l2[1],
            sample_h_states=True,
            sample_v_states=True,
            sparsity_cost=0.,
            dbm_last=True,  # !!!
            metrics_config=dict(
                msre=True,
                pll=True,
                train_metrics_every_iter=500,
            ),
            verbose=True,
            #display_filters=0,
            #display_hidden_activations=24,
            random_seed=args.random_seed[1],
            v_shape=(20, 20),  # what's the v_shape in this case?
            freeze_weights=args.freeze_weights,
            prune=args.prune,
            #filter_shape =  (10,10), #,args.filter_shape[1],
            dtype='float32',
            tf_saver_params=dict(max_to_keep=1),
            model_path=args.rbm2_dirpath)
        #run on cpu
        config = tf.ConfigProto(device_count={'GPU': 0})
        rbm2._tf_session_config = config
        rbm2.init()
    return rbm2
Пример #6
0
def make_rbm1(X, args):
    if os.path.isdir(args.rbm1_dirpath):
        print("\nLoading RBM #1 ...\n\n")
        rbm1 = BernoulliRBM.load_model(args.rbm1_dirpath)
    else:
        print("\nTraining RBM #1 ...\n\n")

        if not hasattr(args, 'double_rf'):
            args.double_rf = False

        rbm1 = BernoulliRBM(
            n_visible=args.n_vis,
            n_hidden=args.n_hidden[0],
            W_init=args.w_init[0],
            vb_init=args.vb_init[0],
            hb_init=args.hb_init[0],
            n_gibbs_steps=args.n_gibbs_steps[0],
            learning_rate=args.lr[0],
            momentum=args.momentum,
            max_epoch=args.epochs[0],
            batch_size=args.batch_size[0],
            l2=args.l2[0],
            sample_h_states=True,
            sample_v_states=True,
            sparsity_cost=0.,
            dbm_first=True,  # !!!
            metrics_config=dict(
                msre=True,
                pll=True,
                train_metrics_every_iter=500,
            ),
            verbose=True,
            #display_filters=30,
            #display_hidden_activations=24,
            v_shape=args.v_shape,
            freeze_weights=args.freeze_weights,
            prune=args.prune,
            filter_shape=args.filter_shape[0],
            double_rf=args.double_rf,
            random_seed=args.random_seed[0],
            dtype='float32',
            tf_saver_params=dict(max_to_keep=1),
            model_path=args.rbm1_dirpath)
        #run on cpu
        config = tf.ConfigProto(device_count={'GPU': 0})
        rbm1._tf_session_config = config
        rbm1.fit(X)
    return rbm1