Skip to content

Commit 6e0de88

Browse files
committed
resolve conflict
1 parent 046ccd5 commit 6e0de88

File tree

9 files changed

+5
-868
lines changed

9 files changed

+5
-868
lines changed

.gitignore

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,6 @@ dmypy.json
129129
.pyre/
130130

131131
.idea
132-
<<<<<<< HEAD
133132
*.csv
134133
.DS_Store
135-
=======
136-
*.csv
137-
>>>>>>> 734986b93a9246f05fb1b15f98977242f436de04
134+

algorithm/DIN/activations.py

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,3 @@
1-
<<<<<<< HEAD
2-
import tensorflow as tf
3-
4-
5-
def prelu(x, name=""):
6-
"""
7-
Args:
8-
x (tf.Tensor): 输入tensor
9-
name (str): alpha的变量名后缀
10-
Returns:
11-
经过prelu激活后的tensor
12-
"""
13-
14-
alpha = tf.get_variable(name=f"prelu_alpha_{name}",
15-
shape=x.shape[-1],
16-
initializer=tf.constant_initializer(1.0),
17-
dtype=x.dtype)
18-
return tf.maximum(0.0, x) + alpha * tf.minimum(0.0, x)
19-
20-
21-
def dice(x, name=""):
22-
"""
23-
Args:
24-
x (tf.Tensor): 输入tensor
25-
name (str): alpha, beta的变量名后缀
26-
Returns:
27-
经过dice激活后的tensor
28-
"""
29-
30-
alpha = tf.get_variable(name=f"dice_alpha_{name}",
31-
shape=x.shape[-1],
32-
initializer=tf.constant_initializer(1.0),
33-
dtype=x.dtype)
34-
# 利用batch_normalization的API, 无需可训练参数beta和gamma
35-
x_norm = tf.layers.batch_normalization(x, center=False, scale=False, name=f"dice_bn_{name}")
36-
px = tf.sigmoid(x_norm)
37-
38-
return x * px + alpha * x * (1 - px)
39-
=======
401
import tensorflow as tf
412

423

@@ -74,4 +35,3 @@ def dice(x, name=""):
7435
px = tf.sigmoid(x_norm)
7536

7637
return x * px + alpha * x * (1 - px)
77-
>>>>>>> 734986b93a9246f05fb1b15f98977242f436de04

algorithm/DIN/din.py

Lines changed: 2 additions & 368 deletions
Large diffs are not rendered by default.

algorithm/DIN/din_attention.py

