Ejemplo n.º 1
0
 def build_model(self):
     time_aware_attention = Time_Aware_Attention()
     with tf.variable_scope("UserHistoryEncoder"):
         user_history = time_aware_attention.self_attention(
             enc=self.behavior_list_embedding_dense,
             num_units=self.num_units,
             num_heads=self.num_heads,
             num_blocks=self.num_blocks,
             dropout_rate=self.dropout_rate,
             is_training=True,
             reuse=False,
             key_length=self.seq_length,
             query_length=self.seq_length,
             t_querys=self.time_list,
             t_keys=self.time_list,
             t_keys_length=self.max_len,
             t_querys_length=self.max_len)
         long_term_prefernce = gather_indexes(
             batch_size=self.now_bacth_data_size,
             seq_length=self.max_len,
             width=self.num_units,
             sequence_tensor=user_history,
             positions=self.mask_index)
         self.predict_behavior_emb = long_term_prefernce
         self.predict_behavior_emb = layer_norm(self.predict_behavior_emb)
     self.output()
Ejemplo n.º 2
0
    def build_model(self):
        time_aware_attention = Time_Aware_Attention()
        self.gru_net_ins = GRU()
        with tf.variable_scope("UserHistoryEncoder"):
            user_history = time_aware_attention.self_attention(
                enc=self.behavior_list_embedding_dense,
                num_units=self.num_units,
                num_heads=self.num_heads,
                num_blocks=self.num_blocks,
                dropout_rate=self.dropout_rate,
                is_training=True,
                reuse=False,
                key_length=self.seq_length,
                query_length=self.seq_length,
                t_querys=self.time_list,
                t_keys=self.time_list,
                t_keys_length=self.max_len,
                t_querys_length=self.max_len)
        with tf.variable_scope('ShortTermIntentEncoder'):
            self.time_aware_gru_net_input = tf.concat([
                self.behavior_list_embedding_dense,
                tf.expand_dims(self.timelast_list, 2),
                tf.expand_dims(self.timenow_list, 2)
            ], 2)
            self.short_term_intent_temp = self.gru_net_ins.time_aware_gru_net(
                hidden_units=self.num_units,
                input_data=self.time_aware_gru_net_input,
                input_length=tf.add(self.seq_length, -1),
                type='new')
            self.short_term_intent = gather_indexes(
                batch_size=self.now_bacth_data_size,
                seq_length=self.max_len,
                width=self.num_units,
                sequence_tensor=self.short_term_intent_temp,
                positions=self.mask_index - 1)
            self.short_term_intent = self.short_term_intent

            short_term_intent4vallina = tf.expand_dims(self.short_term_intent,
                                                       1)
        with tf.variable_scope('NextItemDecoder'):
            hybird_preference = time_aware_attention.vanilla_attention(
                user_history,
                short_term_intent4vallina,
                self.num_units,
                self.num_heads,
                self.num_blocks,
                self.dropout_rate,
                is_training=True,
                reuse=False,
                key_length=self.seq_length,
                query_length=tf.ones_like(short_term_intent4vallina[:, 0, 0],
                                          dtype=tf.int32),
                t_querys=tf.expand_dims(self.target[2], 1),
                t_keys=self.time_list,
                t_keys_length=self.max_len,
                t_querys_length=1)
            self.predict_behavior_emb = layer_norm(hybird_preference)
        self.output()
