File size: 4,329 Bytes
8a48a42
 
 
 
 
 
 
 
 
 
 
 
3ce1634
23e8695
ac57c7b
23e8695
3ce1634
 
2da6076
 
 
 
 
 
 
 
 
 
 
3ce1634
 
 
61b749b
2da6076
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61b749b
 
 
2da6076
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
---
language:
- en
- zh
tags:
- translation
license: apache-2.0
datasets:
- DDDSSS/en-zh-dataset
metrics:
- bleu
- sacrebleu
---
该模型主要的训练数据是opus100和CodeAlpaca_20K中的英文作为翻译内容,采用chatglm作为翻译器翻译成中文,并将脏数据筛选后得到DDDSSS/en-zh-dataset数据集,
缺点是这个模型的sentence len 较短,需要自己进行分句,要不然可能会出现,少翻或者不翻译的情况出现
!注意,如果是pretrain方法下载模型的话,可能部分参数会随机初始化,建议直接下载模型,并从本地读取。


    from transformers import AutoModelForSeq2SeqLM, AutoTokenizer, pipeline
    parser.add_argument('--device', default="cpu", type=str, help='"cuda:1"、"cuda:2"……')
    mode_name = opt.model
    device = opt.device
    model = AutoModelForSeq2SeqLM.from_pretrained(mode_name)
    tokenizer = AutoTokenizer.from_pretrained(mode_name)
    translation = pipeline("translation_en_to_zh", model=model, tokenizer=tokenizer,
                           torch_dtype="float", device_map=True,device=device)
    x=["If nothing is detected and there is a config.json file, it’s assumed the library is transformers.","By looking into the presence of files such as *.nemo or *saved_model.pb*, the Hub can determine if a model is from NeMo or Keras."]
    re = translation(x, max_length=450)
    print('翻译为:' ,re)


微调:

    import numpy as np
    from transformers import AutoModelForSeq2SeqLM, Seq2SeqTrainingArguments, Seq2SeqTrainer
    import torch
    # books =  load_from_disk("")
    books = load_dataset("json", data_files=".json")
    books = books["train"].train_test_split(test_size=0.2)
    checkpoint = "./opus-mt-en-zh"
    # checkpoint = "./model/checkpoint-19304"
    tokenizer = AutoTokenizer.from_pretrained(checkpoint)
    source_lang = "en"
    target_lang = "zh"
    def preprocess_function(examples):
        inputs = [example[source_lang] for example in examples["translation"]]
        targets = [example[target_lang] for example in examples["translation"]]
        model_inputs = tokenizer(inputs, text_target=targets, max_length=512, truncation=True)
        return model_inputs
    tokenized_books = books.map(preprocess_function, batched=True)
    data_collator = DataCollatorForSeq2Seq(tokenizer=tokenizer, model=checkpoint)
    metric = evaluate.load("sacrebleu")
    
    def postprocess_text(preds, labels):
        preds = [pred.strip() for pred in preds]
        labels = [[label.strip()] for label in labels]
        return preds, labels
    
    def compute_metrics(eval_preds):
        preds, labels = eval_preds
        if isinstance(preds, tuple):
            preds = preds[0]
        decoded_preds = tokenizer.batch_decode(preds, skip_special_tokens=True)
    
        labels = np.where(labels != -100, labels, tokenizer.pad_token_id)
        decoded_labels = tokenizer.batch_decode(labels, skip_special_tokens=True)
    
        decoded_preds, decoded_labels = postprocess_text(decoded_preds, decoded_labels)
    
        result = metric.compute(predictions=decoded_preds, references=decoded_labels)
        result = {"bleu": result["score"]}
    
        prediction_lens = [np.count_nonzero(pred != tokenizer.pad_token_id) for pred in preds]
        result["gen_len"] = np.mean(prediction_lens)
        result = {k: round(v, 4) for k, v in result.items()}
        return result
        model = AutoModelForSeq2SeqLM.from_pretrained(checkpoint)
        batchsize=4
        training_args = Seq2SeqTrainingArguments(
    
        output_dir="./my_awesome_opus_books_model",
        evaluation_strategy="epoch",
        learning_rate=2e-4,
        per_device_train_batch_size=batchsize,
        per_device_eval_batch_size=batchsize,
        weight_decay=0.01,
        # save_total_limit=3,
        num_train_epochs=4,
        predict_with_generate=True,
        fp16=True,
        push_to_hub=False,
        save_strategy="epoch",
        jit_mode_eval=True
    )
    
    trainer = Seq2SeqTrainer(
        model=model,
        args=training_args,
        train_dataset=tokenized_books["train"],
        eval_dataset=tokenized_books["test"],
        tokenizer=tokenizer,
        data_collator=data_collator,
        compute_metrics=compute_metrics,
    )
    trainer.train()