An artificial intelligence model that can be used by paramecia

Front row tips:

  • Reading this article does not require any knowledge of artificial intelligence and algorithms.
  • You can use any programming language, but python is better.
  • Just as NLP science, entertainment, the actual algorithm work is not so simple.

X: "Bao, what algorithm are you working on?"

A: "NLP, also known as natural language processing."

X: "It's too abstract"

A: "Like a chatbot?"

X: "ummmmm"

A: "Forget it, try it yourself, get in the car, and sit tight."


It will be divided into two parts:

  • If there is no Python environment, I will provide a colab, but colab is from Google. How to open it can understand what I mean.

  • If you have a Python environment, you can try it directly. The code is exactly the same.

If you don't have a Python environment

So: Transformers, what can they do? - Colaboratory (google.com)

See the link above, and see how Google knows how to open it. (crazy hint)

With colab, you just click the left mouse button.

image.png

See the button in the red box in the picture above, which piece of code I'm talking about, just click it and it will run.

if you have a python environment

If you have a Python environment, I will assume that you have already installed things with pip. Then you just need to copy and paste the code, throw it into your IDE and run it.

first step:

pip install datasets evaluate transformers[sentencepiece]

Download and use the pipeline provided by hug face so that you can quickly see what some of the current NLP models can do.

Step 2:

Open your IDE and start copy-pasting and running my code below.

Note : There is a difference between jupyter and Python's regular IDE. Write the following code directly in jupyter (colab above), the last line of a code block will be output by default, no need to print, but you need to add it yourself to see the result in ordinary IDE print().


emotion analysis

# colab:
from transformers import pipeline

classifier = pipeline("sentiment-analysis")
classifier("I love you!")

# 其他IDE:
from transformers import pipeline

classifier = pipeline("sentiment-analysis")
result = classifier("I love you!")
print(result)
复制代码

这段代码的意思是导入pipeline,新建一个classifier,这个classifier是使用了pipeline的情感分析功能。

之后你只需要给它传入句子,就可以进行句子的情感分析了。

然后你可以看到他的输出大概是长这样

[{'label': 'POSITIVE', 'score': 0.9998717308044434}]

意思是说系统判定这个句子是个积极的句子,得分是99%,得分越高,当然越偏向于积极。

传入多个句子:

当然你也可以给他传入多个句子,句子需要使用列表进行储存。

Python列表(list)的特点是用[]存储,数据项之间使用,分割。

比如我们想传入两个句子,就是["I love you.","我不爱换吃香菜。"]

我们只需要把这个列表传给刚才的classifier即可。

from transformers import pipeline

classifier = pipeline("sentiment-analysis")
classifier(["I love you.","我不爱换吃香菜。"])
复制代码

之后我们可以看到输出结果为:

[{'label': 'POSITIVE', 'score': 0.9998705387115479},

{'label': 'NEGATIVE', 'score': 0.9371575713157654}]

模型认定第一个句子是个积极的句子,得分是99%,第二个句子是个消极的句子,得分是94%。

总结

这个任务就是自然语言处理之中的情感分析任务。可以应用在一些评论中来处理用户极性,比如各大购物平台的评论区啊,或者外卖平台的评论区啊,服务提供商可以迅速地通过对评论进行情感分析,来获取用户对产品的一些喜欢或者厌恶的程度。当然在这里只是进行了一个粗粒度的情感分析。细粒度的情感分析可以做到更精准的定位。


文本生成

文本生成就是说你给他一个句子或者几个句子,他给你继续往下写。我之前就是在做这个东西。然后我做的主要是受控文本生成,就是说你可以去控制模型生成的情感呀主题呀之类的。当时写这个东西的时候大家在想应用场景嘛,然后想来想去,我说了一句是不是要去做免费的网络水军。

今天在这里就简单的展示一下最普通的文本生成。因为各种各样的原因,这些简单的模型现在的生成效果可能并不是特别的好。所以在这里仅仅是做一个展示而已。

from transformers import pipeline

generator = pipeline("text-generation")
generator("I am boring, I want to")
复制代码

注意:

在这里默认使用的是GPT-2,每个人每一次生成出来的结果都是不一样的。所以我生成出来的答案并不是你生成出来的答案,它是随机的。你得到的输出肯定是和我的不一样。

