Exemple #1
0
 def build_loss(self, sample_mean, summary=True):
     V_sample = self.downprop(sample_mean)
     cross_entropy_loss = dist.cross_entropy(V_sample,
                                             self.V_data,
                                             reduce_mean=False)
     gplvm_loss = self.build_gp_loss(summary=True)
     loss = cross_entropy_loss + gplvm_loss
     with tf.control_dependencies([tf.assert_type(loss, c.float_type)]):
         loss = tf.identity(loss)
     if summary:
         tf.summary.scalar(self.name + '_loss', loss)
     return loss
Exemple #2
0
    def test_generalisation(self,
                            test_data,
                            output_table_path,
                            num_iterations=1000,
                            num_runs=1,
                            num_samples_to_average=100):
        test_point_ph = tf.placeholder(shape=(1, test_data.shape[1]), dtype=c.float_type, name='test_point_ph')

        tiled_test_datapoint = tf.tile(test_point_ph, [num_samples_to_average, 1])

        datapoint = tf.get_variable(
            shape=(num_samples_to_average, test_data.shape[1]), name='datapoint')

        init_datapoint = datapoint.assign(tiled_test_datapoint)

        upprop = self.upprop(datapoint, sample=True)
        downprop = self.downprop(upprop, sample=True)
        update_datapoint = datapoint.assign(downprop)

        loss = dist.cross_entropy(tf.reduce_mean(datapoint, axis=0, keepdims=True), test_point_ph)

        table = []

        for test_point_index in range(test_data.shape[0]):
            test_point = np.reshape(test_data[test_point_index], newshape=(1, test_data.shape[1]))
            self.session.run(
                init_datapoint,
                feed_dict={test_point_ph: test_point})

            prev_dist = 10e7

            for run in range(num_runs):
                for i in range(num_iterations):
                    self.session.run(update_datapoint)

                model_point = self.session.run(tf.reduce_mean(downprop, axis=0))

                distance = self.session.run(
                        loss,
                        feed_dict={test_point_ph: test_point})

                if distance < prev_dist:
                    if table and table[-1][0] == test_point_index:
                        del table[-1]
                    table.append((test_point_index, distance, test_point, model_point))
                    prev_dist = distance

                self.logger.info('Test point: {}, Run: {}, Distance: {}'.format(test_point_index, run, distance))

        import pickle
        with open(output_table_path, 'wb') as f:
            pickle.dump(table, f)
