New NLP Attempt: How to make machines understand and predict human emotions?

People rarely wonder whether the sentiment revealed by the tweet is positive or negative. But for computers, it is almost a difficult way to go to the sky: complex sentence structures, irony, metaphors, etc., make it difficult for computers to judge the meaning and emotions expressed in sentences. However, if we can let the computer automatically evaluate the sentiment revealed by the tweet, then we can dig out people's opinions on various issues on a large scale, and it will also help us understand why certain groups hold certain views.

On a more basic level, understanding the emotion of text is a key part of natural language understanding. Therefore, if we want computers to communicate with us effectively, we must solve this problem.

In this blog post, I will introduce the results of a small research project at the University of Sheffield, which is part of the SoBigData project. We tested different text processing methods and analyzed how much emotion they can capture.

Foreword

The purpose of this project is to test how computers use machine learning to understand the emotions expressed in text. To this end, we feed a large number of tweets to the computer, and each tweet is labeled as positive, neutral or negative by people. Every tweet has a related topic, which is important because sentences can have very different sentiments depending on the topic of discussion. For example, if we are discussing the topic of "quality", then the word "high" conveys positive emotions, and when discussing the topic of "price", what this word reveals is negative emotions Up. Another example is the word "Green". When discussing the topic of "environmental issues", this word is positive, but it may be neutral when discussing the topic of "art". The task of the computer now is to predict the sentiment expressed in a given tweet and related topics.

How does the computer read the text?

If you have no experience in machine learning, you may find this seems to be a strange problem. However, machine learning is based on statistics. Therefore, anything that a machine learning system needs to process must be represented by numbers. The process of converting text into numbers is done in so-called embedded models, and the development of these models is itself one of the main research areas. The embedding model converts a word or sentence into a vector. During the training process, the vector is constantly adjusted so that words and sentences with similar meanings will have similar vector endings. Ideally, vectors should be able to capture meaning, context, emotions, etc. But this is not a simple task at all, which is why many different embedding models have been developed. Generally speaking, newer models perform better, but they can also be adjusted for specific tasks.

A mature machine learning system can reach the most advanced level in sentiment analysis and other aspects, which is simply too fierce. They are composed of multiple components, of which text embedding is only one of them. It is usually difficult to evaluate which parts of the system are performance bottlenecks. Since any text needs to be represented as a vector that can be used by a machine learning system, any analysis (including predicting the sentiment of a tweet) depends to a large extent on the chosen embedding model. But this is not to say that other parts of the system are equally important.

To make the role of text embeddings more transparent, we began to test their performance in predicting emotions. For this reason, we designed the system to be the least ambiguous.

How do we predict emotions?

Our method of predicting emotions is fairly simple and inspired by collaborative filtering. Every tweet has a related topic, and it is important that the sentiment must be evaluated based on this topic (because the same statement is positive for one aspect and negative for the other). Since both the tweet and the corresponding topic have the same dimensional vector representation, we can take the inner product of the two and give a number that represents the sentiment. There is no reason to think that it should be used with the "raw" embedding, so before using the inner product, we should learn and apply the transformation (explained further later) to the topic vector space. In this way, we can get emotions, even if this topic has never been seen before.

We hope to be able to predict three different emotions (positive, neutral, and negative). Therefore, in fact, we have learned three different transformations of the topic space, which are predicting positive emotions, neutral emotions, and negative emotions. At that time, when using each of the three transformed topic vectors to obtain the inner product of the tweet, we got three numbers, which can be understood as the model’s inference for each sentiment: the higher the value, the more the model thinks this is the case. The sentiment revealed by a tweet.

Project Overview

We want to test how different word embeddings affect the sentiment of a tweet. To predict emotions, we trained a model that learns three transformations of the subject vector, so that the inner product of the tweet and the three topic vectors is the model's vote for these three emotions.

We have several different options. First, we must choose the embedding model to test. Second, we need to decide how to transform the topic vector. Finally, we also need a dataset of tweets that have been manually tagged with emotion labels, so that we can train and test this model.

Decide on settings data set

