<jupyter_start><jupyter_text>STEP 1: Load and Preprocess DataThe CoNLL2003 NER dataset can be downloaded from [here](https://github.com/amaiya/ktrain/tree/master/ktrain/tests/conll2003).<jupyter_code>TDATA = 'data/conll2003/train.txt'
VDATA = 'data/conll2003/valid.txt'
(trn, val, preproc) = text.entities_from_conll2003(TDATA, val_filepath=VDATA)<jupyter_output>Number of sentences:  14041
Number of words in the dataset:  23623
Tags: ['B-MISC', 'I-MISC', 'I-PER', 'B-ORG', 'I-ORG', 'I-LOC', 'O', 'B-PER', 'B-LOC']
Number of Labels:  9
Longest sentence: 113 words<jupyter_text>STEP 2: Define a Model<jupyter_code>model = text.sequence_tagger('bilstm-crf', preproc)
learner = ktrain.get_learner(model, train_data=trn, val_data=val)<jupyter_output><empty_output><jupyter_text>STEP 3: Train and Evaluate Model<jupyter_code>learner.fit(0.001, 5)
learner.validate()<jupyter_output>F1: 87.20
           precision    recall  f1-score   support

      LOC       0.87      0.94      0.91      1837
      ORG       0.82      0.80      0.81      1341
     MISC       0.88      0.78      0.83       922
      PER       0.89      0.91      0.90      1842

micro avg       0.87      0.88      0.87      5942
macro avg       0.87      0.88      0.87      5942<jupyter_text>We can use the `view_top_losses` method to inspect the sentences we're getting the most wrong. Here, we can see our model has trouble with movie titles, which is understandable since it is mixed into a catch-all miscellaneous category.<jupyter_code>learner.view_top_losses(n=1)<jupyter_output>total incorrect: 12
Word            True : (Pred)
==============================
Best           :O     (B-ORG)
known          :O     (O)
for            :O     (O)
appearances    :O     (O)
in             :O     (O)
"              :O     (O)
Ice            :B-MISC (O)
Cold           :I-MISC (O)
in             :I-MISC (O)
Alex           :I-MISC (B-PER)
"              :O     (O)
,              :O     (O)
"              :O     (O)
Lawrence       :B-MISC (B-PER)
of             :I-MISC (I-MISC)
Arabia         :I-MISC (I-LOC)
"              :O     (O)
and            :O     (O)
,              :O     (O)
as             :O     (O)
Cardinal       :O     (B-PER)
Wolsey         :B-PER (I-PER)
,              :O     (O)
in             :O     (O)
"              :O     (O)
Anne           :B-MISC (B-PER)
of             :I-MISC (O)
a              :I-MISC (O)
Thousand       :I-MISC (I-MISC)
Days           :I-MISC (I-MISC)
"              :O     (O)
.              :O     (O)<jupyter_text>Make Predictions on New Data<jupyter_code>predictor = ktrain.get_predictor(learner.model, preproc)
predictor.predict('As of 2019, <NAME> is still the President of the United States.')
predictor.save('/tmp/mypred')
reloaded_predictor = ktrain.load_predictor('/tmp/mypred')
reloaded_predictor.predict('<NAME> is my favorite actor.')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12996889042516419, 'fraction_numerical': 0.045627376425855515, 'mean_word_length': 1.977366255144033, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '337227', 'license': 'MIT', 'n_tokens_mistral': 1165, 'n_tokens_neox': 1055, 'n_words': 258, 'path': 'examples/text/CoNLL2003-BiLSTM_CRF.ipynb', 'repo_name': 'happy-machine/ktrain', 'size': '2904'}