Exemple #3
0
    def test_generalisation(self,
                            test_data,
                            output_table_path,
                            optimizer,
                            num_iterations=1000,
                            log_var_scaling=0.1,
                            method='all_training',
                            num_runs=1,
                            num_samples_to_average=1):
        """
        :param test_data: A Numpy array of the test data.
        :param output_table_path: Path of the pickled output table.
        :param optimizer: A Tensorflow optimiser.
        :param num_iterations: Number of optimisation iterations per run.
        :param log_var_scaling: Scaling factor of the log predictive variance term.
        :param method: String, it specifies how to initialise the test point at each run.
                       It can be 'all_training', 'random_normal', 'random_training'.
        :param num_runs: Number of random initialisation. If this is equal to X.shape[0] all
                         initial location in X will be tried.
        :param num_samples_to_average: Number of samples to average per point.

        The output of this method is a pickled table. See code.
        """
        test_point_ph = tf.placeholder(shape=(None, test_data.shape[1]),
                                       dtype=c.float_type,
                                       name='test_point_ph')

        predict_mean = self.build_sample_mean(
            self.H2_var, self.H1_mean_var, num_samples=num_samples_to_average)
        downprop_mean = self.downprop(predict_mean)

        # downprop_mean = t_interp_predictive_image
        # loss = dist.cross_entropy(tf.reduce_mean(downprop_mean, axis=0, keepdims=True), test_point_ph)
        loss = 1 / test_data.shape[1] * dist.cross_entropy(
            tf.reduce_mean(downprop_mean, axis=0, keepdims=True),
            test_point_ph) + log_var_scaling * tf.log(self.pred_var)

        min_step = optimizer.minimize(loss, var_list=[self.x_test])

        num_training_points = self.session.run(self.X).shape[0]

        assert num_runs <= num_training_points

        if method == 'all_training':
            assert num_runs == num_training_points
            idx = tf.get_variable(initializer=0, name='idx', dtype=tf.int32)
            zero_idx = tf.assign(idx, 0)
            increment_idx = tf.assign(idx, idx + 1)
            update_x_test = tf.assign(
                self.x_test, tf.reshape(self.X[idx], shape=(1, self.Q)))
        elif method == 'random_normal':
            update_x_test = tf.assign(
                self.x_test,
                tf.random_normal(shape=(1, self.Q), dtype=c.float_type))
        elif method == 'random_training':
            # At each run set the x_test variable at a random training latent location in X
            random_integer = tf.random_uniform(shape=(1, ),
                                               minval=0,
                                               maxval=num_training_points - 1,
                                               dtype=tf.int32,
                                               seed=0,
                                               name=None)
            update_x_test = tf.assign(
                self.x_test,
                tf.reshape(self.X[random_integer[0]], shape=(1, self.Q)))

        table = []

        self.session.graph.finalize()

        for test_point_index in range(test_data.shape[0]):
            test_point = np.reshape(test_data[test_point_index],
                                    newshape=(1, test_data.shape[1]))
            prev_dist = 10e7

            if method == 'all_training':
                self.session.run(zero_idx)

            for run in range(num_runs):
                self.session.run(update_x_test)

                for i in range(num_iterations):
                    self.session.run(min_step,
                                     feed_dict={test_point_ph: test_point})

                distance, x, model_point, predictive_variance = self.session.run(
                    [loss, self.x_test, downprop_mean, self.pred_var],
                    feed_dict={test_point_ph: test_point})

                model_point = np.mean(model_point, axis=0)

                if method == 'all_training':
                    self.session.run(increment_idx)

                distance = distance[0]
                if distance < prev_dist:
                    if table and table[-1][0] == test_point_index:
                        del table[-1]
                    table.append((test_point_index, distance, x, test_point,
                                  model_point, predictive_variance[0]))
                    prev_dist = distance

                self.logger.info(
                    'Test point: {}, Run: {}, Distance: {}'.format(
                        test_point_index, run, distance))

        import pickle
        with open(output_table_path, 'wb') as f:
            pickle.dump(table, f)
Exemple #4
0
    def backprop(self,
                 training_data,
                 test_data,
                 training_sampling=True,
                 training_num_propagation_cycles=5,  # Ignored if training_sampling is 'False'
                 test_num_propagation_cycles=5,
                 learning_rate=0.1,
                 max_iterations=1000,
                 ignore_ckpt_iter=False,
                 eval_interval=0,
                 ckpt_interval=0,
                 ckpt_dir=None):
        self.training_batch_ph = tf.placeholder(
            dtype=c.float_type,
            shape=training_data.batch_shape(),
            name='training_batch_ph')
        self.test_batch_ph = tf.placeholder(dtype=c.float_type,
                                            shape=test_data.batch_shape(),
                                            name='test_batch_ph')

        self.init_variables()

        if not training_sampling:
            training_num_propagation_cycles = 1

        training_v_batch_1 = self.propagate(
            self.training_batch_ph,
            k=training_num_propagation_cycles,
            sample=training_sampling)
        test_v_batch_1 = self.propagate(
            self.test_batch_ph,
            k=test_num_propagation_cycles,
            sample=True)

        training_loss = dist.cross_entropy(
            training_v_batch_1, self.training_batch_ph)
        test_loss = dist.cross_entropy(
            test_v_batch_1, self.test_batch_ph)

        optimizer = tf.train.GradientDescentOptimizer(
            learning_rate=learning_rate)

        training_step = optimizer.minimize(
            training_loss,
            # var_list=self._grad_var_list(lower_layers=True),
            gate_gradients=tf.train.Optimizer.GATE_GRAPH)

        data_distr, model_distr = self._test_distrs(test_v_batch_1)

        if ckpt_interval and ckpt_dir:
            update_ckpt_iter = self.checkpoint_iter.assign_add(ckpt_interval)

        if ignore_ckpt_iter:
            start_iter = 1
        else:
            start_iter = self.session.run(self.checkpoint_iter)

        max_iter = max_iterations + 1

        if eval_interval:
            self._loss_val_lists(start_iter, max_iter, eval_interval)

            # Evaluate the model the first time before training
            self._eval_step(test_data, test_loss, data_distr, model_distr)

        for self.iter in range(start_iter, max_iter):
            training_batch = training_data.next_batch()
            feed_dict = {self.training_batch_ph: training_batch}

            self.session.run(training_step, feed_dict=feed_dict)

            if eval_interval and self.iter % eval_interval == 0:
                self._eval_step(test_data, test_loss, data_distr, model_distr)

            if ckpt_dir and ckpt_interval and self.iter % ckpt_interval == 0:
                self.session.run(update_ckpt_iter)
                self.save_all(self.iter, ckpt_dir)
