Example #1
0
def build_model(config_path,
                checkpoint_path,
                max_seq_length,
                bert_trainable=False):
    in_id = Input(shape=(max_seq_length, ), name="input_ids", dtype="int32")
    in_segment = Input(shape=(max_seq_length, ),
                       name="segment_ids",
                       dtype="int32")

    bert_model = load_pretrained_model(config_path,
                                       checkpoint_path)  # 建立模型,加载权重

    for l in bert_model.layers:
        if bert_trainable:
            l.trainable = True
        else:
            l.trainable = False

    sequence_output = bert_model([in_id, in_segment])
    pooled_output = Lambda(lambda x: x[:, 0])(sequence_output)
    pred = Dense(1, activation="sigmoid")(pooled_output)

    model = Model(inputs=[in_id, in_segment], outputs=pred)
    model.compile(loss="binary_crossentropy",
                  optimizer=Adam(lr=0.001),
                  metrics=["accuracy"])
    model.summary(line_length=150)

    return model
Example #2
0
def build_model(config_path,
                checkpoint_path,
                max_seq_length,
                label_num,
                bert_trainable=False):
    in_id = Input(shape=(max_seq_length, ), name="input_ids", dtype="int32")
    in_segment = Input(shape=(max_seq_length, ),
                       name="segment_ids",
                       dtype="int32")

    bert_model = load_pretrained_model(config_path,
                                       checkpoint_path)  # 建立模型,加载权重

    for l in bert_model.layers:
        if bert_trainable:
            l.trainable = True
        else:
            l.trainable = False

    sequence_output = bert_model([in_id, in_segment])
    bilstm_output = Bidirectional(CuDNNLSTM(
        128, return_sequences=True))(sequence_output)

    layer_dense = Dense(64, activation='tanh', name='layer_dense')
    layer_crf_dense = Dense(label_num, name='layer_crf_dense')
    layer_crf = CRF(label_num, name='layer_crf')

    dense = layer_dense(bilstm_output)
    dense = layer_crf_dense(dense)
    pred = layer_crf(dense)

    model = Model(inputs=[in_id, in_segment], outputs=pred)
    model.compile(loss=layer_crf.loss,
                  optimizer=Adam(lr=1e-5),
                  metrics=[layer_crf.viterbi_accuracy])

    model.summary(line_length=150)

    return model
Example #3
0
    K.set_session(session)

    config_path = "/home/new/Toxicity/bert_model/models/chinese_L-12_H-768_A-12/bert_config.json"
    checkpoint_path = "/home/new/Toxicity/bert_model/models/chinese_L-12_H-768_A-12/bert_model.ckpt"
    vocab_file = "/home/new/Toxicity/bert_model/models/chinese_L-12_H-768_A-12/vocab.txt"

    max_seq_length = 128

    maxlen = 100
    config_path = '/home/new/Toxicity/bert_model/models/chinese_L-12_H-768_A-12/bert_config.json'
    checkpoint_path = '/home/new/Toxicity/bert_model/models/chinese_L-12_H-768_A-12/bert_model.ckpt'
    dict_path = '/home/new/Toxicity/bert_model/models/chinese_L-12_H-768_A-12/vocab.txt'

    token_dict = load_vocab(dict_path)  # 读取词典
    tokenizer = SimpleTokenizer(token_dict)  # 建立分词器
    bert_model = load_pretrained_model(config_path, checkpoint_path)  # 建立模型,加载权重

    # 编码测试
    token_ids, segment_ids = tokenizer.encode(u'语言模型')
    print(bert_model.predict([np.array([token_ids]), np.array([segment_ids])]).shape)

    # save_h5_model(model)
    # tf.keras.experimental.export_saved_model(model, saved_model_path)

"""

train examles:
0	选择珠江花园的原因就是方便,有电动扶梯直接到达海边,周围餐馆、食廊、商场、超市、摊位一应俱全。酒店装修一般,但还算整洁。 泳池在大堂的屋顶,因此很小,不过女儿倒是喜欢。 包的早餐是西式的,还算丰富。 服务吗,一般	None	1
1	15.4寸笔记本的键盘确实爽,基本跟台式机差不多了,蛮喜欢数字小键盘,输数字特方便,样子也很美观,做工也相当不错	None	1
2	房间太小。其他的都一般。。。。。。。。。	None	0
3	1.接电源没有几分钟,电源适配器热的不行. 2.摄像头用不起来. 3.机盖的钢琴漆,手不能摸,一摸一个印. 4.硬盘分区不好办.	None	0