Пример #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.ind_uniq_test = np.unique(test_ind, axis=0)
        self.num_uniq_ind_test = len( self.ind_uniq_test)
        self.num_test_events = len(test_ind)

        # Integral Term
        # sum_i < int_0^T lam_i>
        # placeholders
        self.entry_ind_test = tf.constant( self.ind_uniq_test, dtype=tf.int32 )
        self.event_ind_test = tf.constant( self.test_ind, dtype=tf.int32)
        self.event_y_test = tf.constant( self.test_y, dtype=FLOAT_TYPE)

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

        self.tf_T_test = tf.constant(self.test_y[-1][0] - self.test_y[0][0], dtype=FLOAT_TYPE)

        # 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 = self.tf_T_test * tf.reduce_sum(self.base_rate_entries_test)

        # sample event base rate
        self.Knm_events_test = utils_funcs.kernel_cross_tf(self.X_events_test, self.tf_B, self.tf_log_amp_alpha,
                                                      self.tf_log_lengthscale_alpha)
        self.gp_base_rate_events_test = utils_funcs.sample_pst_f_tf_MLE(self.tf_mu_alpha, self.Kmm_alpha,self.Knm_events_test)

        # event sum term 1
        self.event_sum_test = tf.reduce_sum(self.gp_base_rate_events_test)
        self.llk_test = self.event_sum_test - self.int_part_test

        self.isTestGraphInitialized = True

        return self
