BERT output for NER only (or mostly) predicting '0' label

dmandair: <p>Hi everyone! I’d really appreciate your help with an issue I’m having with BERT for NER in a highly specialized domain. I posted on hugginface as well but figured this might also be a great place for help as I’m not so sure my issue is related to the BERT model itself. I’m using BERT for token classification and used much of the format from here (<a href="https://www.depends-on-the-definition.com/named-entity-recognition-with-bert/" rel="noopener nofollow ugc">Named entity recognition with Bert</a>) as inspiration. I had to generate my own B/I labeling for my dataset, which looks fine - some samples are below. Text lengths were also often longer than BERT max tokens of 512 so I had a chunk a given text piece into smaller chunks (of which at the start I’d place a [CLS] token. I didn’t really add [SEP] tokens on any of these chunked pieces as they’d have padding to reach the length of 512. The labels, input id’s, padding and attention masks appear fine and I’ve also pasted some samples below. I’ve also checked the output numerous times - again my code is below for the predict function. I had to aggregate the byte level predictions to come up with labels at the original word level and did this by averaging over the byte level logit outputs if a word was split by the tokenzier (this is done by the aggregate function). Another idiosyncratic part of my predict function is I had to know indices in the original text that correspond to tokens - the first part of the predict function is just storing those values for the original ‘words’. What’s weird is if I train the BERT model on a handful of data samples (for instance, &lt;10), I’ll get some output that has none ‘0’ predictions. Training over a larger set seems to result in only ‘0’ predictions. Really don’t understand what’s going on at all and would appreciate any help - thank you so much!</p>
<p>[… ‘P’, ‘#<span class="hashtag">#H</span>’, ‘#<span class="hashtag">#Y</span>’, ‘#<span class="hashtag">#SI</span>’, ‘#<span class="hashtag">#CI</span>’, ‘#<span class="hashtag">#AN</span>’, ‘IN’, ‘#<span class="hashtag">#TE</span>’, ‘#<span class="hashtag">#RP</span>’, ‘#<span class="hashtag">#RE</span>’, ‘#<span class="hashtag">#TA</span>’, ‘#<span class="hashtag">#TI</span>’, ‘#<span class="hashtag">#ON</span>’, ‘:’, ‘N’, ‘#<span class="hashtag">#eg</span>’, ‘#<span class="hashtag">#ative</span>’, ‘:’, ‘Tu’, ‘#<span class="hashtag">#mor</span>’, ‘cells’, ‘showing’, ‘no’]</p>
<p>[…‘0’, ‘0’, ‘0’, ‘0’, ‘0’, ‘0’, ‘B-HER2’, ‘B-HER2’, ‘B-HER2’, ‘B-HER2’, ‘I-HER2’, ‘I-HER2’, ‘I-HER2’, ‘I-HER2’, ‘I-HER2’]</p>
<pre><code class="lang-auto">def predict(test_texts, test_note_ids, model, tokenizer):
    offset = 0
    model.eval()
    for test_text, test_note_id in zip(test_texts, test_note_ids):
        num_notes += 1
        chunkified_tokenized_texts, _, _ = chunkify_note(test_text, None, test_note_id)
        predictions = []
        for split_text in chunkified_tokenized_texts:
            tokenized_test_text = tokenizer.encode(split_text)
            tok_list = []
            for m in re.finditer(r'\S+', split_text):
                token = m.group(0)
                sub_tokens = tokenizer.tokenize(token)
                tok_list.append((token, offset+m.start(), offset+m.end() - 1))
            offset = len(split_text)

            if torch.cuda.is_available():
                input_ids = torch.tensor([tokenized_test_text]).cuda()
            else:
                input_ids = torch.tensor([tokenized_test_text])
            with torch.no_grad():
                output = model(input_ids)
            if torch.cuda.is_available():
                scores = output[0].detach().cpu().numpy()
            else:
                scores = output[0].detach().numpy()

            pred = aggregate(scores, tokenizer, tok_list)

            predictions.extend(pred) #then do some extra processing to store this
</code></pre>
<p>Input Id’s on the input:<br>
[ 101 1239 1104 115 115 115 115 115 117 115 115 115<br>
115 115 115 115 115 115 115 4183 2524 115 115 115<br>
115 115 115 115 115 115 115 115 115 115 115 115<br>
117 115 115 115 115 115 115 115 115 115 115 115<br>
115 115 115 115 115 115 115 115 115 117 9292 117<br>
7735 115 115 115 115 115 115 115 115 115 115 117<br>
115 115 115 115 115 115 115 115 115 115 118 115<br>
115 115 115 115 115 115 115 115 115 115 115 115<br>
115 115 2524 11341 131 113 115 115 115 115 115 114<br>
115 115 115 115 115 118 115 115 115 115 115 115<br>
115 115 115 115 115 115 115 115 115 119 115 115<br>
115 115 115 117 9292 117 7735 143 7897 131 113 115<br>
115 115 115 115 114 115 115 115 115 115 118 115<br>
115 115 115 115 9666 9970 18653 11922 131 115 115 115<br>
115 115 137 115 115 115 115 115 119 115 115 115<br>
115 115 115 115 115 115 115 115 115 115 115 115<br>
115 115 115 115 115 117 7735 115 115 115 115 115<br>
115 115 115 115 115 117 9292 117 7735 150 13901 8231<br>
2591 10783 2069 8544 24162 13901 2346 2349 3663 155 16668 9565<br>
1942 7195 9080 10208 131 115 115 115 115 115 117 115<br>
115 115 115 115 115 115 115 115 115 108 131 115<br>
115 115 115 115 118 115 115 115 115 115 2508 1181<br>
119 11336 1665 119 108 131 115 115 115 115 115 159<br>
26868 1204 108 131 115 115 115 115 115 2516 14265 131<br>
1429 120 1407 120 1446 26316 131 1492 2162 9850 131 9714<br>
11336 21437 131 1367 120 5037 120 1446 140 19526 131 115<br>
115 115 115 115 141 2346 2064 131 1429 120 1429 120<br>
2679 113 4936 131 4062 114 7642 6834 27989 1389 113 188<br>
114 131 115 115 115 115 115 115 115 115 115 115<br>
119 115 115 115 115 115 113 113 115 115 115 115<br>
115 114 115 115 115 115 115 118 115 115 115 115<br>
115 114 115 115 115 115 115 115 115 115 115 115<br>
113 113 115 115 115 115 115 114 115 115 115 115<br>
115 118 115 115 115 115 115 114 2687 7136 6902 131<br>
15075 10584 1179 118 4275 117 18311 16274 118 11783 7918 1113<br>
2525 15766 119 115 115 115 115 115 118 115 115 115<br>
115 115 115 115 115 115 115 131 4114 7209 15961 145<br>
1775 131 115 115 115 115 115 119 5539 1580 115 115<br>
115 115 115 115 115 115 115 115 7277 1643 17489 27259<br>
1118 143 6258 3048 10722 23904 9664 18082 2069 3663 153 3048<br>
3663 13882 19747 14962 15969 12880 20336 16941 9159 21669 11414 131<br>
151 12606 5838 131 17037 26271 3652 4000 1185 0 0 0<br>
0 0 0 0 0 0 0 0]</p>
<p>Tagged values:<br>
[ 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9<br>
9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9<br>
9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9<br>
9 9 9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