NLP(四十一)使用HuggingFace翻译模型的一次尝试

  本文将如何如何使用HuggingFace中的翻译模型。
  HuggingFace是NLP领域中响当当的团体,它在预训练模型方面作出了很多接触的工作,并开源了许多预训练模型和已经针对具体某个NLP人物训练好的直接可以使用的模型。本文将使用HuggingFace提供的可直接使用的翻译模型。
  HuggingFace的翻译模型可参考网址:https://huggingface.co/models?pipeline_tag=translation ,该部分模型中的绝大部分是由Helsinki-NLP(Language Technology Research Group at the University of Helsinki)机构开源,模型数量为1333个。

模型使用

  笔者将在PyTorch框架下使用HuggingFace的中译英模型英译中模型。其中中译英模型的模型名称为:opus-mt-zh-en,下载网址为:https://huggingface.co/Helsinki-NLP/opus-mt-zh-en/tree/main英译中模型的模型名称为opus-mt-en-zh,下载网址为:https://huggingface.co/Helsinki-NLP/opus-mt-en-zh/tree/main
  首先我们先尝试中译英模型,即把中文翻译成英语,代码如下:

# -*- coding: utf-8 -*-
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM

tokenizer = AutoTokenizer.from_pretrained("./opus-mt-zh-en")

model = AutoModelForSeq2SeqLM.from_pretrained("./opus-mt-zh-en")

text = "从时间上看,中国空间站的建造比国际空间站晚20多年。"
# Tokenize the text
batch = tokenizer.prepare_seq2seq_batch(src_texts=[text])

# Make sure that the tokenized text does not exceed the maximum
# allowed size of 512
batch["input_ids"] = batch["input_ids"][:, :512]
batch["attention_mask"] = batch["attention_mask"][:, :512]

# Perform the translation and decode the output
translation = model.generate(**batch)
result = tokenizer.batch_decode(translation, skip_special_tokens=True)
print(result)

翻译结果如下:

["In terms of time, the Chinese space station was built more than 20 years later than the International Space Station."]

  接着我们先尝试英译中模型,即把英文翻译成汉语,代码如下:

# -*- coding: utf-8 -*-
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM

tokenizer = AutoTokenizer.from_pretrained("./opus-mt-en-zh")

model = AutoModelForSeq2SeqLM.from_pretrained("./opus-mt-en-zh")

text = "In terms of time, the Chinese space station was built more than 20 years later than the International Space Station."
# Tokenize the text
batch = tokenizer.prepare_seq2seq_batch(src_texts=[text])

# Make sure that the tokenized text does not exceed the maximum
# allowed size of 512
batch["input_ids"] = batch["input_ids"][:, :512]
batch["attention_mask"] = batch["attention_mask"][:, :512]

# Perform the translation and decode the output
translation = model.generate(**batch)
result = tokenizer.batch_decode(translation, skip_special_tokens=True)
print(result)

翻译结果如下:

['就时间而言,中国空间站的建造比国际空间站晚了20多年。']

  有了HuggingFace的transformers模块,我们使用起这些模型相当方便,同时也有很不错的翻译效果。

模型解释

  接着我们再接触一款工具,名称为shapshap是Python开发的一个模型解释包,可以任何机器学习模型的输出。其名称来源于SHapley Additive exPlanation,在合作博弈论的启发下SHAP构建一个加性的解释模型,所有的特征都视为“贡献者”。对于每个预测样本,模型都产生一个预测值,SHAP value就是该样本中每个特征所分配到的数值。
  我们尝试着使用shap模块来对翻译模型进行解释,可以看到shap可视化效果非常棒的解释界面,如下:
中译英模型代码
默认输出结果为翻译结果,如下:

默认输出结果

模型解释的热力图效果如下:

模型解释的热力图效果
从热力图中我们可以发现,空间站这个词语被翻译成space station。
  同样,我们可以看到英译中模型的热力图解释效果,如下:
的热力图解释效果
  本次分享到此结束,感谢大家阅读~
  2021年3月10日于上海浦东~

参考网址

  1. HuggingFace translation model: https://huggingface.co/models?filter=zh&pipeline_tag=translation
  2. Text to Text Explanation: Machine Translation Example: https://shap.readthedocs.io/en/latest/example_notebooks/text_examples/translation/Machine%20Translation%20Explanation%20Demo.html
### 改善Hugging Face上模型的方法 为了提升Hugging Face平台上模型的表现,可以考虑多种策略和技术手段。当一些模型制作者可能会故意使用基准数据训练模型以优化特定指标时[^1],这同样适用于希望提高现有模型性能的研究者或开发者。 #### 数据增强与预处理 增加高质量的数据集规模能够有效改善模型泛化能力。通过引入更多样化的样本,尤其是那些覆盖了目标应用场景中的边缘情况,可以帮助模型更好地学习到不同场景下的特征表示。此外,合理的数据清洗、标准化以及噪声去除也是不可或缺的一环。 #### 超参数调优 超参数的选择对于最终模型的效果有着至关重要的影响。采用网格搜索、随机搜索或是贝叶斯优化等方法来寻找最优配置组合是一种常见做法。同时,在资源允许的情况下,还可以尝试更复杂的自动化机器学习工具来进行大规模实验设计。 #### 使用AutoModels简化流程 借助于`transformers`库提供的`auto-models`功能,可以根据指定的检查点自动选择合适的架构并加载权重,从而大大减少了手动配置的工作量。这种灵活性使得快速迭代成为可能,并且更容易找到最适合当前任务需求的最佳实践方案[^2]: ```python from transformers import AutoModelForSequenceClassification, AutoTokenizer model_name = "distilbert-base-uncased" tokenizer = AutoTokenizer.from_pretrained(model_name) model = AutoModelForSequenceClassification.from_pretrained(model_name) # 进一步微调或其他操作... ``` #### 探索生成对抗网络(GAN) 如果关注的是生成型AI领域,则探索基于GAN框架的新颖变体或许能带来意想不到的进步。这类技术不仅限于图像合成,在自然语言处理方面也有着广泛的应用前景,比如文本摘要、对话系统等领域[^3]。
评论 24
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值