您好,登錄后才能下訂單哦!
這篇文章主要介紹使用keras如何實現BiLSTM+CNN+CRF文字標記NER,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
我就廢話不多說了,大家還是直接看代碼吧~
import keras from sklearn.model_selection import train_test_split import tensorflow as tf from keras.callbacks import ModelCheckpoint,Callback # import keras.backend as K from keras.layers import * from keras.models import Model from keras.optimizers import SGD, RMSprop, Adagrad,Adam from keras.models import * from keras.metrics import * from keras import backend as K from keras.regularizers import * from keras.metrics import categorical_accuracy # from keras.regularizers import activity_l1 #通過L1正則項,使得輸出更加稀疏 from keras_contrib.layers import CRF from visual_callbacks import AccLossPlotter plotter = AccLossPlotter(graphs=['acc', 'loss'], save_graph=True, save_graph_path=sys.path[0]) # from crf import CRFLayer,create_custom_objects class LossHistory(Callback): def on_train_begin(self, logs={}): self.losses = [] def on_batch_end(self, batch, logs={}): self.losses.append(logs.get('loss')) # def on_epoch_end(self, epoch, logs=None): word_input = Input(shape=(max_len,), dtype='int32', name='word_input') word_emb = Embedding(len(char_value_dict)+2, output_dim=64, input_length=max_len, dropout=0.2, name='word_emb')(word_input) bilstm = Bidirectional(LSTM(32, dropout_W=0.1, dropout_U=0.1, return_sequences=True))(word_emb) bilstm_d = Dropout(0.1)(bilstm) half_window_size = 2 paddinglayer = ZeroPadding1D(padding=half_window_size)(word_emb) conv = Conv1D(nb_filter=50, filter_length=(2 * half_window_size + 1), border_mode='valid')(paddinglayer) conv_d = Dropout(0.1)(conv) dense_conv = TimeDistributed(Dense(50))(conv_d) rnn_cnn_merge = merge([bilstm_d, dense_conv], mode='concat', concat_axis=2) dense = TimeDistributed(Dense(class_label_count))(rnn_cnn_merge) crf = CRF(class_label_count, sparse_target=False) crf_output = crf(dense) model = Model(input=[word_input], output=[crf_output]) model.compile(loss=crf.loss_function, optimizer='adam', metrics=[crf.accuracy]) model.summary() # serialize model to JSON model_json = model.to_json() with open("model.json", "w") as json_file: json_file.write(model_json) #編譯模型 # model.compile(loss='categorical_crossentropy', optimizer=adam, metrics=['acc',]) # 用于保存驗證集誤差最小的參數,當驗證集誤差減少時,立馬保存下來 checkpointer = ModelCheckpoint(filepath="bilstm_1102_k205_tf130.w", verbose=0, save_best_only=True, save_weights_only=True) #save_weights_only=True history = LossHistory() history = model.fit(x_train, y_train, batch_size=32, epochs=500,#validation_data = ([x_test, seq_lens_test], y_test), callbacks=[checkpointer, history, plotter], verbose=1, validation_split=0.1, )
補充知識:keras訓練模型使用自定義CTC損失函數,重載模型時報錯解決辦法
使用keras訓練模型,用到了ctc損失函數,需要自定義損失函數如下:
self.ctc_model.compile(loss={'ctc': lambda y_true, output: output}, optimizer=opt)
其中loss為自定義函數,使用字典{‘ctc': lambda y_true, output: output}
訓練完模型后需要重載模型,如下:
from keras.models import load_model
model=load_model('final_ctc_model.h6')
報錯:
Unknown loss function : <lambda>
由于是自定義的損失函數需要加參數custom_objects,這里需要定義字典{'': lambda y_true, output: output},正確代碼如下:
model=load_model('final_ctc_model.h6',custom_objects={'<lambda>': lambda y_true, output: output})
可能是因為要將自己定義的loss函數加入到keras函數里
在這之前試了很多次,如果用lambda y_true, output: output定義loss
函數字典名只能是'<lambda>',不能是別的字符
如果自定義一個函數如loss_func作為loss函數如:
self.ctc_model.compile(loss=loss_func, optimizer=opt)
可以在重載時使用
am=load_model('final_ctc_model.h6',custom_objects={'loss_func': loss_func})
此時注意字典名和函數名要相同
以上是使用keras如何實現BiLSTM+CNN+CRF文字標記NER的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。