Ejemplo n.º 3
0
    def build_model(self):
        time_aware_attention = Time_Aware_Attention()
        with tf.variable_scope("UserHistoryEncoder"):
            user_history = time_aware_attention.self_attention(
                enc=self.behavior_list_embedding_dense,
                num_units=128,
                num_heads=self.num_heads,
                num_blocks=self.num_blocks,
                dropout_rate=self.dropout_rate,
                is_training=True,
                reuse=False,
                key_length=self.seq_length,
                query_length=self.seq_length,
                t_querys=self.time_list,
                t_keys=self.time_list,
                t_keys_length=self.max_len,
                t_querys_length=self.max_len)
            long_term_prefernce = gather_indexes(
                batch_size=self.now_bacth_data_size,
                seq_length=self.max_len,
                width=self.FLAGS.num_units,
                sequence_tensor=user_history,
                positions=self.mask_index)
            self.predict_behavior_emb = long_term_prefernce
            self.predict_behavior_emb = layer_norm(self.predict_behavior_emb)
        with tf.name_scope('CrossEntropyLoss'):

            l2_norm = tf.add_n([
                tf.nn.l2_loss(self.item_list_emb),
                tf.nn.l2_loss(self.category_list_emb),
                tf.nn.l2_loss(self.position_list_emb)
            ])
            regulation_rate = self.FLAGS.regulation_rate
            item_lookup_table_T = tf.transpose(
                self.embedding.item_emb_lookup_table)
            logits = tf.matmul(self.predict_behavior_emb, item_lookup_table_T)
            log_probs = tf.nn.log_softmax(logits)
            label_ids = tf.reshape(self.target[0], [-1])
            one_hot_labels = tf.one_hot(label_ids,
                                        depth=self.embedding.item_count + 3,
                                        dtype=tf.float32)
            self.loss_origin = -tf.reduce_sum(log_probs * one_hot_labels,
                                              axis=[-1])
            self.loss = regulation_rate * l2_norm + tf.reduce_mean(
                self.loss_origin)
            tf.summary.scalar('l2_norm', l2_norm)
            tf.summary.scalar('Training Loss', self.loss)
            tf.summary.scalar('Learning_rate', self.learning_rate)

        self.cal_gradient(tf.trainable_variables())
Ejemplo n.º 4
0
    def build_model(self):
        self.gru_net_ins = GRU()
        self.ggnn_model = gated_GNN()
        with tf.variable_scope('user_behavior_emb'):
            user_behavior_list_embedding = self.behavior_list_embedding_dense

        with tf.variable_scope('ggnn_encoding'):
            gnn_emb = self.ggnn_model.generate_graph_emb(
                init_emb=user_behavior_list_embedding,
                now_batch_size=self.now_bacth_data_size,
                num_units=self.num_units,
                adj_in=self.adj_in,
                adj_out=self.adj_out,
                step=1)

        time_aware_attention = Time_Aware_Attention()
        with tf.variable_scope("UserHistoryEncoder"):
            user_history = time_aware_attention.self_attention(
                gnn_emb,
                self.num_units,
                self.num_heads,
                self.num_blocks,
                self.dropout_rate,
                is_training=True,
                reuse=False,
                key_length=self.seq_length,
                query_length=self.seq_length,
                t_querys=self.time_list,
                t_keys=self.time_list,
                t_keys_length=self.max_len,
                t_querys_length=self.max_len)
            long_term_prefernce = gather_indexes(
                batch_size=self.now_bacth_data_size,
                seq_length=self.max_len,
                width=self.FLAGS.num_units,
                sequence_tensor=user_history,
                positions=self.mask_index)
            self.predict_behavior_emb = long_term_prefernce
            self.predict_behavior_emb = layer_norm(self.predict_behavior_emb)
        self.output()
