1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| import kashgari from kashgari.corpus import SMP2018ECDTCorpus from kashgari.tasks.classification import BiLSTM_Model from kashgari.embeddings import BERTEmbedding from kashgari.callbacks import EvalCallBack from tensorflow.python import keras from kashgari import utils
kashgari.config.use_cudnn_cell = True
train_x, train_y = SMP2018ECDTCorpus.load_data('train') valid_x, valid_y = SMP2018ECDTCorpus.load_data('valid') test_x, test_y = SMP2018ECDTCorpus.load_data('test')
bert_embed = BERTEmbedding('/home/new/Toxicity/bert_model/models/chinese_L-12_H-768_A-12', task=kashgari.CLASSIFICATION, sequence_length=100)
model = BiLSTM_Model(bert_embed) tf_board_callback = keras.callbacks.TensorBoard(log_dir='./logs', update_freq=1000)
eval_callback = EvalCallBack(kash_model=model, valid_x=valid_x, valid_y=valid_y, step=5)
model.fit(train_x, train_y, valid_x, valid_y, batch_size=100, callbacks=[eval_callback, tf_board_callback])
model.evaluate(test_x, test_y)
model.save('saved_classification_model')
loaded_model = kashgari.utils.load_model('saved_classification_model')
loaded_model.predict(test_x[:10])
utils.convert_to_saved_model(model, model_path='saved_model/blstm', version=1)
|