You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In feature extraction demo, you should be able to get the same extraction result as the official model. And in prediction demo, the missing word in the sentence could be predicted.
Train & Use
fromkeras_bertimportget_base_dict, get_model, gen_batch_inputs# A toy input examplesentence_pairs= [
[['all', 'work', 'and', 'no', 'play'], ['makes', 'jack', 'a', 'dull', 'boy']],
[['from', 'the', 'day', 'forth'], ['my', 'arm', 'changed']],
[['and', 'a', 'voice', 'echoed'], ['power', 'give', 'me', 'more', 'power']],
]
# Build token dictionarytoken_dict=get_base_dict() # A dict that contains some special tokensforpairsinsentence_pairs:
fortokeninpairs[0] +pairs[1]:
iftokennotintoken_dict:
token_dict[token] =len(token_dict)
token_list=list(token_dict.keys()) # Used for selecting a random word# Build & train the modelmodel=get_model(
token_num=len(token_dict),
head_num=5,
transformer_num=12,
embed_dim=25,
feed_forward_dim=100,
seq_len=20,
pos_num=20,
dropout_rate=0.05,
)
model.summary()
def_generator():
whileTrue:
yieldgen_batch_inputs(
sentence_pairs,
token_dict,
token_list,
seq_len=20,
mask_rate=0.3,
swap_sentence_rate=1.0,
)
model.fit_generator(
generator=_generator(),
steps_per_epoch=1000,
epochs=100,
validation_data=_generator(),
validation_steps=100,
callbacks=[
keras.callbacks.EarlyStopping(monitor='val_loss', patience=5)
],
)
# Use the trained modelinputs, output_layer=get_model( # `output_layer` is the last feature extraction layer (the last transformer)token_num=len(token_dict),
head_num=5,
transformer_num=12,
embed_dim=25,
feed_forward_dim=100,
seq_len=20,
pos_num=20,
dropout_rate=0.05,
training=False, # The input layers and output layer will be returned if `training` is `False`
)