整理:esim、transformer加lstm和textcnn多分类模型(tf2)

注意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
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值