예제 #1
0
    def __init__(
            self,
            num_v,  # Number of visible units
            num_h,  # Number of hidden units
            Q,  # Dimensionality of X latent space
            N,  # Number of training points
            eval_flag,  # Set this to True at test time
            X0=None,
            # Initial value for X. Not needed at test time.
            # If not provided at training time, it is calculated using PCA on the data.
            V=None,  # High-dimensional data batch. Not needed at test time.
            Y_labels=None,  # Data labels. Unused at test time.
            kern=None,  # Default: SE kernel
            noise_variance=1.0,  # Initial value of the noise variance
            fixed_noise_variance=False,  # If True, the noise variance is constant
            noise_jitter=1e-6,
            W_std=0.001,  # Initial value for the W weights
            lr=0.001,  # Learning rate
            temperature=None,  # Concrete units temperature
            bottom=None,  # Bottom layer
            x_test_var=None,
            loss_plotter=None,
            distr_plotter=None,
            filter_plotter=None,
            latent_point_plotter=None,
            latent_sample_plotter=None,
            latent_space_explorer=None,
            session=None,
            name='GPRBM'):
        self.Q = Q
        self.N = N
        self.D = num_h
        self.Y_labels = Y_labels
        self.x_test = None
        self.lp_plotter = latent_point_plotter
        self.eval_interval = None
        self.downprop_op = None

        if not eval_flag:
            assert V is not None, 'V data must be provided at training time.'
            assert V.shape[0] == self.N
            self.V_data = tf.constant(V, dtype=c.float_type)

        if X0 is not None:
            assert X0.shape == (self.N, self.Q)
            self.X0 = tf.constant(X0, dtype=c.float_type)
        elif not eval_flag:
            X0 = preprocessing.PCA(V, self.Q)
            self.X0 = tf.constant(preprocessing.standardize(X0),
                                  dtype=c.float_type)
        else:
            self.X0 = tf.constant(np.zeros(shape=(self.N, self.Q)),
                                  dtype=c.float_type)

        with tf.variable_scope(name):
            self.noise_jitter = None
            if fixed_noise_variance == True:
                self.noise_variance = tf.constant(noise_variance,
                                                  dtype=c.float_type)
            else:
                noise_variance = tf.constant(noise_variance,
                                             dtype=c.float_type)
                noise_variance = preprocessing.inverse_softplus(noise_variance)
                self.opt_noise_variance = tf.Variable(
                    noise_variance,
                    dtype=c.float_type,
                    name='opt_noise_variance')

                self.noise_jitter = noise_jitter
                self.noise_variance = tf.nn.softplus(self.opt_noise_variance)

            self.X = tf.get_variable(name='X', initializer=self.X0)

            alpha_h_init_val = np.ones(shape=(1, self.D))
            alpha_h_init = tf.constant(alpha_h_init_val, dtype=c.float_type)
            alpha_h_init = preprocessing.inverse_softplus(alpha_h_init)
            self.opt_alpha_h = tf.get_variable(initializer=alpha_h_init,
                                               name='opt_alpha_h')

            # Constrain alpha_h parameter to be positive
            self.alpha_h = tf.nn.softplus(self.opt_alpha_h)

            self.H1_var = tf.get_variable(name='H1',
                                          initializer=tf.zeros(
                                              shape=(self.N, self.D),
                                              dtype=c.float_type),
                                          trainable=False)

            self.Y_labels_var = tf.get_variable(
                name='Y_labels',
                initializer=tf.zeros(shape=(self.N, 1)),
                trainable=False)

        self.kern = kern or gplvm.SEKernel(session=session, Q=self.Q)

        tf.summary.histogram(name + '_X', self.X)
        tf.summary.scalar(name + '_noise_variance', self.noise_variance)
        tf.summary.histogram(name + '_alpha_h', self.alpha_h)

        self.kern.init_variables()

        self.eval_flag_var = tf.get_variable(name='eval_flag',
                                             initializer=eval_flag,
                                             trainable=False)
        self.eval_flag_op = tf.assign(self.eval_flag_var,
                                      tf.logical_not(self.eval_flag_var))
        session.run(tf.variables_initializer([self.eval_flag_var]))
        self.eval_flag = session.run(self.eval_flag_var)

        self.x_ph = tf.cond(self.eval_flag_var, lambda: self._x_ph(x_test_var),
                            lambda: self.X)

        self.K = self.build_K()
        self.L = tf.cholesky(self.K)
        self.X_prior = self.build_X_prior()
        self.log_det = self.build_log_det()
        self.pred_var = self.build_pred_var()

        pred_std = tf.expand_dims(tf.sqrt(self.pred_var), 1)
        self.sigma_h = self.alpha_h * pred_std

        # This must be called after overriding self.sigma_h
        super().__init__(num_v=num_v,
                         num_h=num_h,
                         W_std=W_std,
                         lr=lr,
                         temperature=temperature,
                         bottom=bottom,
                         loss_plotter=loss_plotter,
                         distr_plotter=distr_plotter,
                         filter_plotter=filter_plotter,
                         latent_sample_plotter=latent_sample_plotter,
                         latent_space_explorer=latent_space_explorer,
                         session=session,
                         name=name)

        del self.params['sigma_h']
        self.params['alpha_h'] = self.alpha_h
        self.params['X'] = self.X
        self.params['noise_variance'] = self.noise_variance

        if self.eval_flag:
            self.H1 = self.H1_var
        else:
            self.H1 = self.upprop(self.V_data)
            # self.H1 = tf.constant(H0, dtype=c.float_type)
            self.update_H1_var = self.H1_var.assign(self.H1)

        tf.summary.histogram(self.name + '_H1', self.H1)

        self.H1_mean, _ = tf.nn.moments(self.H1, axes=[0], keep_dims=True)
        self.H2 = (self.H1 - self.H1_mean) / self.alpha_h

        self.H2_var = tf.get_variable(name='H2',
                                      initializer=tf.zeros(shape=(self.N,
                                                                  self.D)),
                                      trainable=False)
        self.H1_mean_var = tf.get_variable(
            name='H1_mean',
            initializer=tf.zeros(shape=(1, self.D)),
            trainable=False)

        self.update_H2_var = self.H2_var.assign(self.H2)
        self.update_H1_mean_var = self.H1_mean_var.assign(self.H1_mean)
