創新互聯www.cdcxhl.cn八線動態BGP香港云服務器提供商,新人活動買多久送多久,劃算不套路!
成都創新互聯堅持“要么做到,要么別承諾”的工作理念,服務領域包括:網站設計制作、網站設計、企業官網、英文網站、手機端網站、網站推廣等服務,滿足客戶于互聯網時代的寶坻網站設計、移動媒體設計的需求,幫助企業找到有效的互聯網解決方案。努力成為您成熟可靠的網絡建設合作伙伴!這篇文章主要介紹使用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如何實現BiLSTM+CNN+CRF文字標記NER-創新互聯
標題網址:http://vcdvsql.cn/article2/ddhpic.html
成都網站建設公司_創新互聯,為您提供微信公眾號、企業網站制作、網站導航、面包屑導航、營銷型網站建設、定制開發
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