[{'generated_text': 'I am happy, I want to go to work."\n\nWith her hand in a pocket, the waitress looked around and saw an expression on Colin's face. "It's pretty obvious that you are here in the business to do this, as'}]

我这边的生成结果是

“我很高兴,我想去工作。”女服务员把手放在口袋里,环顾四周,看到柯林连上的表情……

语言模型并不如大家想象中的那么智能,所以你看到它生成的结果可能并不通顺,或者逻辑也并不清晰,这一点不要诧异。文本生成任务其实还是任重道远的。

生成多个候选:

可以使用参数 num_return_sequences 控制生成多少个不同结果。

from transformers import pipeline

generator = pipeline("text-generation", model="distilgpt2")
generator(
    "In this course, we will teach you how to",
    num_return_sequences=3,
)
复制代码

在这里设置了返回序列的数量为3,这样你就可以一下获得三个结果。

[{'generated_text': 'In this course, we will teach you how to write with both HTML & JavaScript which is easy to learn, easy to use, and in-depth in HTML. You will learn how to write with both HTML & JavaScript which is easy to learn,'},

{'generated_text': 'In this course, we will teach you how to run a simple and simple network with a very simple set of parameters (for example, for a network where you can run a network with just one parameters in place), which is very important to your network'},

{'generated_text': "In this course, we will teach you how to use the skills required to make the right choices while playing a successful video game. If we can't find that many gamers do not feel like making this course even though they don't understand how to get"}]


问答

现在问答系统分为好多种。有的是基于上下文阅读理解归纳推理的,有的是基于信息提取的。二者的区别是比如说我给你一个问题,然后给你一段文本。

  • 基于阅读理解的就是读完你这段文字之后给你归纳出来一个答案。这个答案比较合理,但是和你的原文可能说法不太一样。
  • 而基于信息提取的就是直接把你原文中可能作为答案的部分给你摘出来。
from transformers import pipeline

question_answerer = pipeline("question-answering")
question_answerer(
    question="Where do I work?",
    context="My name is Sylvain and I work at juejin in Beijing"
)
复制代码

输出是:

{'score': 0.758256196975708, 'start': 33, 'end': 39, 'answer': 'juejin'}

这个输出的意思是,模型觉得这个问题的答案应该是你在掘金工作,他觉得这个答案76%的概率是对的。掘金这个词在原文中的起始位置和终止位置是33和39。很明显这是一个基于提取的问答。

总结

问答系统很常见,啊现在大家不是很多都在做什么医疗问答系统,呀智能客服机器人呀。至今多轮对话都是一个研究的重点方向。要做出替代真人客服的系统,问答系统还有很长的一段路要走。


翻译

翻译顾名思义就是进行语言的转换翻译了。现在各大翻译平台都已经用上基于神经网络的翻译了。

from transformers import pipeline

translator = pipeline("translation", model="Helsinki-NLP/opus-mt-zh-en")
translator("今天是周四,我要吃肯德基。")
复制代码

输出是:

[{'translation_text': "It's Thursday. I'm gonna eat Kentucky."}]

先解释一下上面的代码。代码很简单,就是使用pipeline的翻译功能,然后让translator去做翻译。但是这里加了一点东西model="Helsinki-NLP/opus-mt-zh-en",这句的意思就是我不使用默认的模型了,我在这里使用的是这个中文到英文的模型。就是我给他指定了一个Helsinki-NLP/opus-mt-zh-en模型。所以下面我做的是一个中文到英文的翻译。不同的语言要使用不同的模型。当然也有多语言模型,当然多语言模型也不能够涵盖所有的语种。所以尽量还是要选择适合自己的模型。

总结

机器翻译任务最开始是属于基于规则,逐渐发展到深度学习,从开始RNN及其变体,到transformer。但是transformer现在有一个硬性的缺点,就是虽然在encoder部分实现了并行化计算,但是无法在decoder部分并行计算,对于一些大任务来说,它的效率还是很低的。所以近两年非自回归文本翻译蓬勃发展,国内的话在这一领域卷的非常厉害的当然就是字节跳动的火山翻译,扛起非自回归文本翻译的大旗。


最后的总结

自然语言处理涉及的其实非常广泛,并非我列举的这几个任务。要真细数自然语言处理的任务的话,可能会细分到几十个。它的应用其实涵盖到了我们生活的方方面面。比如说我们输入法提示词、比如我们的用户行为分析、比如我们的问答机器人。你可能平时都受益于此,只是因为没有了解到这些方面而已。自然语言处理还在蓬勃发展之中,甚至达不到差强人意的效果,人工智能想要取代人类还有很长的一段路要走。

Guess you like

Origin juejin.im/post/7133156220881338404