Exemple #1
0
def build_model(bert_path, max_seq_length, label_num):
    in_id = tf.keras.layers.Input(shape=(max_seq_length, ), name="input_ids")
    in_mask = tf.keras.layers.Input(shape=(max_seq_length, ),
                                    name="input_masks")
    in_segment = tf.keras.layers.Input(shape=(max_seq_length, ),
                                       name="segment_ids")
    bert_inputs = [in_id, in_mask, in_segment]

    sequence_output, pooled = BertLayer(bert_path=bert_path,
                                        trainable=False,
                                        n_fine_tune_layers=0,
                                        max_len=max_seq_length,
                                        name="Bert_layer")(bert_inputs)
    bilstm_output = Bidirectional(LSTM(128,
                                       return_sequences=True))(sequence_output)
    dense = tf.keras.layers.Dense(label_num, activation="relu")(bilstm_output)
    crf = CRF(label_num)
    pred = crf(dense)

    model = tf.keras.models.Model(inputs=bert_inputs, outputs=pred)
    model.compile(loss=crf.loss,
                  optimizer="adam",
                  metrics=[crf.viterbi_accuracy])
    model.summary(line_length=150)

    return model
Exemple #2
0
def build_model(bert_path, max_seq_length):
    in_id = tf.keras.layers.Input(shape=(max_seq_length,), name="input_ids")
    in_mask = tf.keras.layers.Input(shape=(max_seq_length,), name="input_masks")
    in_segment = tf.keras.layers.Input(shape=(max_seq_length,), name="segment_ids")
    bert_inputs = [in_id, in_mask, in_segment]

    sequence_output, pooled_output = BertLayer(bert_path=bert_path, trainable=False,
                                               n_fine_tune_layers=0,
                                               max_len=max_seq_length)(bert_inputs)
    dense = tf.keras.layers.Dense(128, activation="relu")(pooled_output)
    pred = tf.keras.layers.Dense(1, activation="sigmoid")(dense)

    model = tf.keras.models.Model(inputs=bert_inputs, outputs=pred)
    model.compile(loss="binary_crossentropy", optimizer="adam", metrics=["accuracy"])
    model.summary()

    return model
Exemple #3
0
def build_model(bert_path, max_seq_length, num_labels):
    in_id = tf.keras.layers.Input(shape=(max_seq_length, ), name="input_ids")
    in_mask = tf.keras.layers.Input(shape=(max_seq_length, ),
                                    name="input_masks")
    in_segment = tf.keras.layers.Input(shape=(max_seq_length, ),
                                       name="segment_ids")
    bert_inputs = [in_id, in_mask, in_segment]

    bert_output = BertLayer(bert_path=bert_path,
                            n_fine_tune_layers=3,
                            pooling="mean")(bert_inputs)
    squad_logits_layer = BertSquadLogitsLayer(name='squad_logits')
    start_logits, end_logits = squad_logits_layer(bert_output)

    model = tf.keras.models.Model(inputs=bert_inputs,
                                  outputs=[in_id, start_logits, end_logits])
    model.compile(loss=CRF.loss,
                  optimizer="adam",
                  metrics=CRF.viterbi_accuracy)
    model.summary()

    return model
Exemple #4
0
import tensorflow as tf
from tensorflow.python.keras import backend as K
from tensorflow.python.keras.models import load_model
from bert_text.layers import BertLayer
from bert_text.dataset import ChnSentiCorp
from bert.tokenization import FullTokenizer
from bert_text.prepare.classification import convert_examples_to_features

K.set_learning_phase(0)

model_path = "/home/CAIL/bert_text/examples/Classify/saved_models/mdoel.h5"
model = load_model(model_path,custom_objects=BertLayer().get_custom_objects())

dataset = ChnSentiCorp()


max_seq_length = 256

vocab_file = "/home/CAIL/bert_text/examples/SequenceLabel/vocab.txt"
tokenizer = FullTokenizer(vocab_file=vocab_file)
test_input_ids, test_input_masks, test_segment_ids, test_labels = convert_examples_to_features(tokenizer,
                                                                                                dataset.get_test_examples(),
                                                                                                max_seq_length=max_seq_length)
test_inputs = [test_input_ids, test_input_masks, test_segment_ids]
score = model.evaluate(test_inputs, test_labels, batch_size = 128)

print(score)

print("Test loss:", score[0]) #loss
print('Test accuracy:', score[1]) #accuracy