Ejemplo n.º 5
0
    def build_model(self):
        time_aware_attention = Time_Aware_Attention()
        self.gru_net_ins = GRU()
        with tf.variable_scope("UserHistoryEncoder"):
            user_history = time_aware_attention.self_attention(
                enc=self.behavior_list_embedding_dense,
                num_units=self.num_units,
                num_heads=self.num_heads,
                num_blocks=self.num_blocks,
                dropout_rate=self.dropout_rate,
                is_training=True,
                reuse=False,
                key_length=self.seq_length,
                query_length=self.seq_length,
                t_querys=self.time_list,
                t_keys=self.time_list,
                t_keys_length=self.max_len,
                t_querys_length=self.max_len)
        with tf.variable_scope('ShortTermIntentEncoder'):
            self.time_aware_gru_net_input = tf.concat([
                self.behavior_list_embedding_dense,
                tf.expand_dims(self.timelast_list, 2),
                tf.expand_dims(self.timenow_list, 2)
            ], 2)
            self.short_term_intent_temp = self.gru_net_ins.time_aware_gru_net(
                hidden_units=self.num_units,
                input_data=self.time_aware_gru_net_input,
                input_length=tf.add(self.seq_length, -1),
                type='time-aware')
            self.short_term_intent = gather_indexes(
                batch_size=self.now_bacth_data_size,
                seq_length=self.max_len,
                width=self.num_units,
                sequence_tensor=self.short_term_intent_temp,
                positions=self.mask_index - 1)
            self.short_term_intent = self.short_term_intent

            short_term_intent4vallina = tf.expand_dims(self.short_term_intent,
                                                       1)
        with tf.variable_scope('NextItemDecoder'):
            hybird_preference = time_aware_attention.vanilla_attention(
                user_history,
                short_term_intent4vallina,
                self.num_units,
                self.num_heads,
                self.num_blocks,
                self.dropout_rate,
                is_training=True,
                reuse=False,
                key_length=self.seq_length,
                query_length=tf.ones_like(short_term_intent4vallina[:, 0, 0],
                                          dtype=tf.int32),
                t_querys=tf.expand_dims(self.target[2], 1),
                t_keys=self.time_list,
                t_keys_length=self.max_len,
                t_querys_length=1)
            self.predict_behavior_emb = hybird_preference
            self.predict_behavior_emb = layer_norm(self.predict_behavior_emb)
        with tf.variable_scope('OutputLayer'):
            long_term_prefernce = gather_indexes(
                batch_size=self.now_bacth_data_size,
                seq_length=self.max_len,
                width=self.num_units,
                sequence_tensor=user_history,
                positions=self.mask_index)
            self.long_term_prefernce = long_term_prefernce
            self.short_term_intent = self.short_term_intent
            self.hybird_preference = hybird_preference

            self.z_concate = tf.concat([
                self.long_term_prefernce, self.short_term_intent,
                self.hybird_preference
            ], 1)

            self.z = tf.layers.dense(
                inputs=self.z_concate,
                units=3,
                kernel_regularizer=tf.contrib.layers.l2_regularizer(
                    self.regulation_rate))
            self.z = tf.nn.softmax(self.z)

            if self.FLAGS.PISTRec_type == 'hard':
                if tf.argmax(self.z) == 0:
                    self.predict_behavior_emb = self.long_term_prefernce
                elif tf.argmax(self.z) == 1:
                    self.predict_behavior_emb = self.short_term_intent
                else:
                    self.predict_behavior_emb = self.hybird_preference

            elif self.FLAGS.PISTRec_type == 'soft':
                self.predict_behavior_emb = tf.multiply(tf.slice(self.z,[0,0],[-1,1]),self.long_term_prefernce)+\
                                            tf.multiply(tf.slice(self.z, [0, 1], [-1, 1]), self.short_term_intent)+\
                                            tf.multiply(tf.slice(self.z, [0, 2], [-1, 1]), self.hybird_preference)
            elif self.FLAGS.PISTRec_type == 'short':
                self.predict_behavior_emb = self.short_term_intent
            elif self.FLAGS.PISTRec_type == 'long':
                self.predict_behavior_emb = self.long_term_prefernce
            elif self.FLAGS.PISTRec_type == 'hybird':
                self.predict_behavior_emb = self.hybird_preference

            self.predict_behavior_emb = layer_norm(self.predict_behavior_emb)

        self.output()