예제 #2
0
 def test_standardize_std_ones(self):
     res = preprocessing.standardize(self.arr1)
     res_stds = np.std(res, axis=0)
     ones = np.ones(shape=(1, self.arr1.shape[1] - 1))
     self.assertTrue(np.allclose(res_stds[1:], ones))
예제 #3
0
 def test_standardize_std_zeros(self):
     res = preprocessing.standardize(self.arr1)
     res_stds = np.std(res, axis=0)
     self.assertEqual(res_stds[0], 0.)
예제 #4
0
 def test_standardize_means(self):
     res = preprocessing.standardize(self.arr1)
     res_means = np.mean(res, axis=0)
     zeros = np.zeros(shape=(1, self.arr1.shape[1]))
     self.assertTrue(np.allclose(res_means, zeros, atol=1e-5))
예제 #5
0
 def test_standardize_shape(self):
     res = preprocessing.standardize(self.arr1)
     self.assertEqual(res.shape, self.arr1.shape)
예제 #6
0
파일: gplvm.py 프로젝트: zeis/deepbelief
    def __init__(
            self,
            Y,
            Q,
            Y_labels=None,
            X0=None,
            kern=None,
            noise_variance=1.0,
            fixed_noise_variance=False,  # If True, the noise variance is constant
            noise_jitter=1e-6,
            bottom=None,
            x_test_var=None,
            subtract_Y_mean=True,
            uppropagate_Y=True,  # Ignored if bottom is None
            latent_point_plotter=None,
            latent_sample_plotter=None,
            latent_space_explorer=None,
            session=None,
            name='GPLVM'):

        assert type(Y) == np.ndarray, 'Y must be of type numpy.ndarray'
        assert Y.ndim == 2, 'Y must be 2-dimensional numpy.ndarray'

        self.Q = Q
        self.Y_labels = Y_labels
        self.subtract_Y_mean = subtract_Y_mean

        if uppropagate_Y and bottom is not None:
            self.Y = bottom.upprop(tf.constant(Y, dtype=c.float_type))
        else:
            self.Y = tf.constant(Y, dtype=c.float_type)

        if self.subtract_Y_mean:
            self.Y_mean = tf.reduce_mean(self.Y, axis=0, keepdims=True)
            self.Y = self.Y - self.Y_mean

        self.Y_np = session.run(self.Y)

        if X0 is not None:
            self.X0 = X0
        else:
            self.X0 = preprocessing.PCA(self.Y_np, self.Q)
            self.X0 = preprocessing.standardize(self.X0)

        assert self.X0.shape[0] == self.Y_np.shape[0]
        assert self.X0.shape[1] == self.Q

        self.N = self.X0.shape[0]
        self.D = self.Y_np.shape[1]

        with tf.variable_scope(name):
            self.noise_jitter = None
            if fixed_noise_variance is True:
                self.noise_variance = tf.constant(noise_variance,
                                                  dtype=c.float_type)
            else:
                noise_variance = tf.constant(noise_variance,
                                             dtype=c.float_type)
                noise_variance = preprocessing.inverse_softplus(noise_variance)
                self.opt_noise_variance = tf.Variable(
                    noise_variance,
                    dtype=c.float_type,
                    name='opt_noise_variance')

                self.noise_jitter = noise_jitter
                self.noise_variance = tf.nn.softplus(self.opt_noise_variance)

            self.X = tf.Variable(self.X0, dtype=c.float_type, name='X')

            # TODO: Add a label variable and make use of it at test time. See GPRBM.

        self.kern = kern or SEKernel(session=session, Q=self.Q)

        if x_test_var is not None:
            self.x_test = x_test_var
            self.x_ph = self.x_test
        else:
            self.x_ph = tf.placeholder(shape=(None, self.Q),
                                       dtype=c.float_type,
                                       name='x_ph')

        self.lp_plotter = latent_point_plotter
        self.ls_plotter = latent_sample_plotter
        self.latent_space_explorer = latent_space_explorer

        self.eval_interval = None
        self.loss_values_fname = None
        self.loss_values = None
        self.downprop_op = None

        tf.summary.histogram(name + '_X', self.X)
        tf.summary.scalar(name + '_noise_variance', self.noise_variance)

        params = {'X': self.X, 'noise_variance': self.noise_variance}

        super().__init__(params=params,
                         bottom=bottom,
                         session=session,
                         name=name)