Lines changed: 1 addition & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,3 @@
1-
<<<<<<< HEAD
2-
import tensorflow as tf
3-
4-
5-
def din_attention(query, keys, keys_length, is_softmax=False):
6-
"""
7-
实现DIN模型中的attention模块
8-
Args:
9-
query (tf.Tensor): 目标 shape=(B, H)
10-
keys (tf.Tensor): 历史行为序列, shape=(B, T, H)
11-
keys_length (tf.Tensor): 历史行为队列长度, 目的为生成mask, shape=(B, )
12-
is_softmax (bool): attention权重是否使用softmax激活
13-
14-
Returns:
15-
tf.Tensor, weighted sum pooling结果
16-
"""
17-
18-
embedding_dim = query.shape[-1].value
19-
query = tf.tile(query, multiples=[1, tf.shape(keys)[1]]) # (B, H*T)
20-
query = tf.reshape(query, shape=(-1, tf.shape(keys)[1], embedding_dim)) # (B, T, H)
21-
cross_all = tf.concat([query, keys, query - keys, query * keys], axis=-1) # (B, T, 4*H)
22-
d_layer_1_all = tf.layers.dense(cross_all, 64, activation=tf.nn.relu, name='f1_att', reuse=tf.AUTO_REUSE) # (B, T, 64)
23-
d_layer_2_all = tf.layers.dense(d_layer_1_all, 32, activation=tf.nn.relu, name='f2_att', reuse=tf.AUTO_REUSE) # (B, T, 32)
24-
d_layer_3_all = tf.layers.dense(d_layer_2_all, 1, activation=None, name='f3_att', reuse=tf.AUTO_REUSE) # (B, T, 1)
25-
output_weight = d_layer_3_all # (B, T, 1)
26-
27-
# mask
28-
keys_mask = tf.sequence_mask(keys_length, tf.shape(keys)[1]) # (B, T)
29-
keys_mask = tf.expand_dims(keys_mask, -1) # 与output_weight对齐, (B, T, 1)
30-
31-
if is_softmax:
32-
paddings = tf.ones_like(output_weight) * (-2 ** 32 + 1) # (B, T, 1)
33-
output_weight = tf.where(keys_mask, output_weight, paddings) # (B, T, 1)
34-
# scale, 防止梯度消失
35-
output_weight = output_weight / (embedding_dim ** 0.5) # (B, T, 1)
36-
output_weight = tf.nn.softmax(output_weight, axis=1) # (B, T, 1)
37-
else: # 按论文原文, 不使用softmax激活
38-
output_weight = tf.cast(keys_mask, tf.float32) # (B, T, 1)
39-
40-
outputs = tf.matmul(output_weight, keys, transpose_a=True) # (B, 1, T) * (B, T, H) = (B, 1, H)
41-
outputs = tf.squeeze(outputs, 1) # (B, H)
42-
43-
return outputs
44-
45-
46-
if __name__ == "__main__":
47-
# Test
48-
# B=2, T=3, H=4
49-
# fake_keys = tf.zeros(shape=(2, 3, 4))
50-
fake_keys = tf.random_normal(shape=(2, 3, 4))
51-
fake_query = tf.random_normal(shape=(2, 4))
52-
fake_keys_length = tf.constant([0, 1], 3)
53-
attention_out1 = din_attention(fake_query, fake_keys, fake_keys_length, is_softmax=False)
54-
attention_out2 = din_attention(fake_query, fake_keys, fake_keys_length, is_softmax=True)
55-
56-
with tf.Session() as sess:
57-
sess.run(tf.global_variables_initializer())
58-
print("不使用softmax激活:")
59-
print(sess.run(attention_out1))
60-
print("使用softmax激活:")
61-
print(sess.run(attention_out2))
62-
=======
631
import tensorflow as tf
642

653

@@ -119,5 +57,4 @@ def din_attention(query, keys, keys_length, is_softmax=False):
11957
print("不使用softmax激活:")
12058
print(sess.run(attention_out1))
12159
print("使用softmax激活:")
122-
print(sess.run(attention_out2))
123-
>>>>>>> 734986b93a9246f05fb1b15f98977242f436de04
60+
print(sess.run(attention_out2))

algorithm/DIN/result.md

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,4 @@
1-
<<<<<<< HEAD
2-
3-
| 试验序号 | activation | mini_batch_aware_regularization | l2_lambda | use_softmax |eval_auc
4-
| ------- | ------- | ------- | ------- | ------- |------- |
5-
| 1 |dice |True|0.2|False|0.90204114
6-
| 2 |prelu |True|0.2|False|0.9070767
7-
| 3 |dice |False|0.2|False|0.9115021
8-
| 4 |prelu |False|0.2|False|0.91133076
9-
| 5 |dice |True|0.2|True|0.90439874
10-
| 6 |prelu |True|0.2|True|0.9038621
11-
| 3 |dice |False|0.2|True|0.9116896
12-
| 4 |prelu |False|0.2|True|0.9108566
13-
=======
1+
142

153
| 试验序号 | activation | mini_batch_aware_regularization | l2_lambda | use_softmax |eval_auc
164
| ------- | ------- | ------- | ------- | ------- |------- |
@@ -22,4 +10,3 @@
2210
| 6 |prelu |True|0.2|True|0.9038621
2311
| 7 |dice |False|0.2|True|0.9116896
2412
| 8 |prelu |False|0.2|True|0.9108566
25-
>>>>>>> 734986b93a9246f05fb1b15f98977242f436de04

0 commit comments

Comments
 (0)