We used the English data set provided for SemEval-2017 Task 4 (http://alt.qcri.org/semeval2017/task4/). This data set contains about 26,000 tweets on different topics, all of which sentiments are manually labeled. We keep the split defined by the task organizer, that is, approximately 20,000 tweets for training and 6,000 tweets for testing.

 Embedded model

We chose to test the following four embedding models:

  1. Neural-Net Language Models (NNLM) in 2003 was one of the first experiments to try to learn word embeddings using neural networks. The model builds a 128-dimensional word vector and uses it as a word embedding baseline. More advanced models should be able to beat this baseline.

  2. NNLM as mentioned above, but now that there are standardized word vectors, it can sometimes be observed that they produce better results.

  3. Embeddings from Language Models (ELMo) in early 2018 has been proven to achieve state-of-the-art results in many different tasks, constructing 1024-dimensional word vectors.

  4. The Universal Sentence Encoder (USE) in early 2018, a trained model that can find useful word embeddings in many tasks. Construct a 512-dimensional word vector.

以上提到的所有四种嵌入模型,都可以从 TensorFlow Hub 方便地获得:https://tfhub.dev/

 转换模型

用于转换主题向量空间的模型的选择,是一项很棘手的任务。为什么呢?因为,一方面,我们希望保持原始向量空间尽可能不变;另一方面,我们希望转换足够灵活,以便嵌入单词中的信息实际上能够用来预测情绪。因此,我们决定测试这两种不同的转换模型。

  • 一种简单的仿射变换。这种转换只能表示最基本的转换,如缩放、旋转、剪切和平移,因此,在某种意义上,这将测试 “原始” 嵌入捕获了多少信息。

  • 一种更为复杂的转换,是由神经网络表示。我们使用一个具有两个隐藏层的神经网络,每个隐藏层是嵌入维数的 8 倍:ReLU 激活函数和 Dropout。网络将主题向量作为输入,并输出转换后的主题向量。这种转换可以以高度非线性的方式扭曲主题空间,因此应该能够获得更高的准确度。然而,这样一来训练将更加困难,并更可能对训练集出现过拟合现象。

最后的模型将学习上述每种类型的三种转换,对应于我们想要预测的三种情绪。

纠正数据集中的不平衡

使用真实数据总是很有挑战性的。尤其是如果某个情绪或者主题被过分代表,那么这个模型很可能会在训练期间完全专注于此,这将会导致预测其他情绪,或使用其他主题。相反,我们希望确保模型对所有主题和情绪给予同样的权重,而不管它们出现的频率有多高。纠正这些错误的效果是相当戏剧化的,也是一个值得记住的好教训,所以,让我们花上几分钟的时间来讨论这个问题。

数据集中的不平衡

绘制每种情绪的推文数量,如下图所示,数据集显示,类出现了巨大的不平衡的现象。

为训练集和测试集分配情绪类。

尤其在训练数据中,积极的情绪被严重高估,事实上,近 73% 的训练推文都有积极的情绪。这意味着,该模型将比其他模型更能从预测积极情绪中获益。另一方面,中立情绪只与不到 10% 的推文相关,如果有助于预测积极情绪的话,那么模型可能只是学会了忽略这种情绪。

测试集中的分布截然不同。消极情绪比积极情绪更加丰富,没有一条推文透露出来的情绪是中立的。这使得模型平等对待所有的情绪变得更加重要。

实际上,在 NNLM 上使用仿射变换模型进行的测试表明,由于训练数据的普遍性,因此经过训练后的模型明显倾向于积极情绪。在本次测试中,我们将训练数据中的主题分为训练集和评估集,分别占主题的 90% 和 10%。

显示推文的实际情绪和模型预测的情绪的混淆矩阵(confusion matrix)。百分比显示了特定的实际情绪被预测为模型的三种情绪中的任何一种的频率。完美的模型应该有 100% 的对角线,这意味着预测总是正确的。然而,从此图可以看出,无论实际情绪是什么,这个模型通常会选择预测积极情绪。(译者注:混淆矩阵,是可视化工具,特别用于监督学习,在无监督学习一般叫做匹配矩阵。矩阵的每一列代表一个类的实例预测,而每一行表示一个实际的类的实例。基于实际的名称可以更容易判断机器是否将两个不同的类混淆了。在机器学习领域,混淆矩阵通常被称为列联表或误差矩阵。

上图显示了情绪预测的混淆矩阵,其中,每列对应于一种预测的情绪。每一行显示的是实际的情绪,每个矩阵元素的数量和颜色显示具有这种实际情绪的推文在列中显示的百分比。

理想情况下,对角线应接近 100%,这意味着几乎所有推文的预测情绪都是正确的,但即使是训练集中,也会存在很大的非对角线元素。这表明了即使模型知道正确的情绪,也会更倾向于在大多数情况下默认预测积极情绪。43% 的消极情绪的推文和 55% 以上的中立情绪的推文被预测为积极情绪。这对于分别为 39% 和 78% 的评估集来说更加槽糕。

但是,在训练集和测试集中,每个主题的推文数量也存在很大的不同。

图片

训练集和测试集中与每个主题相关的推文数量。主题基于推文数量从左到右排序,为清晰起见,此处略去了主题的名称。

特别是对训练集来说,我们看到每个主题的推文数量存在明显差异:有些主题有超过 100 条推文,而大约一半的主题只拥有约 20 条推文或更少。

让我们回到仿射模型的测试,观察给定主题的推文情绪预测的平均准确率,结果表明,推文越多,主题的准确率就越高。

图片

给定主题中推文情绪预测的平均准确率。有个明显的趋势是,具有更多相关推文的主题通常会获得更高的平均准确率。

出现这种趋势是有道理的:该模型从学习一种转换中受益匪浅,这种转换适用于包含更多推文的主题。但实际上,这并非我们想要的结果,因为这意味着模型可能不会很好地进行归纳。我们希望模型即使在看不见的主题上也能表现良好,对某些主题过拟合在这方面可能不会有什么帮助。

处理类似这样的类不平衡的一种方法是,通过类的频率的倒数来衡量模型对错误预测的惩罚。这意味着,对于频率较低的数据,模型会收到很大的误差,因此会更加关注这些错误。让我们看看,这将会如何影响模型的训练。

纠正情绪不平衡

重新训练模型,只使用情绪频率的倒数来惩罚错误,我们已经达到了更好的模型。好的模型。

图片

NNML 上仿射模型的混淆矩阵,纠正训练集中的情绪不平衡。

对于训练集的所有情绪来说,对角线接近 100%。对评估集的预测也有所改进,尽管还存在很大的改进空间。

我们还看到训练集中的每个主题的准确率有所提高,尽管这并没有得到明确的鼓励。

图片

NNML 仿射模型的平均主题准确率,纠正了训练集中的情绪不平衡。

有趣的是,评估集的性能似乎有所下降。一种解释可能是评估集中的大多数推文都有积极的情绪,为了更好地表现出消极和中立的情绪,模型牺牲了一些准确性。

纠正主题不平衡

接下来,我们来看看如果只使用主题频率的倒数来惩罚错误会发生什么。这也带来了训练集上更好的情绪预测,这可能是因为,无论与主题相关的推文数量有多少,对主题衡量的权重都是平等的,这将使得模型直面更多种的情绪。

图片

NNML 上仿射模型的混淆矩阵,纠正训练集中的主题不平衡。

但是,要看真正的效果究竟如何,需要看每个主题的准确性。对于训练集来说,现在准确率与主题中的推文数量几乎无关,大多数主题都接近于 1。

图片

NNML 上仿射模型的平均主题准确率,纠正训练集中的主题不平衡。

纠正情绪和主题的不平衡

最终的模型将根据情绪和主题的频率来权衡错误预测的惩罚。这是通过简单地将主题频率和情绪频率的倒数相乘,并使用得到的数量作为权重来完成的。这将鼓励模型在训练期间能够平等对待所有情绪和主题。

这种方式生成的模型,似乎确实在考虑情绪和主题不平衡之间进行了很好的权衡。情绪预测相当准确,而且在评估集上的表现没有受到影响。

图片

NNML 上仿射模型的混淆矩阵,纠正训练集中的两种不平衡。

每个主题的平均准确率同样独立于主题相关的推文数量。

图片

NNML 仿射模型的平均题目正确率,修正了训练集的两种不平衡。

虽然纠正类不平衡明显对训练集有所帮助,但评估集的表现仍然没有什么明显起色。但该模型似乎不能很好地推广到新主题,这可能意味着仿射变换的局限性太大,或者训练集不能很好地代表评估集。我们将在回顾最后的实验时再回过头来看这一点。

信息汇总

现在,在考虑到数据集中的类不平衡,以及已经决定嵌入和转换模型之后,我们准备测试这些模型。看看单词陷入能够收集到多少情绪信息。

该设置遵循标准的机器学习方法:我们用 10 组(fold)交叉验证(cross-validation)来训练模型,并评估测试集上每组的最佳模型。这给我们提供了一个衡量标准,当在(稍微)不同的数据集上训练时,我们可以预期模型的性能会有多大的变化。

包含一些基线实验总是一个好主意。这些应该是你可以想到的最简单的方法,如果你的高级模型无法击败这些方法,那么你就会知道出错了。我们选择了两个简单的基线:1)使用训练集中最频繁的情绪(我们将使用积极的情绪)作为任何推文的预测;2),使用训练集中的随即将情绪作为预测。

