Example #1
0
    def create_standAlone_test_graph(self, test_ind, test_y):
        print("Create testing graph")
        self.test_ind = test_ind
        self.test_y = test_y

        self.num_test_events = len(test_ind)
        self.uniq_ind_test, self.n_i_test, self.sq_sum_test, self.log_sum_test = utils_funcs.extract_event_tensor_Reileigh(
            self.test_ind, self.test_y)
        self.num_uniq_ind_test = len(self.uniq_ind_test)

        # Integral Term
        # sum_i < int_0^T lam_i>
        # placeholders
        self.entry_ind_test = tf.constant(self.uniq_ind_test, dtype=tf.int32)
        self.entry_n_i_test = tf.constant(self.n_i_test, dtype=FLOAT_TYPE)
        self.entry_sq_sum_test = tf.constant(self.sq_sum_test,
                                             dtype=FLOAT_TYPE)
        self.entry_log_sum_test = tf.constant(self.log_sum_test,
                                              dtype=FLOAT_TYPE)

        self.X_entries_test = utils_funcs.concat_embeddings(
            self.tf_U, self.entry_ind_test)

        # sample posterior base rate ( f )
        self.Knm_entries_test = utils_funcs.kernel_cross_tf(
            self.X_entries_test, self.tf_B, self.tf_log_amp_alpha,
            self.tf_log_lengthscale_alpha)
        self.gp_base_rate_entries_test = utils_funcs.sample_pst_f_tf_MLE(
            self.tf_mu_alpha, self.Kmm_alpha, self.Knm_entries_test)
        self.base_rate_entries_test = tf.exp(self.gp_base_rate_entries_test)

        # int term 1, using entryEvent
        self.int_part_test = tf.reduce_sum(self.base_rate_entries_test *
                                           self.entry_sq_sum_test)

        # event sum term 1
        self.event_sum_test = tf.reduce_sum(self.gp_base_rate_entries_test *
                                            self.entry_n_i_test +
                                            self.entry_log_sum_test)
        self.llk_test = self.event_sum_test - self.int_part_test

        self.isTestGraphInitialized = True

        return self
    def create_standAlone_test_graph(self, test_ind, test_y):
        print("Create testing graph")
        self.test_ind = test_ind
        self.test_y = test_y

        self.num_test_events = len(test_ind)
        self.uniq_ind_test, self.n_i_test, self.sq_sum_test, self.log_sum_test = utils_funcs.extract_event_tensor_Reileigh(
            self.test_ind, self.test_y)
        self.num_uniq_ind_test = len(self.uniq_ind_test)

        # Integral Term
        # sum_i < int_0^T lam_i>
        # placeholders
        self.entry_ind_test = tf.constant(self.uniq_ind_test, dtype=tf.int32)
        self.entry_n_i_test = tf.constant(self.n_i_test, dtype=FLOAT_TYPE)
        self.entry_sq_sum_test = tf.constant(self.sq_sum_test,
                                             dtype=FLOAT_TYPE)
        self.entry_log_sum_test = tf.constant(self.log_sum_test,
                                              dtype=FLOAT_TYPE)

        self.gp_base_rate_entries_test = utils_funcs.log_CP_base_rate(
            self.tf_U, self.entry_ind_test)
        self.base_rate_entries_test = tf.exp(self.gp_base_rate_entries_test)

        # int term 1, using entryEvent
        self.int_part_test = tf.reduce_sum(self.base_rate_entries_test *
                                           self.entry_sq_sum_test)

        # event sum term 1
        self.event_sum_test = tf.reduce_sum(self.gp_base_rate_entries_test *
                                            self.entry_n_i_test +
                                            self.entry_log_sum_test)
        self.llk_test = self.event_sum_test - self.int_part_test

        self.isTestGraphInitialized = True

        return self
    def __init__(self, train_ind, train_y, init_config):
        self.train_ind = train_ind
        self.train_y = train_y
        self.nmod = train_ind.shape[1]
        self.uniq_ind, self.n_i, self.sq_sum, self.log_sum = utils_funcs.extract_event_tensor_Reileigh(
            self.train_ind, self.train_y)

        self.num_entries = len(self.uniq_ind)

        #self.log_file_name = init_config['log_file_name']
        self.init_U = init_config['U']
        self.batch_size_entry = init_config['batch_size_entry']
        self.learning_rate = init_config['learning_rate']

        self.GP_SCOPE_NAME = "gp_params"
        #GP parameters
        with tf.variable_scope(self.GP_SCOPE_NAME):
            # Embedding params
            self.tf_U = [
                tf.Variable(self.init_U[k], dtype=FLOAT_TYPE)
                for k in range(self.nmod)
            ]

        # Integral Term
        # sum_i < int_0^T lam_i>
        # placeholders
        self.batch_entry_ind = tf.placeholder(
            dtype=tf.int32, shape=[self.batch_size_entry, self.nmod])
        self.batch_entry_n_i = tf.placeholder(dtype=FLOAT_TYPE,
                                              shape=[self.batch_size_entry, 1])
        self.batch_entry_log_sum = tf.placeholder(
            dtype=FLOAT_TYPE, shape=[self.batch_size_entry, 1])
        self.batch_entry_sq_sum = tf.placeholder(
            dtype=FLOAT_TYPE, shape=[self.batch_size_entry, 1])

        self.tf_T = tf.constant(self.train_y[-1][0] - self.train_y[0][0],
                                dtype=FLOAT_TYPE)
        self.tf_T0 = tf.constant(self.train_y[0][0], dtype=FLOAT_TYPE)
        self.tf_T1 = tf.constant(self.train_y[-1][0], dtype=FLOAT_TYPE)

        # sample posterior base rate ( f )
        self.gp_base_rate_entries = utils_funcs.log_CP_base_rate(
            self.tf_U, self.batch_entry_ind)
        self.base_rate_entries = tf.exp(self.gp_base_rate_entries)

        #int term 1, using entryEvent
        self.int_part1 = self.num_entries / self.batch_size_entry * tf.reduce_sum(
            self.base_rate_entries * self.batch_entry_sq_sum)

        # event sum term 1
        self.eventSum = (self.batch_entry_n_i * self.gp_base_rate_entries +
                         self.batch_entry_log_sum)
        self.event_sum_part1 = self.num_entries / self.batch_size_entry * (
            tf.reduce_sum(self.eventSum))

        self.ELBO = self.event_sum_part1 - self.int_part1
        self.neg_ELBO = -self.ELBO
        self.ELBO_hist = []

        # setting
        self.min_opt = tf.train.AdamOptimizer(self.learning_rate)
        self.min_params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES,
                                            scope=self.GP_SCOPE_NAME)
        #print( self.min_params) ##
        self.min_step = self.min_opt.minimize(self.neg_ELBO,
                                              var_list=self.min_params)

        # GPU settings
        config = tf.ConfigProto()
        config.gpu_options.allow_growth = True
        self.sess = tf.Session(config=config)
        self.sess.run(tf.global_variables_initializer())

        self.entries_ind_y_gnrt = utils_funcs.DataGenerator(
            self.uniq_ind,
            np.concatenate([self.n_i, self.sq_sum, self.log_sum], axis=1))
        self.isTestGraphInitialized = False
