注意tf2 embedding的使用:
加载预训练词向量
1、创建矩阵,索引与词向量的对应映射
model_creative_id = gensim.models.Word2Vec.load('model_creative_id_word_skip_200_3')
## 构造包含所有词语的 list,以及初始化 “词语-序号”字典 和 “词向量”矩阵
vocab_list = [word for word, Vocab in model_creative_id.wv.vocab.items()]# 存储 所有的 词语
word_index = {" ": 0}# 初始化 `[word : token]` ,后期 tokenize 语料库就是用该词典。
word_vector = {} # 初始化`[word : vector]`字典
# 初始化存储所有向量的大矩阵,留意其中多一位(首行),词向量全为 0,用于 padding补零。
# 行数 为 所有单词数+1 比如 10000+1 ; 列数为 词向量“维度”比如100。
embeddings_matrix_creative_id = np.zeros((len(vocab_list) + 1, model_creative_id.vector_size))
## 填充 上述 的字典 和 大矩阵
for i in range(len(vocab_list)):
# print(i)
word = vocab_list[i] # 每个词语
word_index[word] = i + 1 # 词语:序号
word_vector[word] = model_creative_id.wv[word] # 词语:词向量
embeddings_matrix_creative_id[i + 1] = model_creative_id.wv[word] # 词向量矩阵
2、建立单词与索引的对应
creative_id_list =clicks_all1_test.groupby(['user_id']).apply(lambda x: x['creative_id'].tolist()).tolist()
max_length = 200
# X_train_idx = [[word2idx.get(w, 0) for w in sen] for sen in sentenList]
test_creative_id1 = preprocessing.sequence.pad_sequences(
[[word_index.get(str(w), 0) for w in sen] for sen in creative_id_list], # list of list
dtype='int32',
padding = 'post',# post, pre
maxlen = max_length)
产生的矩阵与索引列表都可以np先保存
np.save('embeddings_matrix_creative_id.npy',embeddings_matrix_creative_id)
np.save('test_creative_id1.npy',test_creative_id1)
embeddings_matrix_creative_id = np.load('embeddings_matrix_creative_id.npy')
1、esim短文本匹配模型
input的shape是索引列表padding后的大小,Embedding的200是词向量的维度大小
#esim age
# n_timesteps = 1
# X_train2=X_train.reshape(X_train.shape[0],n_timesteps,X_train.shape[1])
# y_train2=OneHotEncoder(sparse = False).fit_transform(y_train)
# y_train3 = y_train2.reshape(y_train2.shape[0],1,y_train2.shape[1])
# X_test2=X_test.reshape(X_test.shape[0],n_timesteps,X_test.shape[1])
# y_test2=OneHotEncoder(sparse = False).fit_transform(y_test)
# y_test3 = y_test2.reshape(y_test2.shape[0],1,y_test2.shape[1])
from tensorflow.keras import backend as K
i1 = Input(shape=(200,), dtype='int32')
i2 = Input(shape=(200,), dtype='int32')
x1 = Embedding(len(embeddings_matrix_creative_id),200,weights=[embeddings_matrix_creative_id],trainable=False)(i1)
x2 = Embedding(len(embeddings_matrix_advertiser_id),150,weights=[embeddings_matrix_advertiser_id],trainable=False)(i2)
x1 = Bidirectional(GRU(100, return_sequences=True))(x1)
# x1 = Attention()([x1,x1])
x2 = Bidirectional(GRU(100, return_sequences=True))(x2)
# x2 = Attention()([x2,x2])
# att = Attention()([gru,gru])
e = Dot(axes=2)([x1, x2])
e1 = Softmax(axis=2)(e)
e2 = Softmax(axis=1)(e)
e1 = Lambda(K.expand_dims, arguments={'axis' : 3})(e1)
e2 = Lambda(K.expand_dims, arguments={'axis