视频讲解:https://www.yuque.com/chudi/tzqav9/ny150b#aalY8
import tensorflow as tf
from tensorflow import keras
from utils import *
EPOCH = 10
BATCH_SIZE = 32
VEC_DIM = 10
DROPOUT_RATE = 0.5
HEAD_NUM = 4
HIDE_SIZE = 10
base, test = loadData()
# 所有的特征各个类别值个数之和
FEAT_CATE_NUM = base.shape[1] - 1
K = tf.keras.backend
def run():
# 将所有的特征的各个类别值统一id化。x中每行为各特征的类别值的id
val_x, val_y = getAllData(test)
train_x, train_y = getAllData(base)
feat_num = val_x[0].shape[0]
print(feat_num)
inputs_id = keras.Input((feat_num,))
emb = keras.layers.Embedding(FEAT_CATE_NUM, VEC_DIM, input_length=feat_num)(inputs_id)
# ( batch , cate_num, VEC_DIM)
Qs = keras.layers.Dense(HEAD_NUM * VEC_DIM)(emb) # ( batch , cate_num, HEAD_NUM * VEC_DIM)
Qs = keras.layers.Dropout(DROPOUT_RATE)(Qs)
Qs = tf.split(Qs, HEAD_NUM * [VEC_DIM], 2) # (head_num , batch , cate_num, VEC_DIM)
Ks = keras.layers.Dense(HEAD_NUM * VEC_DIM)(emb)
Ks = keras.layers.Dropout(DROPOUT_RATE)(Ks)
Ks = tf.split(Ks, HEAD_NUM * [VEC_DIM], 2)
Vs = keras.layers.Dense(HEAD_NUM * VEC_DIM)(emb)
Vs = keras.layers.Dropout(DROPOUT_RATE)(Vs)
Vs = tf.split(Vs, HEAD_NUM * [VEC_DIM], 2)
es = []
for num in range(HEAD_NUM):
score = tf.nn.softmax(tf.matmul(Qs[num], Ks[num], transpose_b=True) / VEC_DIM) # (batch , cate_num, cate_num)
hide = tf.matmul(score, Vs[num]) # (batch , cate_num, VEC_DIM)
hide = keras.layers.Flatten()(hide) # (batch , cate_num * VEC_DIM)
es.append(hide)
self_att = keras.layers.concatenate(es) # (batch , HEAD_NUM * cate_num * VEC_DIM)
self_att = keras.layers.Dropout(DROPOUT_RATE)(self_att)
self_att = keras.layers.Dense(feat_num * VEC_DIM)(self_att)
emb = keras.layers.Flatten()(emb)
interact_layer = keras.layers.add([self_att, emb])
interact_layer = keras.layers.Dropout(DROPOUT_RATE)(interact_layer)
outputs = keras.layers.Dense(1, activation='sigmoid',
kernel_regularizer=keras.regularizers.l2(0.001))(interact_layer)
model = keras.Model(inputs=inputs_id, outputs=outputs)
model.compile(loss='binary_crossentropy', optimizer=tf.train.AdamOptimizer(0.001), metrics=[keras.metrics.AUC()])
tbCallBack = keras.callbacks.TensorBoard(log_dir='./logs',
histogram_freq=0,
write_graph=True,
write_grads=True,
write_images=True,
embeddings_freq=0,
embeddings_layer_names=None,
embeddings_metadata=None)
model.fit(train_x, train_y, batch_size=BATCH_SIZE, epochs=EPOCH, verbose=2, validation_data=(val_x, val_y),
callbacks=[tbCallBack])
run()