Example #4
0
    def __init__(self, train_ind, train_y, init_config):
        self.train_ind = train_ind
        self.train_y = train_y
        self.nmod = train_ind.shape[1]
        self.uniq_ind, self.n_i, self.sq_sum, self.log_sum = utils_funcs.extract_event_tensor_Reileigh(
            self.train_ind, self.train_y)

        self.num_entries = len(self.uniq_ind)

        #self.log_file_name = init_config['log_file_name']
        self.init_U = init_config['U']
        self.batch_size_entry = init_config['batch_size_entry']
        self.learning_rate = init_config['learning_rate']

        # VI Sparse GP
        self.B = init_config['inducing_B']  # init with k-means, [len_B, rank]
        self.len_B = len(self.B)

        self.GP_SCOPE_NAME = "gp_params"
        #GP parameters
        with tf.variable_scope(self.GP_SCOPE_NAME):
            # Embedding params
            self.tf_U = [
                tf.Variable(self.init_U[k], dtype=FLOAT_TYPE)
                for k in range(self.nmod)
            ]
            # pseudo inputs
            self.tf_B = tf.Variable(self.B, dtype=FLOAT_TYPE)

            # pseudo outputs
            self.tf_mu_alpha = tf.Variable(np.zeros([self.len_B, 1]),
                                           dtype=FLOAT_TYPE)
            self.tf_Ltril_alpha = tf.Variable(np.eye(self.len_B),
                                              dtype=FLOAT_TYPE)
            self.tf_log_lengthscale_alpha = tf.Variable(np.zeros(
                [self.B.shape[1], 1]),
                                                        dtype=FLOAT_TYPE)
            self.tf_log_amp_alpha = tf.Variable(0, dtype=FLOAT_TYPE)

        self.Kmm_alpha = utils_funcs.kernel_cross_tf(
            self.tf_B, self.tf_B, self.tf_log_amp_alpha,
            self.tf_log_lengthscale_alpha)
        self.Kmm_alpha = self.Kmm_alpha + MATRIX_JITTER * tf.linalg.eye(
            self.len_B, dtype=FLOAT_TYPE)
        self.Var_alpha = self.tf_Ltril_alpha @ tf.transpose(
            self.tf_Ltril_alpha)
        # KL terms
        self.KL_alpha = utils_funcs.KL_q_p_tf(self.Kmm_alpha, self.Var_alpha,
                                              self.tf_Ltril_alpha,
                                              self.tf_mu_alpha, self.len_B)

        # Integral Term
        # sum_i < int_0^T lam_i>
        # placeholders
        self.batch_entry_ind = tf.placeholder(
            dtype=tf.int32, shape=[self.batch_size_entry, self.nmod])
        self.batch_entry_n_i = tf.placeholder(dtype=FLOAT_TYPE,
                                              shape=[self.batch_size_entry, 1])
        self.batch_entry_log_sum = tf.placeholder(
            dtype=FLOAT_TYPE, shape=[self.batch_size_entry, 1])
        self.batch_entry_sq_sum = tf.placeholder(
            dtype=FLOAT_TYPE, shape=[self.batch_size_entry, 1])

        self.X_entries = utils_funcs.concat_embeddings(self.tf_U,
                                                       self.batch_entry_ind)

        self.tf_T = tf.constant(self.train_y[-1][0] - self.train_y[0][0],
                                dtype=FLOAT_TYPE)
        self.tf_T0 = tf.constant(self.train_y[0][0], dtype=FLOAT_TYPE)
        self.tf_T1 = tf.constant(self.train_y[-1][0], dtype=FLOAT_TYPE)

        # sample posterior base rate ( f )
        self.Knm_entries = utils_funcs.kernel_cross_tf(
            self.X_entries, self.tf_B, self.tf_log_amp_alpha,
            self.tf_log_lengthscale_alpha)
        self.gp_base_rate_entries = utils_funcs.sample_pst_f_tf(
            self.tf_mu_alpha, self.tf_Ltril_alpha, self.Kmm_alpha,
            self.Knm_entries, self.tf_log_amp_alpha, MATRIX_JITTER)
        self.base_rate_entries = tf.exp(self.gp_base_rate_entries)

        #int term 1, using entryEvent
        self.int_part1 = self.num_entries / self.batch_size_entry * tf.reduce_sum(
            self.base_rate_entries * self.batch_entry_sq_sum)

        # event sum term 1
        self.eventSum = (self.batch_entry_n_i * self.gp_base_rate_entries +
                         self.batch_entry_log_sum)
        self.event_sum_part1 = self.num_entries / self.batch_size_entry * (
            tf.reduce_sum(self.eventSum))

        self.ELBO = self.event_sum_part1 - self.int_part1 - self.KL_alpha
        self.neg_ELBO = -self.ELBO
        self.ELBO_hist = []

        # setting
        self.min_opt = tf.train.AdamOptimizer(self.learning_rate)
        self.min_params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES,
                                            scope=self.GP_SCOPE_NAME)
        #print( self.min_params) ##
        self.min_step = self.min_opt.minimize(self.neg_ELBO,
                                              var_list=self.min_params)

        # GPU settings
        config = tf.ConfigProto()
        config.gpu_options.allow_growth = True
        self.sess = tf.Session(config=config)
        self.sess.run(tf.global_variables_initializer())

        self.entries_ind_y_gnrt = utils_funcs.DataGenerator(
            self.uniq_ind,
            np.concatenate([self.n_i, self.sq_sum, self.log_sum], axis=1))
        self.isTestGraphInitialized = False