Skip to content

Commit 5126f82

Browse files
committed
[ehealth] modify NER code according to comments
1 parent ff02709 commit 5126f82

File tree

5 files changed

+23
-17
lines changed

5 files changed

+23
-17
lines changed

examples/biomedical/cblue/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@
3232
```text
3333
cblue/
3434
├── README.md # 使用说明
35-
└── train_classification.py # 分类任务训练评估脚本
35+
├── train_classification.py # 分类任务训练评估脚本
36+
├── train_ner.py # 实体识别任务训练评估脚本
37+
└── train_spo.py # 关系抽取任务训练评估脚本
3638
```
3739

3840
### 模型训练

examples/biomedical/cblue/model.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ class ElectraForBinaryTokenClassification(ElectraPretrainedModel):
1313
An instance of ElectraModel.
1414
num_classes (list):
1515
The number of classes.
16-
use_crf (bool, optional):
17-
Use conditional random fields for named entity recognition.
18-
Defaults to False.
1916
dropout (float, optionl):
2017
The dropout probability for output of Electra.
2118
If None, use the same value as `hidden_dropout_prob' of 'ElectraModel`
@@ -41,11 +38,6 @@ def forward(self,
4138
token_type_ids=None,
4239
position_ids=None,
4340
attention_mask=None):
44-
r"""
45-
The ElectraForMedicalClassification forward method, overrides the __call__() special method.
46-
47-
TODO
48-
"""
4941
sequence_output = self.electra(input_ids, token_type_ids, position_ids,
5042
attention_mask)
5143
sequence_output = self.dropout(sequence_output)
@@ -56,6 +48,10 @@ def forward(self,
5648

5749

5850
class MultiHeadAttentionForSPO(nn.Layer):
51+
"""
52+
Multi-head attention layer for SPO task.
53+
"""
54+
5955
def __init__(self, embed_dim, num_heads, scale_value=768):
6056
super(MultiHeadAttentionForSPO, self).__init__()
6157
self.embed_dim = embed_dim
@@ -78,6 +74,19 @@ def forward(self, query, key):
7874

7975
class ElectraForSPO(ElectraPretrainedModel):
8076
"""
77+
Electra Model with a linear layer on top of the hidden-states output
78+
layers for entity recognition, and a multi-head attention layer for
79+
relation classification.
80+
81+
Args:
82+
electra (:class:`ElectraModel`):
83+
An instance of ElectraModel.
84+
num_classes (int):
85+
The number of classes.
86+
dropout (float, optionl):
87+
The dropout probability for output of Electra.
88+
If None, use the same value as `hidden_dropout_prob' of 'ElectraModel`
89+
instance `electra`. Defaults to None.
8190
"""
8291

8392
def __init__(self, electra, num_classes, dropout=None):

examples/biomedical/cblue/train_ner.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929
parser.add_argument('--warmup_proportion', default=0.1, type=float, help='Linear warmup proportion over the training process.')
3030
parser.add_argument('--use_amp', default=False, type=bool, help='Enable mixed precision training.')
3131
parser.add_argument('--epochs', default=1, type=int, help='Total number of training epochs.')
32-
parser.add_argument('--eval_mention', default=True, type=bool, help='.')
33-
parser.add_argument('--update_tokenizer', default=True, type=bool, help='Update the word tokenizer during training.')
3432
parser.add_argument('--seed', default=1000, type=int, help='Random seed.')
3533
parser.add_argument('--save_dir', default='./checkpoint', type=str, help='The output directory where the model checkpoints will be written.')
3634

@@ -105,7 +103,7 @@ def do_train():
105103
'input_ids': Pad(axis=0, pad_val=tokenizer.pad_token_id, dtype='int64'),
106104
'token_type_ids': Pad(axis=0, pad_val=tokenizer.pad_token_type_id, dtype='int64'),
107105
'position_ids': Pad(axis=0, pad_val=tokenizer.pad_token_id, dtype='int64'),
108-
'mask': Pad(axis=0, pad_val=0, dtype='float32'),
106+
'attention_mask': Pad(axis=0, pad_val=0, dtype='float32'),
109107
'label_oth': Pad(axis=0, pad_val=pad_label_id[0], dtype='int64'),
110108
'label_sym': Pad(axis=0, pad_val=pad_label_id[1], dtype='int64')
111109
}): fn(samples)
@@ -164,8 +162,6 @@ def do_train():
164162
with paddle.amp.auto_cast(
165163
args.use_amp,
166164
custom_white_list=['layer_norm', 'softmax', 'gelu'], ):
167-
att_mask = paddle.unsqueeze(masks, axis=2)
168-
att_mask = paddle.matmul(att_mask, att_mask, transpose_y=True)
169165
logits = model(input_ids, token_type_ids, position_ids, masks)
170166

171167
loss_oth = criterion(logits[0], paddle.unsqueeze(label_oth, 2))

examples/biomedical/cblue/utils.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,8 @@ def convert_example_ner(example,
114114
text=text,
115115
max_seq_len=max_seq_length,
116116
return_position_ids=True,
117-
return_length=True)
117+
return_attention_mask=True)
118118
input_len = len(encoded_inputs['input_ids'])
119-
encoded_inputs['mask'] = np.ones(input_len)
120119

121120
if example.get('labels', None):
122121
labels = example['labels']

paddlenlp/transformers/electra/modeling.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ def _init_weights(self, layer):
386386
elif isinstance(layer, nn.LayerNorm):
387387
layer.bias.set_value(paddle.zeros_like(layer.bias))
388388
layer.weight.set_value(paddle.full_like(layer.weight, 1.0))
389-
layer._epsilon = getattr(self, 'layer_norm_eps', 1e-12)
389+
layer._epsilon = getattr(self, "layer_norm_eps", 1e-12)
390390
if isinstance(layer, nn.Linear) and layer.bias is not None:
391391
layer.bias.set_value(paddle.zeros_like(layer.bias))
392392

0 commit comments

Comments
 (0)