File tree Expand file tree Collapse file tree 2 files changed +17
-11
lines changed
plugins/hanlp_demo/hanlp_demo/zh/train Expand file tree Collapse file tree 2 files changed +17
-11
lines changed Original file line number Diff line number Diff line change @@ -246,12 +246,13 @@ def fit(self,
246
246
first_device = - 1
247
247
if _device_placeholder and first_device >= 0 :
248
248
_dummy_placeholder = self ._create_dummy_placeholder_on (first_device )
249
- if finetune :
250
- if isinstance (finetune , str ):
251
- self .load (finetune , devices = devices , ** self .config )
252
- else :
253
- self .load (save_dir , devices = devices , ** self .config )
254
- self .config .finetune = finetune
249
+ if finetune or self .model :
250
+ if not self .model :
251
+ if isinstance (finetune , str ):
252
+ self .load (finetune , devices = devices , ** self .config )
253
+ else :
254
+ self .load (save_dir , devices = devices , ** self .config )
255
+ self .config .finetune = finetune or True
255
256
self .vocabs .unlock () # For extending vocabs
256
257
logger .info (
257
258
f'Finetune model loaded with { sum (p .numel () for p in self .model .parameters () if p .requires_grad )} '
Original file line number Diff line number Diff line change 9
9
10
10
cdroot ()
11
11
12
+ # 0. Prepare your dataset for finetuning
12
13
your_training_corpus = 'data/ner/finetune/word_to_iobes.tsv'
13
14
your_development_corpus = your_training_corpus # Use a different one in reality
14
15
save_dir = 'data/ner/finetune/model'
25
26
'''
26
27
)
27
28
29
+ # 1. Load a pretrained model for finetuning
28
30
ner = TransformerNamedEntityRecognizer ()
31
+ ner .load (hanlp .pretrained .ner .MSRA_NER_ELECTRA_SMALL_ZH )
32
+
33
+ # 2. Override hyper-parameters
34
+ ner .config ['epochs' ] = 50 # Since the corpus is small, overfit it
35
+
36
+ # 3. Fit on your dataset
29
37
ner .fit (
30
38
trn_data = your_training_corpus ,
31
39
dev_data = your_development_corpus ,
32
40
save_dir = save_dir ,
33
- epochs = 50 , # Since the corpus is small, overfit it
34
- finetune = hanlp .pretrained .ner .MSRA_NER_ELECTRA_SMALL_ZH ,
35
- # You MUST set the same parameters with the fine-tuning model:
36
- average_subwords = True ,
37
- transformer = 'hfl/chinese-electra-180g-small-discriminator' ,
41
+ ** ner .config
38
42
)
39
43
44
+ # 4. Test it out on your data points
40
45
HanLP = hanlp .pipeline ()\
41
46
.append (hanlp .load (hanlp .pretrained .tok .FINE_ELECTRA_SMALL_ZH ), output_key = 'tok' )\
42
47
.append (ner , output_key = 'ner' )
You can’t perform that action at this time.
0 commit comments