如下图所示,展示了对所有八个模型和两条基线进行训练以及对不可见测试集进行评估的结果。穿过数据点的垂直线表示 10 个交叉验证组的一个标准差。

图片

在这里,有很多有趣的观察等待我们去做。首先,在基线之上的任何嵌入的模型都有很大的改进。因此,正如所预期的那样,嵌入这个词捕获了可以用来推导推文情绪的信息。其次,转向 NNLM 嵌入,使用非线性模型似乎并没有什么改进。这很有趣,因为它表明了嵌入空间足够简单,仿射模型能够使用嵌入中所有可用的情绪信息。这与最新的嵌入方法 ELMo 和 USE 形成了对比。在 ELMo 和 USE 中,我们确实观察到在非线性模型的使用情况有所改善,这表明这些模型学习的嵌入空间更加复杂。对于 NNLM,归一化向量确实比非归一化向量有更好的表现趋势,但是在我们的实验中,效果并不显著。最后,虽然 ELMo 和 USE 都包含比 NNLM 嵌入更多的信息,但它们在这些实验中的表现却非常相似。USE 似乎通常包含比 ELMo 略多的信息,但也并没有多多少。然而,这仍然很有趣,因为 USE 嵌入空间的维数比 ELMo 空间要低得多,因此,模型训练速度要快得多。