Пример #2
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
Пример #3
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 = np.unique(self.train_ind, axis=0)

        self.num_events = len(self.train_ind)
        self.num_entries = len(self.uniq_ind)

        # self.log_file_name = init_config['log_file_name']
        self.init_U = init_config['U']
        self.rank = self.init_U[0].shape[1]

        self.batch_size_event = init_config['batch_size_event']
        self.batch_size_inner_event = init_config['batch_size_inner_event']
        self.batch_size_entry = init_config['batch_size_entry']
        self.batch_size_entryEvent = init_config['batch_size_entryEvent']

        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)
            ]
            #keras.initializers.
            # pseudo inputs
            self.tf_B = tf.Variable(self.B, dtype=FLOAT_TYPE)

            # pseudo outputs
            self.tf_mu_alpha = tf.Variable(np.random.randn(self.len_B, 1) *
                                           0.1,
                                           dtype=FLOAT_TYPE)
            #self.tf_Ltril_alpha = tf.Variable(np.eye(self.len_B), dtype=FLOAT_TYPE)
            self.tf_Ltril_alpha = tf.linalg.band_part(
                tf.Variable(np.eye(self.len_B) * 0.5, dtype=FLOAT_TYPE), -1, 0)

            self.tf_log_lengthscale_alpha = tf.Variable(np.zeros(
                [self.B.shape[1], 1]),
                                                        dtype=FLOAT_TYPE)
            self.tf_log_amp_alpha = tf.Variable(init_config['log_amp_alpha'],
                                                dtype=FLOAT_TYPE)

            self.tf_log_lengthscale_delta = tf.Variable(np.zeros(
                [self.B.shape[1], 1]),
                                                        dtype=FLOAT_TYPE)

            self.tf_log_amp_delta = tf.Variable(init_config['log_amp_delta'],
                                                dtype=FLOAT_TYPE)
            #self.tf_log_amp_delta = self.tf_log_amp_alpha

            self.tf_log_lengthscale_trig = tf.Variable(np.zeros(
                [self.B.shape[1], 1]),
                                                       dtype=FLOAT_TYPE)
            self.tf_log_amp_trig = tf.Variable(init_config['log_amp_trig'],
                                               dtype=FLOAT_TYPE)
            #self.tf_log_amp_trig = tf.constant( -50, 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.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_entryEvent_ind = tf.placeholder(
            dtype=tf.int32, shape=[self.batch_size_entryEvent, self.nmod])
        self.batch_entryEvent_y = tf.placeholder(
            dtype=FLOAT_TYPE, shape=[self.batch_size_entryEvent, 1])

        self.batch_event_ind = tf.placeholder(
            dtype=tf.int32, shape=[self.batch_size_event, self.nmod])
        self.batch_event_y = tf.placeholder(dtype=FLOAT_TYPE,
                                            shape=[self.batch_size_event, 1])

        self.batch_inner_event_ind = tf.placeholder(
            dtype=tf.int32, shape=[self.batch_size_inner_event, self.nmod])
        self.batch_inner_event_y = tf.placeholder(
            dtype=FLOAT_TYPE, shape=[self.batch_size_inner_event, 1])

        self.X_entries = utils_funcs.concat_embeddings(self.tf_U,
                                                       self.batch_entry_ind)
        self.X_entryEvents = utils_funcs.concat_embeddings(
            self.tf_U, self.batch_entryEvent_ind)
        self.X_events = utils_funcs.concat_embeddings(self.tf_U,
                                                      self.batch_event_ind)
        self.X_inner_events = utils_funcs.concat_embeddings(
            self.tf_U, self.batch_inner_event_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 * self.tf_T * tf.reduce_sum(
            self.base_rate_entries)

        # term 2
        # Sample posterior decay rate (delta)
        self.delta_entries = 1.0 / (utils_funcs.kernel_cross_tf(
            self.X_entries, self.X_entryEvents, self.tf_log_amp_delta,
            self.tf_log_lengthscale_delta) + DELTA_JITTER)
        self.T_sub_y = self.tf_T1 - self.batch_entryEvent_y  # [N_e, 1]
        self.lag_effect = (1.0 - tf.exp(-self.delta_entries * tf.transpose(
            self.T_sub_y))) / self.delta_entries  # [ N_i, N_e]
        self.K_trig = utils_funcs.kernel_cross_tf(
            self.X_entries, self.X_entryEvents, self.tf_log_amp_trig,
            self.tf_log_lengthscale_trig)  # [S,Q]
        self.Trig_Mat = self.K_trig * self.lag_effect
        # integral term 2
        self.int_part2 = self.num_entries / self.batch_size_entry * self.num_events / self.batch_size_entryEvent * tf.reduce_sum(
            self.Trig_Mat)

        # sample event base rate
        self.Knm_events = utils_funcs.kernel_cross_tf(
            self.X_events, self.tf_B, self.tf_log_amp_alpha,
            self.tf_log_lengthscale_alpha)
        self.gp_base_rate_events = utils_funcs.sample_pst_f_tf(
            self.tf_mu_alpha, self.tf_Ltril_alpha, self.Kmm_alpha,
            self.Knm_events, self.tf_log_amp_alpha, MATRIX_JITTER)
        self.base_rate_events = tf.exp(self.gp_base_rate_events)

        self.event_delay = self.batch_event_y - tf.transpose(
            self.batch_inner_event_y)
        self.valid_event_delay = tf.cast(self.event_delay > 0, FLOAT_TYPE)
        self.event_delay = self.event_delay * self.valid_event_delay
        self.delta_eventSum = 1.0 / (utils_funcs.kernel_cross_tf(
            self.X_events, self.X_inner_events, self.tf_log_amp_delta,
            self.tf_log_lengthscale_delta) + DELTA_JITTER)
        self.event_delay_effect = tf.exp(-self.delta_eventSum *
                                         self.event_delay)
        self.K_trig_event = utils_funcs.kernel_cross_tf(
            self.X_events, self.X_inner_events, self.tf_log_amp_trig,
            self.tf_log_lengthscale_trig)
        self.trig_mat_event = self.K_trig_event * self.event_delay_effect * self.valid_event_delay
        self.trig_effects_eventSum = tf.reduce_sum(self.trig_mat_event,
                                                   axis=1,
                                                   keepdims=True)

        # Bias est of event rates
        self.event_rates = self.base_rate_events + self.num_events / self.batch_size_inner_event * self.trig_effects_eventSum

        # sum term
        self.event_sum_term = self.num_events / self.batch_size_event * tf.reduce_sum(
            tf.log(self.event_rates))

        sqr_U = [tf.reduce_sum(U_i * U_i) for U_i in self.tf_U]
        self.U_kernel_mag = tf.reduce_sum(sqr_U)

        ###
        self.train_ELBO = 1.0 * self.event_sum_term - self.int_part1 - self.int_part2 - self.KL_alpha  # - self.KL_delta
        ###

        self.train_ELBO_hist = []

        # setting
        self.min_opt = tf.train.AdamOptimizer(self.learning_rate)
        self.min_step = self.min_opt.minimize(-self.train_ELBO)

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

        self.entries_ind_gnrt = utils_funcs.DataGenerator(self.uniq_ind)
        self.event_ind_y_gnrt = utils_funcs.DataGenerator(
            self.train_ind, self.train_y)
        self.entryEvent_ind_y_gnrt = utils_funcs.DataGenerator(
            self.train_ind, self.train_y)
        self.inner_event_ind_y_gnrt = utils_funcs.DataGenerator(
            self.train_ind, self.train_y)

        self.isTestGraphInitialized = False
Пример #4
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.ind_uniq_test = np.unique(test_ind, axis=0)
        self.num_uniq_ind_test = len(self.ind_uniq_test)
        self.num_test_events = len(test_ind)

        self.tf_test_event_ind_test = tf.constant(
            self.test_ind, dtype=tf.int32)  # ALL test events
        self.tf_test_event_y_test = tf.constant(
            self.test_y, dtype=FLOAT_TYPE)  # ALL test events
        self.tf_batch_entries_ind_test = tf.placeholder(
            dtype=tf.int32, shape=[None, self.nmod])

        # integral term
        # Use full testing event term when calculating batch integral terms
        self.T_test = tf.constant(self.test_y[-1][0] - self.test_y[0][0],
                                  dtype=FLOAT_TYPE)
        self.T0_test = tf.constant(self.test_y[0][0], dtype=FLOAT_TYPE)
        self.T1_test = tf.constant(self.test_y[-1][0], dtype=FLOAT_TYPE)

        self.X_batch_entries_test = utils_funcs.concat_embeddings(
            self.tf_U, self.tf_batch_entries_ind_test)
        self.X_all_test_events_test = utils_funcs.concat_embeddings(
            self.tf_U, self.tf_test_event_ind_test)

        self.Knm_entries_test = utils_funcs.kernel_cross_tf(
            self.X_batch_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)

        # term1
        self.int_term1_test = tf.reduce_sum(
            self.base_rate_entries_test) * self.T_test

        delta_entries_test = 1.0 / (utils_funcs.kernel_cross_tf(
            self.X_batch_entries_test, self.X_all_test_events_test,
            self.tf_log_amp_delta, self.tf_log_lengthscale_delta) +
                                    DELTA_JITTER)
        T_sub_y_test = self.T1_test - self.tf_test_event_y_test
        lag_effect_test = (
            1.0 - tf.exp(-delta_entries_test *
                         tf.transpose(T_sub_y_test))) / delta_entries_test
        self.K_trig_term3_test = utils_funcs.kernel_cross_tf(
            self.X_batch_entries_test, self.X_all_test_events_test,
            self.tf_log_amp_trig, self.tf_log_lengthscale_trig)
        self.term3_mat = self.K_trig_term3_test * lag_effect_test

        # term3
        self.int_term3_test = tf.reduce_sum(self.term3_mat)
        self.int_term_test = self.int_term1_test + self.int_term3_test

        # event sum term
        self.tf_batch_event_ind_test = tf.placeholder(dtype=tf.int32,
                                                      shape=[None, self.nmod])
        self.tf_batch_event_y_test = tf.placeholder(dtype=FLOAT_TYPE,
                                                    shape=[None, 1])
        self.X_batch_event_test = utils_funcs.concat_embeddings(
            self.tf_U, self.tf_batch_event_ind_test)

        self.test_eventSum_Knm = utils_funcs.kernel_cross_tf(
            self.X_batch_event_test, self.tf_B, self.tf_log_amp_alpha,
            self.tf_log_lengthscale_alpha)
        self.gp_eventSum_base_rate = utils_funcs.sample_pst_f_tf_MLE(
            self.tf_mu_alpha, self.Kmm_alpha, self.test_eventSum_Knm)
        self.eventSum_base_rate = tf.exp(
            self.gp_eventSum_base_rate)  # [ N_prime_batch, 1]

        self.event_delay_test = self.tf_batch_event_y_test - tf.transpose(
            self.tf_test_event_y_test)
        self.valid_event_delay_test = tf.cast(
            self.tf_batch_event_y_test > tf.transpose(
                self.tf_test_event_y_test),
            dtype=FLOAT_TYPE)
        self.event_delay_test = self.event_delay_test * self.valid_event_delay_test

        self.delta_eventSum_test = 1.0 / (utils_funcs.kernel_cross_tf(
            self.X_batch_event_test, self.X_all_test_events_test,
            self.tf_log_amp_delta, self.tf_log_lengthscale_delta) +
                                          DELTA_JITTER)
        self.event_delay_effect_test = tf.exp(-self.delta_eventSum_test *
                                              self.event_delay_test)

        self.K_trig_event_sum_test = utils_funcs.kernel_cross_tf(
            self.X_batch_event_test, self.X_all_test_events_test,
            self.tf_log_amp_trig, self.tf_log_lengthscale_trig)
        self.trig_mat_event_test = self.K_trig_event_sum_test * self.event_delay_effect_test * self.valid_event_delay_test

        self.event_rates_test = self.eventSum_base_rate + tf.reduce_sum(
            self.trig_mat_event_test, axis=1, keepdims=True)

        # eventsum term
        self.eventSum_term = tf.reduce_sum(tf.log(self.event_rates_test))

        self.isTestGraphInitialized = True

        return self