Exemple #5
0
    def test_generalisation(self,
                            test_data,
                            output_table_path,
                            optimizer,
                            log_var_scaling=0.1,
                            num_iterations=1000,
                            num_runs=1):
        test_point_ph = tf.placeholder(shape=(None, test_data.shape[1]),
                                       dtype=c.float_type,
                                       name='test_point_ph')

        pred_mean = self.build_pred_mean_normalized(self.pred_mean)
        clipped_mean = tf.clip_by_value(self.pred_mean, 0.0, 1.0)

        # loss = dist.cross_entropy(pred_mean, test_point_ph)
        loss = 1 / test_data.shape[1] * dist.cross_entropy(
            pred_mean, test_point_ph) + log_var_scaling * tf.log(self.pred_var)

        min_step = optimizer.minimize(loss, var_list=[self.x_test])

        num_training_points = self.session.run(self.X).shape[0]

        assert num_runs <= num_training_points

        if num_runs == num_training_points:
            idx = tf.get_variable(initializer=0, name='idx', dtype=tf.int32)
            zero_idx = tf.assign(idx, 0)
            increment_idx = tf.assign(idx, idx + 1)
            update_x_test = tf.assign(
                self.x_test, tf.reshape(self.X[idx], shape=(1, self.Q)))
        else:
            # At each run set the x_test variable at a random training latent location in X
            random_integer = tf.random_uniform(shape=(1, ),
                                               minval=0,
                                               maxval=num_training_points - 1,
                                               dtype=tf.int32,
                                               seed=0,
                                               name=None)
            update_x_test = tf.assign(
                self.x_test,
                tf.reshape(self.X[random_integer[0]], shape=(1, self.Q)))

        table = []

        self.session.graph.finalize()

        for test_point_index in range(test_data.shape[0]):
            test_point = np.reshape(test_data[test_point_index],
                                    newshape=(1, test_data.shape[1]))
            prev_dist = 10e7

            if num_runs == num_training_points:
                self.session.run(zero_idx)

            for run in range(num_runs):
                self.session.run(update_x_test)

                for i in range(num_iterations):
                    self.session.run(min_step,
                                     feed_dict={test_point_ph: test_point})

                distance, x, model_point, clipped_model_point = self.session.run(
                    [loss, self.x_test, pred_mean, clipped_mean],
                    feed_dict={test_point_ph: test_point})

                if num_runs == num_training_points:
                    self.session.run(increment_idx)

                distance = distance[0]
                if distance < prev_dist:
                    if table and table[-1][0] == test_point_index:
                        del table[-1]
                    table.append((test_point_index, distance, x, test_point,
                                  model_point, clipped_model_point))
                    prev_dist = distance

                self.logger.info(
                    'Test point: {}, Run: {}, Distance: {}'.format(
                        test_point_index, run, distance))

        import pickle
        with open(output_table_path, 'wb') as f:
            pickle.dump(table, f)