このモデルはLuke-japanese-base-liteをファインチューニングしたものです。
このモデルを用いることで文章がポジティブかネガティブかをLUKEを用いて分類することができます。
夏目漱石さんの文章(こころ、坊ちゃん、三四郎、etc)を日本語極性辞書
(
http://www.cl.ecei.tohoku.ac.jp/Open_Resources-Japanese_Sentiment_Polarity_Dictionary.html )
を用いてポジティブ・ネガティブ判定したものを教師データとしてモデルの学習を行いました。
使用した教師データから、口語より文語に対して高い正答率となることが期待されます。
This model is based on Luke-japanese-base-lite
This model is fine-tuned model which besed on studio-ousia/Luke-japanese-base-lite.
This could be able to distinguish between positive and negative content.
This model was fine-tuned by using Natsume Souseki's documents.
For example Kokoro, Bocchan, Sanshiro and so on...
what is Luke? Lukeとは?[1]
LUKE (Language Understanding with Knowledge-based Embeddings) is a new pre-trained contextualized representation of words and entities based on transformer. LUKE treats words and entities in a given text as independent tokens, and outputs contextualized representations of them. LUKE adopts an entity-aware self-attention mechanism that is an extension of the self-attention mechanism of the transformer, and considers the types of tokens (words or entities) when computing attention scores.
LUKE achieves state-of-the-art results on five popular NLP benchmarks including SQuAD v1.1 (extractive question answering), CoNLL-2003 (named entity recognition), ReCoRD (cloze-style question answering), TACRED (relation classification), and Open Entity (entity typing).
luke-japaneseは、単語とエンティティの知識拡張型訓練済み Transformer モデルLUKEの日本語版です。LUKE は単語とエンティティを独立したトークンとして扱い、これらの文脈を考慮した表現を出力します。
how to use 使い方
ステップ0:pythonとpytorchのインストールとtransformersのアップデート(バージョンが古すぎるとMLukeTokenizerが入っていないため)
update transformers and install python and pytorch
ステップ1:My_luke_model_pn.pthをダウンロードする。Download "My_luke_model_pn.pth"
ステップ2:"My_luke_model_pn.pth"のあるディレクトリを入力し、"Mymodel_luke_pn.py"を実行する。execute "Mymodel_luke_pn.py"
出力としてはpre.logitsが得られます。
pre.logitsはtensor[[x, y]]というテンソルになっています。
num = SOFTMAX(pre.logits)にすることで、num[0]がネガティブである確率、num[1]がポジティブである確率を表すようになります。
we could get "pre.logits" as the output.
"pre.logits" is the shape like tensor[[x, y]].
"num = SOFTMAX(pre.logits)"
num[0] will show the probability of negative, num[1] will show the probability of positive.
1
2import torch
3
4from transformers import MLukeTokenizer
5
6from torch import nn
7
8tokenizer = MLukeTokenizer.from_pretrained('studio-ousia/luke-japanese-base-lite')
9
10model = torch.load('C:\\[My_luke_model_pn.pthのあるディレクトリ]\\My_luke_model_pn.pth')
11
12text=input()
13
14encoded_dict = tokenizer.encode_plus(
15 text,
16 return_attention_mask = True, # Attention masksの作成
17 return_tensors = 'pt', # Pytorch tensorsで返す
18 )
19
20pre = model(encoded_dict['input_ids'], token_type_ids=None, attention_mask=encoded_dict['attention_mask'])
21SOFTMAX=nn.Softmax(dim=0)
22
23num=SOFTMAX(pre.logits[0])
24
25if num[1]>0.5:
26 print(str(num[1]))
27 print('ポジティブ')
28
29else:
30 print(str(num[1]))
31 print('ネガティブ')
Acknowledgments 謝辞
Lukeの開発者である山田先生とStudio ousiaさんには感謝いたします。
I would like to thank Mr.Yamada @ikuyamada and Studio ousia @StudioOusia.
Citation
[1]@inproceedings{yamada2020luke,
title={LUKE: Deep Contextualized Entity Representations with Entity-aware Self-attention},
author={Ikuya Yamada and Akari Asai and Hiroyuki Shindo and Hideaki Takeda and Yuji Matsumoto},
booktitle={EMNLP},
year={2020}
}