tf2crf
tf2crf copied to clipboard
How to fit a model with two types of tagging(output)?
Hello,
I am trying to implement a NER where for an input I would like to receive two outputs. I have multiple questions for the same. I am using tensorflow 2.5.0, tensorflow_addons 0.14.0 and python 3.8.10.
- This is arising some assertion error in ModelWithCRFLoss. My current code snippet
inp = Input(shape=(max_len,))
model = Embedding(input_dim=n_chars + 1, output_dim=16, input_length=max_len, mask_zero=True)(inp)
bi_lstm = Bidirectional(LSTM(units=16, return_sequences=True, recurrent_dropout=0.2))(model)
crf_0 = CRF(n_tags[0]+1)
crf_1 = CRF(n_tags[1]+1)
out_0 = crf_0(bi_lstm)
out_1 = crf_1(bi_lstm)
base_model = Model(inp, [out_0,out_1])
#loss is crf loss, accuracy is crf accuracy
model = ModelWithCRFLoss(base_model, sparse_target=True)
model.compile(optimizer="rmsprop")
history = model.fit(X_tr, [y_tr_0, y_tr_1], batch_size=my_batch_size, epochs=my_epochs, validation_split=0.1, verbose=1)
- I also tried training the model using single output which works. But, while trying to save the model in a json format as described in a closed issue I am getting a NotImplementedError from get_config() as the ModelWithCRFLoss do not have a get_config(). What instead I tried is to save the architecture of the 'base_model' and weights of the 'model'. Then while loading the model I am getting a 'deserialization error'
- Is it possible to not use the ModelWithCRFLoss and use crf loss directly from CRF?I tried to follow a closed old issue, but the CRF object do not have a loss function any more.
I am sorry that currently I am not able to test this problem. You can try implement a get_config() function according to this tensorflow tutorial. It is simple.