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
?著作權歸作者所有,轉載或內容合作請聯系作者
- 文/潘曉璐 我一進店門谈况,熙熙樓的掌柜王于貴愁眉苦臉地迎上來勺美,“玉大人递胧,你說我怎么就攤上這事∩娜祝” “怎么了缎脾?”我有些...
- 文/花漫 我一把揭開白布譬涡。 她就那樣靜靜地躺著闪幽,像睡著了一般。 火紅的嫁衣襯著肌膚如雪涡匀。 梳的紋絲不亂的頭發(fā)上盯腌,一...
- 文/蒼蘭香墨 我猛地睜開眼蒿囤,長吁一口氣:“原來是場噩夢啊……” “哼客们!你這毒婦竟也來了?” 一聲冷哼從身側響起材诽,我...
- 正文 年R本政府宣布,位于F島的核電站菲语,受9級特大地震影響妄辩,放射性物質發(fā)生泄漏。R本人自食惡果不足惜山上,卻給世界環(huán)境...
- 文/蒙蒙 一眼耀、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧佩憾,春花似錦哮伟、人聲如沸潭辈。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽把敢。三九已至,卻和暖如春谅辣,著一層夾襖步出監(jiān)牢的瞬間修赞,已是汗流浹背。 一陣腳步聲響...
推薦閱讀更多精彩內容
- 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...