我们实现目标了吗?

没有,绝对没有。关于单词嵌入的信息内容,还有很多有趣的问题需要回答。

例如,我们用三点量表(消极、中立、积极)来处理情绪。如果处理情绪要扩展到更细粒度的话,比如五点量表,就需要更多的嵌入。这些嵌入包含这么多信息吗?

询问是否需要巨大的嵌入空间也是合理的。ELMo 的嵌入是 1024 维,但信息可能嵌入在更低维度的空间中。嵌入空间的降维如何影响情绪预测?

在测试主题空间的两种不同变换时,我们发现,只有较新的嵌入才需要非线性变换。扩展转换的类型,包括创建一些更复杂的神经网络,以及测试哪些嵌入可以从哪些转换中获益,这将是一件非常有趣的事情。这可以让我们能够深入了解不同嵌入空间的复杂性。

结  果

在这个项目中,我们想测试不同的单词嵌入对推文的情绪预测有什么样的影响。为此,我们构建了两个模型,在尽可能不干扰的情况下来预测情绪,从而使我们能够看到元是单词嵌入包含了多少情绪信息。

结果表明,无论新的还是旧的单词嵌入,确实都包含了关于情绪的信息,新的单词嵌入包含的内容要比旧的更多,这并不奇怪。结果还表明,对于较新的嵌入,主体向量的非线性变化的比仿射变换表现得更好,这表明这些空间比较旧的嵌入更加复杂。

总之,单词嵌入通常包含大量关于推文情绪的信息,而更新的嵌入包含的信息要多得多。它强调了高级嵌入模型在预测推文情绪方面的重要性,但这并不过分令人惊讶。


Guess you like

Origin blog.51cto.com/15060462/2679503