import tensorflow as tf
from sklearn.model_selection import train_test_split
from bert4keras.models import build_transformer_model
from bert4keras.tokenizers import Tokenizer
from bert4keras.backend import keras
tf.config.experimental.set_memory_growth(device=tf.config.experimental.list_physical_devices(device_type='GPU')[0], enable=True)
embed_dim = 64
NEG, batch_size = 20, 128
config_path = 'chinese_roberta_L-6_H-384_A-12/bert_config.json'
checkpoint_path = 'chinese_roberta_L-6_H-384_A-12/bert_model.ckpt'
query_bert = build_transformer_model(config_path, checkpoint_path, return_keras_model=False).model
query_layer = keras.layers.Dropout(0.1)(query_bert.output)
query_layer = keras.layers.Dense(128, activation='relu', kernel_regularizer='l2', name="query_tower")(query_layer)
doc_bert = build_transformer_model(config_path, checkpoint_path, return_keras_model=False).model
for layer in doc_bert.layers:
layer.name = layer.name + str("_doc")
doc_layer = keras.layers.Dropout(0.1)(doc_bert.output)
doc_layer = keras.layers.Dense(128, activation='relu', kernel_regularizer='l2', name="doc_tower")(doc_layer)
output = keras.layers.Dot(axes=1)([query_layer, doc_layer])
# output = tf.keras.layers.Dense(1, activation='sigmoid')(output)
output = keras.layers.Dense(2, activation='softmax')(output)
model = keras.models.Model(query_bert.input+doc_bert.input, output)
model.compile(loss="categorical_crossentropy", metrics=['acc' ], optimizer='RMSprop')
# query tower
query_model = keras.Model(inputs=query_bert.input, outputs=query_layer)
# doc tower
doc_model = keras.Model(inputs=doc_bert.input, outputs=doc_layer)
print("[INFO] training model...")
model.fit(
train_generator.forfit(),
steps_per_epoch=len(train_generator),
epochs=2, verbose=1)
dssm using bert
?著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事?!?“怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- https://arxiv.org/abs/1908.10084[https://arxiv.org/abs/19...
- 目录 - Motivation:论文想要解决的问题 -Methodology:本文提出的方法 - ex...
- 提出了一种用于中文序列标注的词库增强型BERT,通过Lexicon Adapter layer 将外部词库知识融入...
- 本文来源:bert-as-service 博客 Serving Google BERT in Production...
- Spelling Error Correction with Soft-Masked BERT 基于Soft-Ma...