-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
170 lines (145 loc) · 8.19 KB
/
model.py
File metadata and controls
170 lines (145 loc) · 8.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# -*- coding: utf-8 -*-
import tensorflow as tf
from layers import (
masked_average_pooling,
build_pairwise_time_gaps_from_seconds,
single_feature_embedding,
macro_temporal_encoding_personalized,
causal_attention
)
def build_graph(config):
seq_len = config.seq_len
d_model = config.d_model
num_heads = config.num_heads
labels_ph = tf.placeholder(tf.float32, shape=[None, 1], name="labels")
# sequence inputs
item_ids_ph = tf.placeholder(tf.int32, shape=[None, seq_len], name="item_ids")
seq_mask_ph = tf.placeholder(tf.bool, shape=[None, seq_len], name="seq_mask")
hour_ph = tf.placeholder(tf.float32, shape=[None, seq_len], name="hour_0_23")
weekday_ph = tf.placeholder(tf.float32, shape=[None, seq_len], name="weekday_0_6")
delta_t_hours_ph = tf.placeholder(tf.float32, shape=[None, seq_len], name="delta_t_hours")
seq_time_seconds_ph = tf.placeholder(tf.float32, shape=[None, seq_len], name="seq_time_seconds")
prev_day_segment_stat_ph = tf.placeholder(tf.float32, shape=[None, seq_len], name="prev_day_segment_stat")
hist30_segment_avg_stat_ph = tf.placeholder(tf.float32, shape=[None, seq_len], name="hist30_segment_avg_stat")
# user side features
user_active_degree_ph = tf.placeholder(tf.int32, shape=[None], name="user_active_degree")
is_lowactive_period_ph = tf.placeholder(tf.int32, shape=[None], name="is_lowactive_period")
is_live_streamer_ph = tf.placeholder(tf.int32, shape=[None], name="is_live_streamer")
is_video_author_ph = tf.placeholder(tf.int32, shape=[None], name="is_video_author")
follow_user_num_range_ph = tf.placeholder(tf.int32, shape=[None], name="follow_user_num_range")
fans_user_num_range_ph = tf.placeholder(tf.int32, shape=[None], name="fans_user_num_range")
register_days_range_ph = tf.placeholder(tf.int32, shape=[None], name="register_days_range")
onehot_feat1_ph = tf.placeholder(tf.int32, shape=[None], name="onehot_feat1")
onehot_feat2_ph = tf.placeholder(tf.int32, shape=[None], name="onehot_feat2")
onehot_feat3_ph = tf.placeholder(tf.int32, shape=[None], name="onehot_feat3")
# target item side features
target_video_id_ph = tf.placeholder(tf.int32, shape=[None], name="target_video_id")
target_author_id_ph = tf.placeholder(tf.int32, shape=[None], name="target_author_id")
target_video_type_ph = tf.placeholder(tf.int32, shape=[None], name="target_video_type")
target_music_type_ph = tf.placeholder(tf.int32, shape=[None], name="target_music_type")
# sequence item embedding
with tf.variable_scope("sequence_embedding", reuse=tf.AUTO_REUSE):
item_table = tf.get_variable(
"item_table",
shape=[config.video_vocab_size, d_model],
dtype=tf.float32,
initializer=tf.contrib.layers.xavier_initializer()
)
item_emb = tf.nn.embedding_lookup(item_table, item_ids_ph) # [N, L, D]
# user context for macro gating
user_context = masked_average_pooling(item_emb, seq_mask_ph) # [N, D]
# macro-temporal module
macro_temporal_emb, macro_scalar, macro_beta = macro_temporal_encoding_personalized(
hour_0_23=hour_ph,
weekday_0_6=weekday_ph,
delta_t_hours=delta_t_hours_ph,
seq_mask=seq_mask_ph,
d_model=d_model,
user_context=user_context,
burst_value=prev_day_segment_stat_ph,
avg_brust_value=hist30_segment_avg_stat_ph,
scope="macro_temporal"
)
seq_input = item_emb + macro_temporal_emb # [N, L, D]
# micro-sequential module inputs
pairwise_time_gaps = build_pairwise_time_gaps_from_seconds(seq_time_seconds_ph) # [N, L, L]
# decoder-only backbone: single masked self-attention block
decoder_output, att_vec = causal_attention(
queries=seq_input,
keys=seq_input,
num_units=d_model,
num_output_units=d_model,
num_heads=num_heads,
scope="decoder_block_self_attention",
reuse=tf.AUTO_REUSE,
query_masks=seq_mask_ph,
key_masks=seq_mask_ph,
pairwise_time_gaps=pairwise_time_gaps,
use_micro_bias=True,
micro_gamma=config.micro_gamma,
micro_tau_init=config.micro_tau_init,
micro_alpha_init=config.micro_alpha_init
) # [N, L, D]
# index 0 = most recent behavior
target_repr = decoder_output[:, 0, :] # [N, D]
# user side encoder
with tf.variable_scope("user_side_encoder", reuse=tf.AUTO_REUSE):
u1 = single_feature_embedding(user_active_degree_ph, config.user_active_degree_vocab_size, 4, "user_active_degree_emb")
u2 = single_feature_embedding(is_lowactive_period_ph, 2, 2, "is_lowactive_period_emb")
u3 = single_feature_embedding(is_live_streamer_ph, 2, 2, "is_live_streamer_emb")
u4 = single_feature_embedding(is_video_author_ph, 2, 2, "is_video_author_emb")
u5 = single_feature_embedding(follow_user_num_range_ph, config.range_vocab_size, 4, "follow_user_num_range_emb")
u6 = single_feature_embedding(fans_user_num_range_ph, config.range_vocab_size, 4, "fans_user_num_range_emb")
u7 = single_feature_embedding(register_days_range_ph, config.range_vocab_size, 4, "register_days_range_emb")
u8 = single_feature_embedding(onehot_feat1_ph, config.onehot_feat1_vocab_size, 4, "onehot_feat1_emb")
u9 = single_feature_embedding(onehot_feat2_ph, config.onehot_feat2_vocab_size, 4, "onehot_feat2_emb")
u10 = single_feature_embedding(onehot_feat3_ph, config.onehot_feat3_vocab_size, 8, "onehot_feat3_emb")
user_side_concat = tf.concat([u1, u2, u3, u4, u5, u6, u7, u8, u9, u10], axis=-1)
user_side_repr = tf.layers.dense(user_side_concat, d_model, activation=tf.nn.relu, name="user_side_proj")
# target item side encoder
with tf.variable_scope("target_item_side_encoder", reuse=tf.AUTO_REUSE):
t1 = single_feature_embedding(target_video_id_ph, config.video_vocab_size, 16, "target_video_id_emb")
t2 = single_feature_embedding(target_author_id_ph, config.author_vocab_size, 8, "target_author_id_emb")
t3 = single_feature_embedding(target_video_type_ph, config.video_type_vocab_size, 2, "target_video_type_emb")
t4 = single_feature_embedding(target_music_type_ph, config.music_type_vocab_size, 4, "target_music_type_emb")
target_side_concat = tf.concat([t1, t2, t3, t4], axis=-1)
target_side_repr = tf.layers.dense(target_side_concat, d_model, activation=tf.nn.relu, name="target_side_proj")
# final fusion + task tower
final_repr = tf.concat([target_repr, user_side_repr, target_side_repr], axis=-1)
with tf.variable_scope("task_tower", reuse=tf.AUTO_REUSE):
h1 = tf.layers.dense(final_repr, 16, activation=tf.nn.relu, name="fc16")
h2 = tf.layers.dense(h1, 8, activation=tf.nn.relu, name="fc8")
logits = tf.layers.dense(h2, 1, activation=None, name="fc1")
probs = tf.sigmoid(logits)
loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels=labels_ph, logits=logits))
optimizer = tf.train.AdamOptimizer(learning_rate=config.learning_rate)
train_op = optimizer.minimize(loss)
return {
"labels_ph": labels_ph,
"item_ids_ph": item_ids_ph,
"seq_mask_ph": seq_mask_ph,
"hour_ph": hour_ph,
"weekday_ph": weekday_ph,
"delta_t_hours_ph": delta_t_hours_ph,
"seq_time_seconds_ph": seq_time_seconds_ph,
"user_active_degree_ph": user_active_degree_ph,
"is_lowactive_period_ph": is_lowactive_period_ph,
"is_live_streamer_ph": is_live_streamer_ph,
"is_video_author_ph": is_video_author_ph,
"follow_user_num_range_ph": follow_user_num_range_ph,
"fans_user_num_range_ph": fans_user_num_range_ph,
"register_days_range_ph": register_days_range_ph,
"onehot_feat1_ph": onehot_feat1_ph,
"onehot_feat2_ph": onehot_feat2_ph,
"onehot_feat3_ph": onehot_feat3_ph,
"target_video_id_ph": target_video_id_ph,
"target_author_id_ph": target_author_id_ph,
"target_video_type_ph": target_video_type_ph,
"target_music_type_ph": target_music_type_ph,
"loss": loss,
"train_op": train_op,
"probs": probs,
"macro_scalar": macro_scalar,
"macro_beta": macro_beta,
"att_vec": att_vec
}