🛒

【言語モデルをファインチューニング】感情分析モデルを利用してAmazonレビューの星の数を予想してみた

2025/02/11に公開

chatGPTの登場以降、LLMの利用した文章生成は爆発的に広がり、日々の生活に無くてはならなくなったという人も多いのではないでしょうか。

今回は、言語モデルの能力を活かして、文章から新たな情報を引き出す研究を学び、実際のデータセットで試してみました。

https://github.com/shogoromal/AmazonRating_T5/tree/master

参考にした論文について

https://arxiv.org/abs/2302.08624

上記の論文を参考にしました。まずはこちらの論文について簡単に解説します。

文章による感情分析

この論文で行われているタスクは、ABSA (Aspect Based Sentiment Analysis)と呼ばれており、文章の中で、単語と感情 (positive や negative など) の関係を予想するものになります。

論文中の以下の表が分かりやすかったので、引用しておきます。ABSAについて、さらに細かく分類したものです。
例えば、ATE (Aspect Term Extraction) は、文章 (S_i) を与えた時にその文章の主題 (a^1, a^2) は何かを推論するタスクになります。
表が下に行くほどタスクが複雑になり、一番下の AOTE(Aspect Opinion Sentiment Triplet Extraction) では、文章を入れただけで、「主題(a)」・「主題について述べた単語(o)」・「主題についての認識(sp)」の3つを同時に推論しています。

image.png

T5 モデルについて

この論文で使用されている言語モデルは T5 (Text-To-Text Transfer Transformer)と呼ばれ。テキスト生成、翻訳、要約、質問応答など幅広いタスクに適用できるのが特徴です。T5はすべてのNLPタスクを「テキスト入力をテキスト出力に変換する問題」として統一的に扱うことで、タスクごとの違いを減らし、汎用性を高めています。また、事前学習に大規模なデータセットを使用し、微調整(ファインチューニング)によって特定のタスクへの適応が可能です。

image.png

論文の成果

この論文では、入力文の与え方の違いによって、モデルの推論性能にどのような違いが出るかを検証しています。結果は、指示文や例文があった場合の方が推論の性能が良くなるというものです。詳細は論文を参照してください。
ちなみに、この結果によって、単に言語モデルが単語や文の構造から次に来る単語を予測しているだけではないという示唆が得られるそうです。つまり、定義 ( Definition ) を与えて性能が良くなるということは、言語モデルがタスクの本質的な意味を理解していると言えるとのこと。面白いですねえ。

image.png

本題

Amazonoレビューのデータセットにこの研究を適用

使用したデータは以下のもの。Amazonレビューのデータがカテゴリごとに分けて保存されています。
https://amazon-reviews-2023.github.io/

これらのデータから"All_beauty"カテゴリ(美容品)のものを使って、レビューの文章から星の数を予測する推論モデルを作成しました。

工夫した点など

基本的なモデル学習の手法・パラメータ設定については論文のものと変えていません。論文にもあるように、与える例文の数が2つの場合のほうが、性能が良くなるのですが、メモリが不足してしまったため、例文を1つにしています。具体的な入力文は以下の通りです。

Definition : The output will be 5 rank rating (output),
which is Very Poor, Poor, Average, Good or Excellent.
These rating is belong the sentence (input) to explain the rating.
example1:
input: This is a literal piece of garbage. It has no sharp edges to actually remove anything and it’s just vibrates like a cheap toothbrush. Even the “scaler” that comes with it is a joke, again no sharp edges to scrape anything. Save your money and get an actual scaler. This thing is a joke.
output: Very Poor
example2:
input: WARNING! This product is advertised as being a thermal heat protection spray. It is not, it completely damaged my hair!!!! I had to go back to salon literally the month after I just got it cut because the ends were fried to the max, even my hairdresser was mortified as to what damaged my hair. I had to pay a pretty penny for a deep conditioning salon treatment to get my hair back to normal.<br /><br />Also, it smells really bad though if it actually worked I won't mind it as much.<br /><br />Do not waste your money on this!!
output: Poor
example3:
input: Maybe as I continue to use this product, I will be able to give it a 5 star rating. At this point in time, I am getting results, which I am satisfied with. I am hoping that with continued use, the results will also improve. I would still recommend this product. I have experience no adverse side effects from using this product.
output: Average
example4:
input: I have really fine and thin hair. These extensions (and I'm guessing any clip-in extensions) are good for adding a little volume/mass, but NOT GOOD for adding a lot of length. I had to trim it to just a few inches longer than my natural hair to get it to blend, and I was only able to add one three-clip piece, a two-clip piece, and a one-clip piece in total for it to not show through in the end result. I would see this as not too much of an issue for people with thicker hair. They also aren't a bad choice if you want to do a ponytail or something that can easily conceal the clips. Overall, not a bad product.
output: Good
example5:
input: I enjoy sheet masks but on a regular they can get pricey. This is such a smart idea. I keep these on my dressing room vanity by the sink and use them frequently. I love that I feel no guilt whether I keep it on half an hour or I just have 15 minutes because I don't feel like I'm throwing away money.<br /><br />I'll tell you how I messed up, though. I turned them box upside down because some people mentioned how they weren't all moist. I often do this. Well, This jar isn't sealed for that so in the morning I had a little leakage on my counter. But no biggie. I didn't lose much and I just turned it back rightside up and it's all good.<br /><br />During this pandemic, I've been spending a lot of time just pampering myself. If I'm in all day, I wear my night cream all day long. Or I soak my feet under my desk while I'm writing content.<br /><br />This is a nice treat and I plan to buy some more for impromptu holiday gifts this year. Things like this will be appreciated.
output: Excellent
Now complete the following input-
input:

また、レビューの星の数を具体的な単語に変換しています。(星1 → Very Poor など)
単なる数字は、T5 モデルにとっては意味を持たないですが、具体的な単語の場合はモデルが持つ事前知識を使って意味を解釈することができます。
ちなみに、数字のまま学習を行いましたが、ほとんど性能が改善されませんでした。

1: "Very Poor",
2: "Poor",
3: "Average",
4: "Good",
5: "Excellent"

結果

以下、実際のラベルと予想したラベルの対応表です。概ね正しい推論ができているかと思います。

image.png

定量的な評価についても、簡単に計算しました。以下のようになったのですが、まずまずの性能が出ているのではないでしょうか。
そもそも、レビューの星の数なんて主観が多いに入った指標ですので、完璧を追い求めるのもなかなか難しいかと思います。(「もうこれ無しでは生きられません!」→星3つ とかあったりしますし。)

Mean Absolute Error: 0.23625
Mean Squared Error: 0.28875
Root Mean Squared Error: 0.537354631505117

おまけ

最後にせっかくなので、英作文を入力して狙いの星の数を出力させる遊びをしてみました。
以下、星4を狙って私が作成した文章。で、結果は星4 (Good) だったので、見事成功しました。

"I'm sure this is not the best. But, in so many usecase, it is so useful."

# pred_labels = "Good"

英語力向上に使えるかも!!???

Discussion