forked from CyberZHG/keras-bert
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
241 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import sys | ||
import codecs | ||
import numpy as np | ||
from keras_bert import load_trained_model_from_checkpoint | ||
|
||
|
||
if len(sys.argv) != 4: | ||
print('python load_model.py CONFIG_PATH CHECKPOINT_PATH DICT_PATH') | ||
|
||
config_path, checkpoint_path, dict_path = tuple(sys.argv[1:]) | ||
|
||
model = load_trained_model_from_checkpoint(config_path, checkpoint_path) | ||
model.summary(line_length=120) | ||
|
||
tokens = ['[CLS]', '语', '言', '模', '型', '[SEP]'] | ||
|
||
token_dict = {} | ||
with codecs.open(dict_path, 'r', 'utf8') as reader: | ||
for line in reader: | ||
token = line.strip() | ||
token_dict[token] = len(token_dict) | ||
|
||
token_input = np.asarray([[token_dict[token] for token in tokens] + [0] * (512 - len(tokens))]) | ||
seg_input = np.asarray([[0] * len(tokens) + [0] * (512 - len(tokens))]) | ||
pos_input = np.asarray([list(range(len(tokens))) + [0] * (512 - len(tokens))]) | ||
|
||
print(token_input[0][:len(tokens)]) | ||
|
||
predicts = model.predict([token_input, seg_input, pos_input])[0] | ||
for i, token in enumerate(tokens): | ||
print(token, predicts[i].tolist()[:5]) | ||
|
||
"""Official outputs: | ||
{ | ||
"linex_index": 0, | ||
"features": [ | ||
{ | ||
"token": "[CLS]", | ||
"layers": [ | ||
{ | ||
"index": -1, | ||
"values": [-0.63251, 0.203023, 0.079366, -0.032843, 0.566809, ...] | ||
} | ||
] | ||
}, | ||
{ | ||
"token": "语", | ||
"layers": [ | ||
{ | ||
"index": -1, | ||
"values": [-0.758835, 0.096518, 1.071875, 0.005038, 0.688799, ...] | ||
} | ||
] | ||
}, | ||
{ | ||
"token": "言", | ||
"layers": [ | ||
{ | ||
"index": -1, | ||
"values": [0.547702, -0.792117, 0.444354, -0.711265, 1.20489, ...] | ||
} | ||
] | ||
}, | ||
{ | ||
"token": "模", | ||
"layers": [ | ||
{ | ||
"index": -1, | ||
"values": [-0.292423, 0.605271, 0.499686, -0.42458, 0.428554, ...] | ||
} | ||
] | ||
}, | ||
{ | ||
"token": "型", | ||
"layers": [ | ||
{ | ||
"index": -1, | ||
"values": [ -0.747346, 0.494315, 0.718516, -0.872353, 0.83496, ...] | ||
} | ||
] | ||
}, | ||
{ | ||
"token": "[SEP]", | ||
"layers": [ | ||
{ | ||
"index": -1, | ||
"values": [-0.874138, -0.216504, 1.338839, -0.105871, 0.39609, ...] | ||
} | ||
] | ||
} | ||
] | ||
} | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
from __future__ import absolute_import | ||
|
||
from .bert import get_model, get_custom_objects, get_base_dict, gen_batch_inputs | ||
from .bert import gelu, get_model, get_custom_objects, get_base_dict, gen_batch_inputs | ||
from .loader import load_trained_model_from_checkpoint |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
from .wrapper import Wrapper | ||
from .inputs import get_inputs | ||
from .embedding import Embeddings | ||
from .embedding import get_embedding | ||
from .masked import Masked | ||
from .extract import Extract |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,99 +1,44 @@ | ||
import keras | ||
import keras.backend as K | ||
from .wrapper import Wrapper | ||
from keras_layer_normalization import LayerNormalization | ||
|
||
|
||
class Embeddings(Wrapper): | ||
def get_embedding(inputs, token_num, pos_num, embed_dim, dropout_rate=0.1): | ||
"""Get embedding layer. | ||
See: https://arxiv.org/pdf/1810.04805.pdf | ||
""" | ||
|
||
def __init__(self, | ||
input_dim, | ||
output_dim, | ||
position_dim=512, | ||
dropout_rate=0.1, | ||
**kwargs): | ||
"""Initialize the layer. | ||
:param input_dim: Number of tokens. | ||
:param output_dim: The dimension of all embedding layers. | ||
:param position_dim: Maximum position. | ||
:param dropout_rate: Dropout rate. | ||
""" | ||
self.supports_masking = True | ||
self.input_dim = input_dim | ||
self.output_dim = output_dim | ||
self.position_dim = position_dim | ||
self.dropout_rate = dropout_rate | ||
super(Embeddings, self).__init__(**kwargs) | ||
|
||
def get_config(self): | ||
config = { | ||
'input_dim': self.input_dim, | ||
'output_dim': self.output_dim, | ||
'position_dim': self.position_dim, | ||
'dropout_rate': self.dropout_rate, | ||
} | ||
base_config = super(Embeddings, self).get_config() | ||
return dict(list(base_config.items()) + list(config.items())) | ||
def compute_output_shape(self, input_shape): | ||
return input_shape[0] + (self.output_dim,) | ||
|
||
def compute_mask(self, inputs, input_mask=None): | ||
return K.not_equal(inputs[0], 0) | ||
|
||
def build(self, input_shape): | ||
self.layers['Embedding-Token'] = keras.layers.Embedding( | ||
input_dim=self.input_dim, | ||
output_dim=self.output_dim, | ||
:param inputs: Input layers. | ||
:param token_num: Number of tokens. | ||
:param pos_num: Maximum position. | ||
:param embed_dim: The dimension of all embedding layers. | ||
:param dropout_rate: Dropout rate. | ||
:return: The merged embedding layer. | ||
""" | ||
embeddings = [ | ||
keras.layers.Embedding( | ||
input_dim=token_num, | ||
output_dim=embed_dim, | ||
mask_zero=True, | ||
trainable=self.trainable, | ||
name='Embedding-Token', | ||
) | ||
self.layers['Embedding-Segment'] = keras.layers.Embedding( | ||
)(inputs[0]), | ||
keras.layers.Embedding( | ||
input_dim=2, | ||
output_dim=self.output_dim, | ||
trainable=self.trainable, | ||
output_dim=embed_dim, | ||
name='Embedding-Segment', | ||
) | ||
self.layers['Embedding-Position'] = keras.layers.Embedding( | ||
input_dim=self.position_dim, | ||
output_dim=self.output_dim, | ||
trainable=self.trainable, | ||
)(inputs[1]), | ||
keras.layers.Embedding( | ||
input_dim=pos_num, | ||
output_dim=embed_dim, | ||
name='Embedding-Position', | ||
) | ||
self.layers['Dropout-Token'] = keras.layers.Dropout( | ||
rate=self.dropout_rate, | ||
trainable=self.trainable, | ||
name='Dropout-Token', | ||
) | ||
self.layers['Dropout-Segment'] = keras.layers.Dropout( | ||
rate=self.dropout_rate, | ||
trainable=self.trainable, | ||
name='Dropout-Segment', | ||
) | ||
self.layers['Dropout-Position'] = keras.layers.Dropout( | ||
rate=self.dropout_rate, | ||
trainable=self.trainable, | ||
name='Dropout-Position', | ||
) | ||
self.layers['Embedding'] = keras.layers.Add(name='Embedding') | ||
self.layers['Embedding-Dropout'] = keras.layers.Dropout( | ||
rate=self.dropout_rate, | ||
trainable=self.trainable, | ||
)(inputs[2]), | ||
] | ||
embed_layer = keras.layers.Add(name='Embedding')(embeddings) | ||
if dropout_rate > 0.0: | ||
dropout_layer = keras.layers.Dropout( | ||
rate=dropout_rate, | ||
name='Embedding-Dropout', | ||
) | ||
super(Embeddings, self).build(input_shape) | ||
|
||
def call(self, inputs, **kwargs): | ||
input_token, input_segment, input_position = inputs[:3] | ||
dropouts = [ | ||
self.layers['Dropout-Token'](self.layers['Embedding-Token'](input_token)), | ||
self.layers['Dropout-Segment'](self.layers['Embedding-Segment'](input_segment)), | ||
self.layers['Dropout-Position'](self.layers['Embedding-Position'](input_position)), | ||
] | ||
embed_layer = self.layers['Embedding'](dropouts) | ||
return self.layers['Embedding-Dropout'](embed_layer) | ||
)(embed_layer) | ||
else: | ||
dropout_layer = embed_layer | ||
norm_layer = LayerNormalization(name='Embedding-Norm')(dropout_layer) | ||
return norm_layer |
Oops, something went wrong.