Skip to content

Commit

Permalink
resolve conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
tangxyw committed Jan 12, 2022
1 parent 046ccd5 commit 6e0de88
Show file tree
Hide file tree
Showing 9 changed files with 5 additions and 868 deletions.
5 changes: 1 addition & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,6 @@ dmypy.json
.pyre/

.idea
<<<<<<< HEAD
*.csv
.DS_Store
=======
*.csv
>>>>>>> 734986b93a9246f05fb1b15f98977242f436de04

40 changes: 0 additions & 40 deletions algorithm/DIN/activations.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,3 @@
<<<<<<< HEAD
import tensorflow as tf


def prelu(x, name=""):
"""
Args:
x (tf.Tensor): 输入tensor
name (str): alpha的变量名后缀
Returns:
经过prelu激活后的tensor
"""

alpha = tf.get_variable(name=f"prelu_alpha_{name}",
shape=x.shape[-1],
initializer=tf.constant_initializer(1.0),
dtype=x.dtype)
return tf.maximum(0.0, x) + alpha * tf.minimum(0.0, x)


def dice(x, name=""):
"""
Args:
x (tf.Tensor): 输入tensor
name (str): alpha, beta的变量名后缀
Returns:
经过dice激活后的tensor
"""

alpha = tf.get_variable(name=f"dice_alpha_{name}",
shape=x.shape[-1],
initializer=tf.constant_initializer(1.0),
dtype=x.dtype)
# 利用batch_normalization的API, 无需可训练参数beta和gamma
x_norm = tf.layers.batch_normalization(x, center=False, scale=False, name=f"dice_bn_{name}")
px = tf.sigmoid(x_norm)

return x * px + alpha * x * (1 - px)
=======
import tensorflow as tf


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

return x * px + alpha * x * (1 - px)
>>>>>>> 734986b93a9246f05fb1b15f98977242f436de04
370 changes: 2 additions & 368 deletions algorithm/DIN/din.py

Large diffs are not rendered by default.

65 changes: 1 addition & 64 deletions algorithm/DIN/din_attention.py
Original file line number Diff line number Diff line change
@@ -1,65 +1,3 @@
<<<<<<< HEAD
import tensorflow as tf


def din_attention(query, keys, keys_length, is_softmax=False):
"""
实现DIN模型中的attention模块
Args:
query (tf.Tensor): 目标 shape=(B, H)
keys (tf.Tensor): 历史行为序列, shape=(B, T, H)
keys_length (tf.Tensor): 历史行为队列长度, 目的为生成mask, shape=(B, )
is_softmax (bool): attention权重是否使用softmax激活
Returns:
tf.Tensor, weighted sum pooling结果
"""

embedding_dim = query.shape[-1].value
query = tf.tile(query, multiples=[1, tf.shape(keys)[1]]) # (B, H*T)
query = tf.reshape(query, shape=(-1, tf.shape(keys)[1], embedding_dim)) # (B, T, H)
cross_all = tf.concat([query, keys, query - keys, query * keys], axis=-1) # (B, T, 4*H)
d_layer_1_all = tf.layers.dense(cross_all, 64, activation=tf.nn.relu, name='f1_att', reuse=tf.AUTO_REUSE) # (B, T, 64)
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)
d_layer_3_all = tf.layers.dense(d_layer_2_all, 1, activation=None, name='f3_att', reuse=tf.AUTO_REUSE) # (B, T, 1)
output_weight = d_layer_3_all # (B, T, 1)

# mask
keys_mask = tf.sequence_mask(keys_length, tf.shape(keys)[1]) # (B, T)
keys_mask = tf.expand_dims(keys_mask, -1) # 与output_weight对齐, (B, T, 1)

if is_softmax:
paddings = tf.ones_like(output_weight) * (-2 ** 32 + 1) # (B, T, 1)
output_weight = tf.where(keys_mask, output_weight, paddings) # (B, T, 1)
# scale, 防止梯度消失
output_weight = output_weight / (embedding_dim ** 0.5) # (B, T, 1)
output_weight = tf.nn.softmax(output_weight, axis=1) # (B, T, 1)
else: # 按论文原文, 不使用softmax激活
output_weight = tf.cast(keys_mask, tf.float32) # (B, T, 1)

outputs = tf.matmul(output_weight, keys, transpose_a=True) # (B, 1, T) * (B, T, H) = (B, 1, H)
outputs = tf.squeeze(outputs, 1) # (B, H)

return outputs


if __name__ == "__main__":
# Test
# B=2, T=3, H=4
# fake_keys = tf.zeros(shape=(2, 3, 4))
fake_keys = tf.random_normal(shape=(2, 3, 4))
fake_query = tf.random_normal(shape=(2, 4))
fake_keys_length = tf.constant([0, 1], 3)
attention_out1 = din_attention(fake_query, fake_keys, fake_keys_length, is_softmax=False)
attention_out2 = din_attention(fake_query, fake_keys, fake_keys_length, is_softmax=True)

with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print("不使用softmax激活:")
print(sess.run(attention_out1))
print("使用softmax激活:")
print(sess.run(attention_out2))
=======
import tensorflow as tf


Expand Down Expand Up @@ -119,5 +57,4 @@ def din_attention(query, keys, keys_length, is_softmax=False):
print("不使用softmax激活:")
print(sess.run(attention_out1))
print("使用softmax激活:")
print(sess.run(attention_out2))
>>>>>>> 734986b93a9246f05fb1b15f98977242f436de04
print(sess.run(attention_out2))
15 changes: 1 addition & 14 deletions algorithm/DIN/result.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,4 @@
<<<<<<< HEAD

| 试验序号 | activation | mini_batch_aware_regularization | l2_lambda | use_softmax |eval_auc
| ------- | ------- | ------- | ------- | ------- |------- |
| 1 |dice |True|0.2|False|0.90204114
| 2 |prelu |True|0.2|False|0.9070767
| 3 |dice |False|0.2|False|0.9115021
| 4 |prelu |False|0.2|False|0.91133076
| 5 |dice |True|0.2|True|0.90439874
| 6 |prelu |True|0.2|True|0.9038621
| 3 |dice |False|0.2|True|0.9116896
| 4 |prelu |False|0.2|True|0.9108566
=======


| 试验序号 | activation | mini_batch_aware_regularization | l2_lambda | use_softmax |eval_auc
| ------- | ------- | ------- | ------- | ------- |------- |
Expand All @@ -22,4 +10,3 @@
| 6 |prelu |True|0.2|True|0.9038621
| 7 |dice |False|0.2|True|0.9116896
| 8 |prelu |False|0.2|True|0.9108566
>>>>>>> 734986b93a9246f05fb1b15f98977242f436de04
Loading

0 comments on commit 6e0de88

Please sign in to comment.