def build_model(self): num_units = self.FLAGS.num_units gru_net_ins = GRU() self.sequence_embedding, self.positive_embedding, \ self.behavior_embedding_result_dense, self.positive_embedding_result_dense, \ self.mask_index, self.label_ids, \ self.seq_length, user_embedding, self.time = self.embedding.get_embedding(num_units) with tf.variable_scope("EnhanceUserPreferenceIntentEncoder"): user_preference_temp = gru_net_ins.gru_net_initial(hidden_units=num_units, input_length=self.mask_index, input_data=self.behavior_embedding_result_dense, initial_state=user_embedding) self.user_preference = gather_indexes(batch_size=self.now_bacth_data_size, seq_length=self.FLAGS.max_len, width=self.FLAGS.num_units, sequence_tensor=user_preference_temp, positions=tf.add(self.mask_index, -1)) with tf.variable_scope("OutputLayer"): self.predict_behavior_emb = layer_norm(self.user_preference) # self.mf_auc = tf.reduce_mean(tf.to_float((tf.reduce_sum(tf.multiply(tf.expand_dims(self.predict_behavior_emb, 1), # tf.expand_dims(self.positive_embedding_result_dense, 1) - self.negative_embedding_result_dense), 2)) > 0)) l2_norm = tf.add_n([ tf.nn.l2_loss(self.sequence_embedding), tf.nn.l2_loss(self.positive_embedding) ]) 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.label_ids, [-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]) lstur_loss = regulation_rate * l2_norm + tf.reduce_mean(self.loss_origin) with tf.name_scope("LearningtoRankLoss"): self.loss = lstur_loss tf.summary.scalar("l2_norm", l2_norm) tf.summary.scalar("Training Loss", self.loss) tf.summary.scalar("Learning_rate", self.learning_rate) trainable_params = tf.trainable_variables() gradients = tf.gradients(self.loss, trainable_params) clip_gradients, _ = tf.clip_by_global_norm(gradients, self.FLAGS.max_gradient_norm) self.train_op = self.opt.apply_gradients(zip(clip_gradients, trainable_params)) self.summery()
def build_model(self): time_aware_attention = Time_Aware_Attention() self.gru_net_ins = GRU() 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') user_history = self.short_term_intent_temp 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 = layer_norm(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, 1, 1, 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 = tf.concat([self.short_term_intent, hybird_preference], 1) self.predict_behavior_emb = layer_norm(self.predict_behavior_emb) self.output_concat()
def build_model(self): time_aware_attention = Time_Aware_Attention() self.gru_net_ins = GRU() with tf.variable_scope("UserHistoryEncoder"): user_history = self.behavior_list_embedding_dense with tf.variable_scope('ShortTermIntentEncoder'): self.short_term_intent_temp = self.gru_net_ins.gru_net(hidden_units=self.num_units, input_data=self.behavior_list_embedding_dense, input_length=tf.add(self.seq_length, -1)) 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 ) #z = tf.concat([self.short_term_intent, hybird_preference], 1) #z = tf.layers.dropout(tf.layers.dense(z, self.num_units, activation=tf.nn.relu), rate=self.FLAGS.dropout, #training=True) #z = tf.sigmoid(tf.layers.dense(z, 1)) #self.predict_behavior_emb = layer_norm(z * hybird_preference + (1 - z) * self.short_term_intent) #self.output() self.predict_behavior_emb = layer_norm(hybird_preference) self.output()
def build_model(self): self.gru_net_ins = GRU() with tf.variable_scope('ShortTermIntentEncoder'): timenext_list = self.timelast_list[:, 1:] zeros = tf.zeros(shape=(self.now_bacth_data_size, 1)) timenext_list = tf.concat([timenext_list, zeros], axis=1) self.time_aware_gru_net_input = tf.concat([ self.behavior_list_embedding_dense, tf.expand_dims(self.timelast_list, 2), tf.expand_dims(timenext_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='T_Gru_Extend') 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.predict_behavior_emb = layer_norm(self.short_term_intent) self.output()
def build_model(self): print('--------------------num blocks-------------------------'+str(self.num_blocks)) self.gru_net_ins = GRU() with tf.variable_scope('ShortTermIntentEncoder',reuse=tf.AUTO_REUSE): time_aware_gru_net_input = tf.concat([self.behavior_list_embedding_dense, self.reconsume_lst_embedding, tf.expand_dims(self.timelast_list, 2), tf.expand_dims(self.reconsume_list,2)], axis=2) self.short_term_intent_temp = self.gru_net_ins.reconsume_prediction_gru_net(hidden_units=self.num_units, input_data=time_aware_gru_net_input, input_length=tf.add(self.seq_length,-1), # type='new', scope='gru') # batch_size, max_len, num_units emb = gather_indexes(batch_size=self.now_bacth_data_size, seq_length=self.max_len, width= self.num_units * 2 , sequence_tensor=self.short_term_intent_temp, positions=self.mask_index -1 )#batch_size, num_units # self.predict_is_reconsume = short_term_intent[:,-1] # 最后一位是interval batch_size, short_term_intent = emb[:,:self.num_units]# 前面是h # short_term_intent = tf.layers.dense(short_term_intent, self.num_units) self.predict_behavior_emb = layer_norm(short_term_intent) # batch_size, num_units predict_reconsume_emb = emb[:,self.num_units:] reconsume_table = self.embedding.reconsume_emb_lookup_table reconsume_scores = tf.nn.softmax(tf.matmul(predict_reconsume_emb,reconsume_table,transpose_b=True)) self.predict_is_reconsume = reconsume_scores[:,1] def cosine(q, a): pooled_len_1 = tf.sqrt(tf.reduce_sum(q * q, 1)) pooled_len_2 = tf.sqrt(tf.reduce_sum(a * a, 1)) pooled_mul_12 = tf.reduce_sum(q * a, 1) score = tf.div(pooled_mul_12, pooled_len_1 * pooled_len_2 + 1e-8, name="scores") return score item_embs = tf.reshape(self.item_list_emb,[-1,self.num_units]) # batch_size * max_len, num_units predict_target_embs = tf.tile(self.predict_behavior_emb,[self.max_len,1]) # batch_size * max_len, num_units reconsume_scores = cosine(predict_target_embs,item_embs) # batch_size * max_len self.reconsume_scores = tf.reshape(reconsume_scores,[-1,self.max_len]) self.output()
def build_model(self): print('--------------------num blocks-------------------------'+str(self.num_blocks)) self.gru_net_ins = GRU() with tf.variable_scope('ShortTermIntentEncoder',reuse=tf.AUTO_REUSE): timefirst_lst = tf.reshape(self.time_list[:, 0], [-1, 1]) idx = tf.range(start=1., limit=self.max_len , delta=1) idx0 = tf.constant([1.]) idx = tf.concat([idx0, idx], axis=0) avg_interval_lst = (self.time_list - timefirst_lst) / idx time_emb = self.embedding.get_thp_time_embedding(M=self.num_units,time_lst=self.timelast_list) time_aware_gru_net_input = tf.concat([self.behavior_list_embedding_dense, time_emb, tf.expand_dims(self.timelast_list, 2), tf.expand_dims(avg_interval_lst,2)], axis=2) self.short_term_intent_temp = self.gru_net_ins.only_time_prediction_gru_net(hidden_units=self.num_units, input_data=time_aware_gru_net_input, input_length=tf.add(self.seq_length,-1), # type='new', scope='gru') # batch_size, max_len, num_units short_term_intent = gather_indexes(batch_size=self.now_bacth_data_size, seq_length=self.max_len, width= self.num_units+1, sequence_tensor=self.short_term_intent_temp, positions=self.mask_index -1 )#batch_size, num_units self.interval_bar = short_term_intent[:,-1] # 最后一位是interval batch_size, self.last_time = gather_indexes(batch_size=self.now_bacth_data_size, seq_length=self.max_len, width=1, sequence_tensor=self.time_list, positions=self.mask_index - 1) # 上一个时间 self.interval = self.interval_bar self.predict_time = tf.reshape(tf.reshape(self.last_time,[-1,])+tf.reshape(self.interval,[-1,]),[-1,]) # masks = tf.sequence_mask(lengths=self.seq_length-1,maxlen=self.max_len,dtype=tf.float32) # timelast_list = masks * self.timelast_list # self.interval = tf.layers.dense(inputs = timelast_list, units=1, activation=tf.nn.relu) # self.predict_time = tf.reshape(tf.reshape(self.last_time,[-1,])+tf.reshape(self.interval,[-1,]),[-1,]) # self.predict_time = self.target[2] self.output()
def build_model(self): self.gru_net_ins = GRU() with tf.variable_scope('ShortTermIntentEncoder'): self.short_term_intent_temp = self.gru_net_ins.gru_net_initial(hidden_units=self.num_units, initial_state=self.user_embedding, input_data=self.behavior_list_embedding_dense, input_length=tf.add(self.seq_length, -1)) 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.predict_behavior_emb = layer_norm(self.short_term_intent) self.output()
class T_SeqRec(RNN_Baseline_model): def build_model(self): self.gru_net_ins = GRU() 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='T-SeqRec') 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 self.predict_behavior_emb = layer_norm(self.short_term_intent) self.output()
def build_model(self): self.gru = GRU() self.attention = Attention() self.wgat_model = WGAT() with tf.variable_scope('user_behavior_emb'): user_behavior_list_embedding = self.behavior_list_embedding_dense with tf.variable_scope('WGAT_gnn_encoding',reuse=tf.AUTO_REUSE): self.gnn_emb = self.wgat_model.generate_graph_emb(init_emb = user_behavior_list_embedding, key_length =self.seq_length, num_units = self.num_units , mask_adj=self.adj_in, num_head=self.FLAGS.graph_head, step=self.FLAGS.graph_step, dropout_rate=self.dropout_rate, max_len = self.max_len) with tf.variable_scope('readout_function',reuse=tf.AUTO_REUSE): q_star = self.gru_set2set(init_emb=self.gnn_emb, num_units=self.num_units, step=self.FLAGS.FGNN_readout_step) hybrid_preference = tf.layers.dense(q_star, units = self.num_units) self.predict_behavior_emb = hybrid_preference self.predict_behavior_emb = layer_norm(self.predict_behavior_emb) self.output()
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) with tf.variable_scope('ShortTermIntentEncoder'): timenext_list = self.timelast_list[:, 1:] zeros = tf.zeros(shape=(self.now_bacth_data_size, 1)) timenext_list = tf.concat([timenext_list, zeros], axis=1) self.time_aware_gru_net_input = tf.concat([ gnn_emb, tf.expand_dims(self.timelast_list, 2), tf.expand_dims(timenext_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.predict_behavior_emb = layer_norm(self.short_term_intent) self.output()
def build_model(self): attention = Attention() 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'): self.short_term_intent_temp = self.ggnn_model.generate_time_aware_emb( init_emb=user_behavior_list_embedding, adj_avg_time=self.adj_avg_time, now_batch_size=self.now_bacth_data_size, num_units=self.num_units, adj_in=self.adj_in, adj_out=self.adj_out, step=self.FLAGS.graph_step) # with tf.variable_scope('ShortTermIntentEncoder'): # self.short_term_intent_temp = self.gru_net_ins.gru_net(hidden_units=self.num_units, # input_data=self.behavior_list_embedding_dense, # input_length=tf.add(self.seq_length, -1)) user_history = self.short_term_intent_temp 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 = layer_norm(self.short_term_intent) short_term_intent4vallina = tf.expand_dims(self.short_term_intent, 1) with tf.variable_scope('NextItemDecoder'): hybird_preference = attention.vanilla_attention( user_history, short_term_intent4vallina, self.num_units, 1, 1, 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)) self.predict_behavior_emb = tf.concat( [self.short_term_intent, hybird_preference], 1) self.predict_behavior_emb = layer_norm(self.predict_behavior_emb) self.output_concat()
class Vallina_Gru4Rec(RNN_Baseline_model): def build_model(self): self.gru_net_ins = GRU() with tf.variable_scope('ShortTermIntentEncoder'): self.short_term_intent_temp = self.gru_net_ins.gru_net( hidden_units=self.num_units, input_data=self.item_list_emb, input_length=tf.add(self.seq_length, -1)) 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 self.predict_behavior_emb = layer_norm(self.short_term_intent) self.output()
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()
class MSRTP(MSRTP_model): def build_model(self): print('--------------------num blocks-------------------------' + str(self.num_blocks)) self.gru_net_ins = GRU() with tf.variable_scope('ShortTermIntentEncoder', reuse=tf.AUTO_REUSE): timefirst_lst = tf.reshape(self.time_list[:, 0], [-1, 1]) idx = tf.range(start=1., limit=self.max_len, delta=1) idx0 = tf.constant([1.]) idx = tf.concat([idx0, idx], axis=0) avg_interval_lst = (self.time_list - timefirst_lst) / idx time_aware_gru_net_input = tf.concat([ self.behavior_list_embedding_dense, tf.expand_dims(self.timelast_list, 2), tf.expand_dims(avg_interval_lst, 2) ], axis=2) self.short_term_intent_temp = self.gru_net_ins.time_prediction_gru_net( hidden_units=self.num_units, input_data=time_aware_gru_net_input, input_length=tf.add(self.seq_length, -1), # type='new', scope='gru') # batch_size, max_len, num_units short_term_intent = gather_indexes( batch_size=self.now_bacth_data_size, seq_length=self.max_len, width=2 * self.num_units + 1, sequence_tensor=self.short_term_intent_temp, positions=self.mask_index - 1) #batch_size, num_units self.interval_bar = short_term_intent[:, -1] # 最后一位是interval batch_size, short_term_intent = short_term_intent[:, :self.num_units] # 前面是h # short_term_intent = tf.layers.dense(short_term_intent, self.num_units) self.predict_behavior_emb = layer_norm( short_term_intent) # batch_size, num_units with tf.variable_scope('TimePredict', reuse=tf.AUTO_REUSE): # TODO need modified self.last_time = gather_indexes( batch_size=self.now_bacth_data_size, seq_length=self.max_len, width=1, sequence_tensor=self.time_list, positions=self.mask_index - 1) # 上一个时间 self.interval = self.interval_bar self.predict_time = tf.reshape( tf.reshape(self.last_time, [ -1, ]) + tf.reshape(self.interval, [ -1, ]), [ -1, ]) # self.predict_time = self.target[2] self.output()
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()
def build_model(self): time_aware_attention = Time_Aware_Attention() self.gru_net_ins = GRU() with tf.variable_scope("UserHistoryEncoder"): user_history = self.behavior_list_embedding_dense 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 = layer_norm(self.short_term_intent) with tf.variable_scope('StateEncoder'): 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.state_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.state = gather_indexes(batch_size=self.now_bacth_data_size, seq_length=self.max_len, width=self.num_units, sequence_tensor=self.state_temp, positions=self.mask_index - 1) self.state = layer_norm(self.state) 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) hybird_preference = layer_norm(hybird_preference) with tf.variable_scope('Switch'): self.long_term_prefernce = hybird_preference self.short_term_intent = self.short_term_intent self.z_concate = tf.concat( [self.long_term_prefernce, self.short_term_intent, self.state], 1) self.z = tf.layers.dense( inputs=self.z_concate, units=32, kernel_regularizer=tf.contrib.layers.l2_regularizer( self.regulation_rate), activation=tf.nn.relu) self.z = tf.layers.dense( inputs=self.z, units=1, kernel_regularizer=tf.contrib.layers.l2_regularizer( self.regulation_rate)) self.z = tf.nn.sigmoid(self.z) self.predict_behavior_emb = self.z * self.long_term_prefernce + ( 1 - self.z) * self.short_term_intent self.predict_behavior_emb = layer_norm(self.predict_behavior_emb) self.output()
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 embedding_size = self.FLAGS.itemid_embedding_size use_mmoe = self.FLAGS.use_mmoe attention_net = Attention() gru_net_ins = GRU() self.sequence_embedding, self.positive_embedding, \ self.behavior_embedding_result_dense, self.positive_embedding_result_dense, \ self.mask_index, self.label_ids, \ self.seq_length, user_embedding, self.time_interval, \ self.time, self.pos_last_list = self.embedding.get_embedding(num_units) with tf.variable_scope("LongTermIntentEncoder"): attention_output = attention_net.self_attention(enc=self.behavior_embedding_result_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) attention_pooling = tf.reduce_sum(attention_output, 1) # flat_attention_output = tf.reshape(attention_output, shape=[-1, embedding_size]) # flat_behavior_emb = tf.reshape(self.behavior_embedding_result_dense, shape=[-1, embedding_size]) # concat_emb = tf.concat([flat_attention_output, flat_behavior_emb], axis=1) # net = tf.layers.dense(concat_emb, embedding_size, activation=tf.nn.relu, use_bias=False) # att_wgt = tf.layers.dense(net, 1, activation=tf.nn.relu, use_bias=False) # att_wgt = tf.reshape(att_wgt, shape=[-1, seq_len]) # att_wgt = att_wgt / (embedding_size ** 0.5) # att_wgt = tf.nn.softmax(att_wgt) # att_wgt = tf.reshape(att_wgt, shape=[-1, seq_len, 1]) # output = tf.multiply(attention_output, att_wgt) # attention_pooling = tf.reduce_sum(output, 1) with tf.variable_scope("EnhanceUserPreferenceIntentEncoder"): gru_input = tf.concat([self.behavior_embedding_result_dense, tf.expand_dims(self.time_interval, 2), tf.expand_dims(self.pos_last_list, 2)], 2) self.gru_output = gru_net_ins.time_aware_gru_net(hidden_units=num_units, input_data=gru_input, input_length=tf.add(self.seq_length, -1)) self.gru_output = gather_indexes(batch_size=self.now_bacth_data_size, seq_length=self.FLAGS.max_len, width=self.FLAGS.num_units, sequence_tensor=self.gru_output, positions=tf.add(self.mask_index, -1)) _, seq_len, size = self.behavior_embedding_result_dense.get_shape().as_list() feature_emb = tf.reshape(self.behavior_embedding_result_dense, [-1, seq_len * size]) concat_output = tf.concat([self.gru_output, attention_pooling, feature_emb], axis=1) dence1 = tf.layers.dense(concat_output, concat_output.get_shape().as_list()[1] // 2, activation=tf.nn.relu, use_bias=False) self.user_preference = tf.layers.dense(dence1, num_units, activation=tf.nn.relu, use_bias=False) if use_mmoe: print("mmoe") with tf.variable_scope("mmoe"): num_expert = 8 expert_outputs = [] for _ in range(num_expert): expert_output = tf.layers.dense(self.user_preference, embedding_size, activation=tf.nn.relu, use_bias=False) expert_output = tf.expand_dims(expert_output, axis=2) # [B, 64, 1] expert_outputs.append(expert_output) expert_outputs = tf.concat(expert_outputs, axis=2) # [B, 64, 8] gate_network = tf.layers.dense(self.user_preference, num_expert, activation=tf.nn.softmax, use_bias=False) gate_network_dim = tf.expand_dims(gate_network, axis=1) # [B, 1, 8] weighted_expert_ouptputs = tf.tile(gate_network_dim, [1, embedding_size, 1]) * expert_outputs final_output = tf.reduce_sum(weighted_expert_ouptputs, axis=2) self.user_preference = tf.layers.dense(final_output, num_units, activation=tf.nn.relu, use_bias=False) with tf.variable_scope("OutputLayer"): self.predict_behavior_emb = layer_norm(self.user_preference) l2_norm = tf.add_n([ tf.nn.l2_loss(self.sequence_embedding), tf.nn.l2_loss(self.positive_embedding), tf.nn.l2_loss(user_embedding) ]) regulation_rate = self.FLAGS.regulation_rate item_lookup_table_T = tf.transpose(self.embedding.item_emb_lookup_table) #print("item_embedding:", item_lookup_table_T.get_shape().as_list()) logits = tf.matmul(self.predict_behavior_emb, item_lookup_table_T) log_probs = tf.nn.log_softmax(logits) label_ids = tf.reshape(self.label_ids, [-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]) lstur_loss = regulation_rate * l2_norm + tf.reduce_mean(self.loss_origin) with tf.name_scope("LearningtoRankLoss"): self.loss = lstur_loss if self.FLAGS.add_summary: tf.summary.scalar("l2_norm", l2_norm) tf.summary.scalar("Training Loss", self.loss) tf.summary.scalar("Learning_rate", self.learning_rate) trainable_params = tf.trainable_variables() gradients = tf.gradients(self.loss, trainable_params) clip_gradients, _ = tf.clip_by_global_norm(gradients, self.FLAGS.max_gradient_norm) self.train_op = self.opt.apply_gradients(zip(clip_gradients, trainable_params)) self.summery()
def build_model(self): num_units = self.FLAGS.num_units # self.sequence_embedding, self.positive_embedding, self.negative_embedding, \ # self.behavior_embedding_result_dense, self.positive_embedding_result_dense, \ # self.negative_embedding_result_dense, self.mask_index, self.label_ids, \ # self.seq_length = self.embedding.get_embedding(num_units) self.sequence_embedding, self.positive_embedding, \ self.behavior_embedding_result_dense, self.positive_embedding_result_dense, \ self.mask_index, self.label_ids, \ self.seq_length, user_embedding, self.time = self.embedding.get_embedding(num_units) gru = GRU() gru_output = gru.bidirectional_gru_net( num_units, self.behavior_embedding_result_dense, input_length=self.seq_length) attention_net = Attention() attention_output = attention_net.vanilla_attention_single( queries=gru_output, keys=gru_output, keys_length=self.seq_length) self.predict_behavior_emb = gather_indexes( batch_size=self.now_bacth_data_size, seq_length=self.FLAGS.max_len, width=self.FLAGS.num_units, sequence_tensor=attention_output, positions=self.mask_index) # self.predict_behavior_emb = tf.reshape(attention_output, [self.now_bacth_data_size, num_units]) with tf.variable_scope("OutputLayer"): self.predict_behavior_emb = layer_norm(self.predict_behavior_emb) # self.mf_auc = tf.reduce_mean( # tf.to_float((tf.reduce_sum(tf.multiply(tf.expand_dims(self.predict_behavior_emb, 1), # tf.expand_dims(self.positive_embedding_result_dense, # 1) - self.negative_embedding_result_dense), 2)) > 0)) l2_norm = tf.add_n([ tf.nn.l2_loss(self.sequence_embedding), tf.nn.l2_loss(self.positive_embedding) ]) 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.label_ids, [-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]) lstur_loss = regulation_rate * l2_norm + tf.reduce_mean( self.loss_origin) with tf.name_scope("LearningtoRankLoss"): self.loss = lstur_loss tf.summary.scalar("l2_norm", l2_norm) tf.summary.scalar("Training Loss", self.loss) tf.summary.scalar("Learning_rate", self.learning_rate) trainable_params = tf.trainable_variables() gradients = tf.gradients(self.loss, trainable_params) clip_gradients, _ = tf.clip_by_global_norm( gradients, self.FLAGS.max_gradient_norm) self.train_op = self.opt.apply_gradients( zip(clip_gradients, trainable_params)) self.summery()
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())
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 attention_net = Attention() gru_net_ins = GRU() self.sequence_embedding, self.positive_embedding, self.negative_embedding, \ self.behavior_embedding_result_dense, self.positive_embedding_result_dense, \ self.negative_embedding_result_dense, self.mask_index, self.label_ids, \ self.seq_length, user_embedding, time = self.embedding.get_embedding(num_units) with tf.variable_scope("LongTermIntentEncoder"): long_term_intent_temp = attention_net.self_attention( enc=self.behavior_embedding_result_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) self.long_term_intent = gather_indexes( batch_size=self.now_bacth_data_size, seq_length=self.FLAGS.max_len, width=self.FLAGS.num_units, sequence_tensor=long_term_intent_temp, positions=self.mask_index) # average pooling for long_term_intent_temp self.long_term_preference = tf.reduce_mean(long_term_intent_temp, axis=1) with tf.variable_scope('ShortTermIntentEncoder'): short_term_intent_temp = gru_net_ins.gru_net( hidden_units=num_units, input_data=self.behavior_embedding_result_dense, input_length=self.mask_index) 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=short_term_intent_temp, positions=tf.add(self.mask_index, -1)) with tf.variable_scope("EnhancePreferenceIntentEncoder"): user_enhance_preference_temp = gru_net_ins.gru_net_initial( hidden_units=num_units, input_length=self.mask_index, input_data=long_term_intent_temp, initial_state=self.long_term_preference) self.user_enhance_preference = gather_indexes( batch_size=self.now_bacth_data_size, seq_length=self.FLAGS.max_len, width=self.FLAGS.num_units, sequence_tensor=user_enhance_preference_temp, positions=tf.add(self.mask_index, -1)) with tf.variable_scope("EnhanceUserPreferenceIntentEncoder"): user_enhance_preference_temp_user = gru_net_ins.gru_net_initial( hidden_units=num_units, input_length=self.mask_index, input_data=long_term_intent_temp, initial_state=user_embedding) self.user_enhance_preference_user = gather_indexes( batch_size=self.now_bacth_data_size, seq_length=self.FLAGS.max_len, width=self.FLAGS.num_units, sequence_tensor=user_enhance_preference_temp_user, positions=tf.add(self.mask_index, -1)) with tf.variable_scope("PreferenceEncoder"): user_preference_temp = gru_net_ins.gru_net( hidden_units=num_units, input_data=long_term_intent_temp, input_length=self.mask_index) self.user_preference = gather_indexes( batch_size=self.now_bacth_data_size, seq_length=self.FLAGS.max_len, width=self.FLAGS.num_units, sequence_tensor=user_preference_temp, positions=tf.add(self.mask_index, -1)) with tf.variable_scope("OutputLayer"): self.predict_behavior_emb = self.user_enhance_preference_user self.predict_behavior_emb = layer_norm(self.predict_behavior_emb) self.mf_auc = tf.reduce_mean( tf.to_float((tf.reduce_sum( tf.multiply( tf.expand_dims(self.predict_behavior_emb, 1), tf.expand_dims(self.positive_embedding_result_dense, 1) - self.negative_embedding_result_dense), 2)) > 0)) l2_norm = tf.add_n([ tf.nn.l2_loss(self.sequence_embedding), tf.nn.l2_loss(self.positive_embedding), tf.nn.l2_loss(self.negative_embedding) ]) 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.label_ids, [-1]) one_hot_labels = tf.one_hot(label_ids, depth=500000, dtype=tf.float32) self.loss_origin = -tf.reduce_sum(log_probs * one_hot_labels, axis=[-1]) lstur_loss = regulation_rate * l2_norm + tf.reduce_mean( self.loss_origin) with tf.name_scope("LearningtoRankLoss"): self.loss = lstur_loss tf.summary.scalar("l2_norm", l2_norm) tf.summary.scalar("Training Loss", self.loss) tf.summary.scalar("Learning_rate", self.learning_rate) trainable_params = tf.trainable_variables() gradients = tf.gradients(self.loss, trainable_params) clip_gradients, _ = tf.clip_by_global_norm( gradients, self.FLAGS.max_gradient_norm) self.train_op = self.opt.apply_gradients( zip(clip_gradients, trainable_params)) self.summery()