Ejemplo n.º 6
0
    def build_model(self):

        num_units = self.FLAGS.num_units
        num_heads = self.FLAGS.num_heads
        num_blocks = self.FLAGS.num_blocks
        dropout_rate = self.FLAGS.dropout

        time_aware_attention = Time_Aware_Attention()
        self.gru_net_ins = GRU()

        self.user_embedding, \
        self.behavior_list_embedding_dense, \
        self.item_list_emb,\
        self.category_list_emb, \
        self.position_list_emb,\
        self.time_list, \
        self.timelast_list, \
        self.timenow_list, \
        self.target, \
        self.seq_length = self.embedding.get_embedding(num_units)
        max_len = self.FLAGS.length_of_user_history
        self.mask_index = tf.reshape(self.seq_length - 1,
                                     [self.now_bacth_data_size, 1])

        with tf.variable_scope("UserHistoryEncoder"):
            user_history = time_aware_attention.self_attention(
                enc=self.behavior_list_embedding_dense,
                num_units=128,
                num_heads=num_heads,
                num_blocks=num_blocks,
                dropout_rate=dropout_rate,
                is_training=True,
                reuse=False,
                key_length=self.seq_length,
                query_length=self.seq_length,
                t_querys=self.time_list,
                t_keys=self.time_list,
                t_keys_length=max_len,
                t_querys_length=max_len)

            self.user_h = user_history
        with tf.variable_scope('ShortTermIntentEncoder'):

            timelast_list = tf.expand_dims(self.timelast_list, 2)
            self.time_aware_gru_net_input = tf.concat([
                self.behavior_list_embedding_dense,
                tf.expand_dims(self.timelast_list, 2),
                tf.expand_dims(self.timenow_list, 2)
            ], 2)
            self.error = tf.reshape(tf.add(self.seq_length, -1), [-1])
            self.short_term_intent_temp = self.gru_net_ins.time_aware_gru_net(
                hidden_units=num_units,
                input_data=self.time_aware_gru_net_input,
                input_length=tf.add(self.seq_length, -1))

            self.short_term_intent = gather_indexes(
                batch_size=self.now_bacth_data_size,
                seq_length=self.FLAGS.max_len,
                width=self.FLAGS.num_units,
                sequence_tensor=self.short_term_intent_temp,
                positions=self.mask_index - 1)

            short_term_intent4vallina = tf.expand_dims(self.short_term_intent,
                                                       1)
        with tf.variable_scope('NextItemDecoder'):
            hybird_preference = time_aware_attention.vanilla_attention(
                user_history,
                short_term_intent4vallina,
                num_units,
                num_heads,
                num_blocks,
                dropout_rate,
                is_training=True,
                reuse=False,
                key_length=self.seq_length,
                query_length=tf.ones_like(short_term_intent4vallina[:, 0, 0],
                                          dtype=tf.int32),
                t_querys=tf.expand_dims(self.target[2], 1),
                t_keys=self.time_list,
                t_keys_length=max_len,
                t_querys_length=1)

            #self.hybird_preference = hybird_preference

        with tf.variable_scope('OutputLayer'):
            long_term_prefernce = gather_indexes(
                batch_size=self.now_bacth_data_size,
                seq_length=self.FLAGS.max_len,
                width=self.FLAGS.num_units,
                sequence_tensor=user_history,
                positions=self.mask_index)
            self.long_term_prefernce = long_term_prefernce
            self.short_term_intent = self.short_term_intent
            self.hybird_preference = hybird_preference

            self.z_concate = tf.concat([
                self.long_term_prefernce, self.short_term_intent,
                self.hybird_preference
            ], 1)

            self.z = tf.layers.dense(
                inputs=self.z_concate,
                units=3,
                kernel_regularizer=tf.contrib.layers.l2_regularizer(
                    self.FLAGS.regulation_rate))
            self.z = tf.nn.softmax(self.z)

            if self.FLAGS.PISTRec_type == 'hard':
                if tf.argmax(self.z) == 0:
                    self.predict_behavior_emb = self.long_term_prefernce
                elif tf.argmax(self.z) == 1:
                    self.predict_behavior_emb = self.short_term_intent
                else:
                    self.predict_behavior_emb = self.hybird_preference

            elif self.FLAGS.PISTRec_type == 'soft':
                self.predict_behavior_emb = tf.multiply(tf.slice(self.z,[0,0],[-1,1]),self.long_term_prefernce)+\
                                            tf.multiply(tf.slice(self.z, [0, 1], [-1, 1]), self.short_term_intent)+\
                                            tf.multiply(tf.slice(self.z, [0, 2], [-1, 1]), self.hybird_preference)
            elif self.FLAGS.PISTRec_type == 'short':
                self.predict_behavior_emb = self.short_term_intent
            elif self.FLAGS.PISTRec_type == 'long':
                self.predict_behavior_emb = self.long_term_prefernce
            elif self.FLAGS.PISTRec_type == 'hybird':
                self.predict_behavior_emb = self.hybird_preference

            self.predict_behavior_emb = layer_norm(self.predict_behavior_emb)

            l2_norm = tf.add_n([
                tf.nn.l2_loss(self.item_list_emb),
                tf.nn.l2_loss(self.category_list_emb),
                tf.nn.l2_loss(self.position_list_emb)
            ])
            regulation_rate = self.FLAGS.regulation_rate

            item_lookup_table_T = tf.transpose(
                self.embedding.item_emb_lookup_table)
            logits = tf.matmul(self.predict_behavior_emb, item_lookup_table_T)
            log_probs = tf.nn.log_softmax(logits)
            label_ids = tf.reshape(self.target[0], [-1])
            one_hot_labels = tf.one_hot(label_ids,
                                        depth=self.embedding.item_count + 3,
                                        dtype=tf.float32)
            self.loss_origin = -tf.reduce_sum(log_probs * one_hot_labels,
                                              axis=[-1])

            pistloss = regulation_rate * l2_norm + tf.reduce_mean(
                self.loss_origin)

        with tf.name_scope('LearningtoRankLoss'):
            self.loss = pistloss
            tf.summary.scalar('l2_norm', l2_norm)
            tf.summary.scalar('Training Loss', self.loss)
            tf.summary.scalar('Learning_rate', self.learning_rate)

        self.cal_gradient(tf.trainable_variables())