keras-contrib icon indicating copy to clipboard operation
keras-contrib copied to clipboard

How to load the customized layer and loss function.

Open doggeral opened this issue 6 years ago • 4 comments

Hi, I'm training the LSTM-CRF model for NER. I used the keras_contrib.layers import CRF as the CRF layer. Once I finished the training, I saved the model by model.save('testing_new_all_trainning_words.h5'). But when I tried to load the model by

from keras.models import load_model  
model = load_model('testing_new_all_trainning_words.h5')

I got exception: "ValueError: Unknown layer: CRF"

doggeral avatar May 18 '18 18:05 doggeral

Required, as usual

from keras.models import load_model

Recommended method; requires knowledge of the underlying architecture of the model

from keras_contrib.layers.advanced_activations import PELU

Not recommended; however this will correctly find the necessary contrib modules

from keras_contrib import *

Load our model

model = load_model('example.h5')

Do try something like above , please refer to the readme file (Front page - last part)

anand-sonawane avatar May 20 '18 20:05 anand-sonawane

@anand-sonawane It didn't work. I still get the same error. My keras version is 2.1.6

doggeral avatar May 25 '18 18:05 doggeral

@doggeral Have you solved this issue? I have the same problem now.

buptcjj avatar Oct 24 '18 07:10 buptcjj

from keras_contrib.layers import CRF
from keras_contrib.losses import  crf_loss
from keras_contrib.metrics import crf_viterbi_accuracy

model= load_model('your_model.h5',custom_objects={'CRF':CRF, 
                                                  'crf_loss':crf_loss, 
                                                  'crf_viterbi_accuracy':crf_viterbi_accuracy})

Kongkukongku avatar May 15 '19 01:05 Kongkukongku