mirror of
https://github.com/modelscope/modelscope.git
synced 2026-09-01 19:49:03 +02:00
support getting labels from dataset in sbert text classification and building dataset from file in chatglm-6b
1.Add getting labels from dataset in "text_classificationfinetune_text_classification.py" to simplify user's operation in flex training. Parameters "--num_labels" and "--labels" were removed in "run_train.sh". 2.In "chatglm6b / finetune.py", building dataset from file is necessary to support flex training. Link: https://code.alibaba-inc.com/Ali-MaaS/MaaS-lib/codereview/13382745 * support getting labels from dataset in sbert text classification and building dataset from file in chatglm-6b * support getting labels from dataset in sbert text classification and building dataset from file in chatglm-6b * remove repetitive labels in a concise manner of using set * reserve parameter labels in finetune_text_classification * Merge branch 'master' of http://gitlab.alibaba-inc.com/Ali-MaaS/MaaS-lib reserve parameter labels in finetune_text_classification * Merge branch 'support_text_cls_labels_chatglm_json' reserve parameter labels in finetune_text_classification
This commit is contained in:
@@ -7,7 +7,7 @@ from chatglm_trainer import Seq2SeqTrainer
|
||||
from text_generation_metric import TextGenerationMetric
|
||||
from transformers import DataCollatorForSeq2Seq
|
||||
|
||||
from modelscope import snapshot_download
|
||||
from modelscope import build_dataset_from_file, snapshot_download
|
||||
from modelscope.metainfo import Models
|
||||
from modelscope.models import Model
|
||||
from modelscope.msdatasets import MsDataset
|
||||
@@ -172,14 +172,20 @@ def cfg_modify_fn(cfg):
|
||||
return cfg
|
||||
|
||||
|
||||
train_dataset = MsDataset.load(
|
||||
args.train_dataset_name,
|
||||
subset_name=args.train_subset_name,
|
||||
split=args.train_split)
|
||||
validation_dataset = MsDataset.load(
|
||||
args.val_dataset_name,
|
||||
subset_name=args.val_subset_name,
|
||||
split=args.val_split)
|
||||
if args.dataset_json_file is None:
|
||||
train_dataset = MsDataset.load(
|
||||
args.train_dataset_name,
|
||||
subset_name=args.train_subset_name,
|
||||
split=args.train_split,
|
||||
namespace=args.train_dataset_namespace)
|
||||
validation_dataset = MsDataset.load(
|
||||
args.val_dataset_name,
|
||||
subset_name=args.val_subset_name,
|
||||
split=args.val_split,
|
||||
namespace=args.val_dataset_namespace)
|
||||
else:
|
||||
train_dataset, validation_dataset = build_dataset_from_file(
|
||||
args.dataset_json_file)
|
||||
|
||||
model_dir = snapshot_download(args.model)
|
||||
model_config = read_config(model_dir)
|
||||
|
||||
@@ -8,8 +8,14 @@ from modelscope.trainers import build_trainer
|
||||
|
||||
def set_labels(labels):
|
||||
if isinstance(labels, str):
|
||||
labels = labels.split(',')
|
||||
return {label: id for id, label in enumerate(labels)}
|
||||
label_list = labels.split(',')
|
||||
else:
|
||||
unique_labels = set(labels)
|
||||
label_list = list(unique_labels)
|
||||
label_list.sort()
|
||||
label_list = list(
|
||||
map(lambda x: x if isinstance(x, str) else str(x), label_list))
|
||||
return {label: id for id, label in enumerate(label_list)}
|
||||
|
||||
|
||||
@dataclass(init=False)
|
||||
@@ -52,7 +58,8 @@ class TextClassificationArguments(TrainingArgs):
|
||||
})
|
||||
|
||||
|
||||
config, args = TextClassificationArguments().parse_cli().to_config()
|
||||
training_args = TextClassificationArguments().parse_cli()
|
||||
config, args = training_args.to_config()
|
||||
|
||||
print(config, args)
|
||||
|
||||
@@ -62,6 +69,10 @@ def cfg_modify_fn(cfg):
|
||||
cfg.merge_from_dict(config)
|
||||
else:
|
||||
cfg = config
|
||||
if training_args.labels is None:
|
||||
labels = train_dataset[training_args.label] + validation_dataset[
|
||||
training_args.label]
|
||||
cfg.merge_from_dict({'preprocessor.label2id': set_labels(labels)})
|
||||
cfg.model['num_labels'] = len(cfg.preprocessor.label2id)
|
||||
if cfg.evaluation.period.eval_strategy == 'by_epoch':
|
||||
cfg.evaluation.period.by_epoch = True
|
||||
|
||||
@@ -9,8 +9,6 @@ PYTHONPATH=. python examples/pytorch/text_classification/finetune_text_classific
|
||||
--val_split 'validation' \
|
||||
--first_sequence 'sentence' \
|
||||
--label label \
|
||||
--num_labels 15 \
|
||||
--labels '0,1,2,3,4,5,6,7,8,9,10,11,12,13,14' \
|
||||
--preprocessor 'sen-cls-tokenizer' \
|
||||
--use_model_config True \
|
||||
--max_epochs 1 \
|
||||
|
||||
Reference in New Issue
Block a user