SoFunction
Updated on 2024-11-15

keras solves error loading lstm+crf models

Error Display

new_model = load_model(“model.h5”)

Report an error:

1、keras load_model valueError: Unknown Layer :CRF

2、keras load_model valueError: Unknown loss function:crf_loss

bug fix

1、load_modelModify the source code:custom_objects = None change into def load_model(filepath, custom_objects, compile=True):

2、new_model = load_model(“model.h5”,custom_objects={‘CRF': CRF,‘crf_loss': crf_loss,‘crf_viterbi_accuracy': crf_viterbi_accuracy}

After the above modifications, it is ready to run.

Additional knowledge:Building bilstm crf with keras

utilization/keras-team/keras-contribrealization of the crf layer.

Install keras-contrib

pip install git+/keras-team/

Code Example:

# coding: utf-8
from  import Sequential
from  import Embedding
from  import LSTM
from  import Bidirectional
from  import Dense
from  import TimeDistributed
from  import Dropout
from keras_contrib. import CRF
from keras_contrib.utils import save_load_utils

VOCAB_SIZE = 2500
EMBEDDING_OUT_DIM = 128
TIME_STAMPS = 100
HIDDEN_UNITS = 200
DROPOUT_RATE = 0.3
NUM_CLASS = 5

def build_embedding_bilstm2_crf_model():
 """
 Bidirectional LSTM with embedding + crf
 """
 model = Sequential()
 (Embedding(VOCAB_SIZE, output_dim=EMBEDDING_OUT_DIM, input_length=TIME_STAMPS))
 (Bidirectional(LSTM(HIDDEN_UNITS, return_sequences=True)))
 (Dropout(DROPOUT_RATE))
 (Bidirectional(LSTM(HIDDEN_UNITS, return_sequences=True)))
 (Dropout(DROPOUT_RATE))
 (TimeDistributed(Dense(NUM_CLASS)))
 crf_layer = CRF(NUM_CLASS)
 (crf_layer)
 ('rmsprop', loss=crf_layer.loss_function, metrics=[crf_layer.accuracy])
 return model

def save_embedding_bilstm2_crf_model(model, filename):
 save_load_utils.save_all_weights(model,filename)

def load_embedding_bilstm2_crf_model(filename):
 model = build_embedding_bilstm2_crf_model()
 save_load_utils.load_all_weights(model, filename)
 return model

if __name__ == '__main__':
 model = build_embedding_bilstm2_crf_model()

Attention:

If executing the build model reports an error, it is most likely a keras version issue. With keras-contrib==2.0.8 and keras==2.0.8, the above code will not report an error.

Above this keras to solve the problem of loading lstm+crf model error is all I have shared with you, I hope to give you a reference, and I hope you support me more.