栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

我的NLP

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

我的NLP

安装Huggingface
https://github.com/huggingface/transformers

到这个网站下载下来,放到

C:SoftwareanacondaLibsite-packages

这里,我喜欢放在这里,之后,在新建的conda环境里cd到这里,输入

python setup.py install

即可安装

还有HuggingFace的其它Datasets包,Tokenizers包,也可以这么安装

参考教程

最重要的教程

Introduction - Hugging Face Course

其余是一些同人

【Huggingface Transformers】保姆级使用教程—上 - 知乎

【Huggingface Transformers】保姆级使用教程02—微调预训练模型 Fine-tuning - 知乎

HuggingFace 重要链接
https://huggingface.co/models

遇到translation pipeline,可能出现如下错误:
 
ValueError: This tokenizer cannot be instantiated. Please make sure you have `sentencepiece` installed in order to use this tokenizer.
说明需要安装sentencepiece
 conda install sentencepiece
Hugging Face Pipeline

我下载了模型,在E盘,模型文件可以一直保留

zero-shot-classification
from transformers import pipeline
path = r"E:HuggingFacetransformersfacebookbart-large-mnli"
clssifier = pipeline("zero-shot-classification",model=path)
print(clssifier(
    "This is a course about the Transformers library!",
    candidate_labels = ["education","politics","business"],
))
text-generation
from transformers import pipeline
path = r"E:HuggingFacetransformersdistilgpt2"
generator = pipeline("text-generation",model=path)
print(generator(
    "In this course,we will teach you how to",
    max_length = 30,
    num_return_sequences = 2,
))


fill-mask
from transformers import pipeline
path = r"E:HuggingFacetransformersbert-base-uncased"
unmasker = pipeline("fill-mask",model=path)
print(unmasker("This course will teach you all about [MASK] models.",top_k = 2))

ner
from transformers import pipeline
path = r"E:HuggingFacetransformersdslimbertlarge-NER"
ner = pipeline("ner",grouped_entities = True,model=path)
print(ner("My name is Sylvain and I work at Hugging Face in Brooklyn."))

question-answering
from transformers import pipeline
path = r"E:HuggingFacetransformersbert-large-uncased-whole-word-masking-finetuned-squad"
question_answerer = pipeline("question-answering",model=path)
print(question_answerer(question="Where do I work?",
                        context="My name is Sylvain and I work at Hugging Face in Brooklyn."
                        )
      )

summarization
from transformers import pipeline
path = r"E:HuggingFacetransformersgooglepegasus-xsum"
summarizer = pipeline("summarization",model=path)
print(summarizer(
    # 夜に駆ける
        """
    Like sinking, like melting
    
    At night when the sky of only two people spreads
    
    It was just "goodbye"
    
    I understood everything in that one word
    
    The sky where the sun has set and your figure
    
    Overlapping over the fence
    
    From the day I first met
    
    Taken away all of my heart
    
    You who wear a fleeting air somewhere
    
    I had lonely eyes
    
    Always with Tic Tac
    
    How many times in the ringing world
    
    To the noisy voice
    
    Even if tears are about to spill
    
    Mundane joy I'm sure you can find it
    
    To you who can't laugh in noisy days
    
    Have a dazzling tomorrow as long as you can think of
    
    Before falling on a night that doesn't dawn
    
    Grab my hand
    
    Even the days I locked up because I wanted to forget
    
    I'll melt it with the warmth I hug
    
    Not scary until the sun rises
    
    Let's be two
    
    I can only see you
    
    I hate you staring at something
    
    Like falling in love as if you were in love
    
    I hate that kind of face
    
    I want to believe but I can't believe it
    
    I'm sure what happened
    
    There are many from now on
    
    I'm angry and crying
    
    Still, I'm sure someday we'll be sure
    
    I believe you can understand each other
    
    I'm tired because I don't like it anymore
    
    You who shake my hand out to me
    
    I'm tired because I don't like it anymore
    
    I really want to say that too
    
    You see, with Tic Tac
    
    How many times in the ringing world
    
    I can't reach any of the words I prepared for you
    
    "I want to end it"
    
    When I was caught and put it into words
    
    You laughed for the first time
    
    I couldn't laugh in the noisy days
    
    You are beautiful in my eyes
    
    Even the tears that spilled on the night that didn't dawn
    
    Melting into your smile
    
    I was crying in the same days
    
    You gently invite to the end
    
    Like sinking, like melting
    
    The soaked fog clears
    
    In the days I was trapped because I wanted to forget
    
    Take your hand for reaching out
    
    The cool breeze blows through now like swimming in the sky
    
    Don't let go of your hands
    
    Two people are running out at night now
    """
))

translation
from transformers import pipeline
path = r"E:HuggingFacetransformersHelsinki-NLPopus-mt-zh-en"
translator = pipeline("translation",model=path)
print(translator("我爱你"))

from transformers import pipeline
path = r"E:HuggingFacetransformersHelsinki-NLPopus-mt-en-zh"
translator = pipeline("translation",model=path)
print(translator("I love you"))

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/822200.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号