aMLP Transformer Model for Japanese

Overview

aMLP-japanese

Japanese aMLP Pretrained Model

aMLPとは、Liu, Daiらが提案する、Transformerモデルです。

ざっくりというと、BERTの代わりに使えて、より性能の良いモデルです。

詳しい解説は、こちらの記事などを参考にしてください。

このプロジェクトは、スポンサーを募集しています

aMLP 日本語モデル

New

  • 2021/11/13 - 事前学習済みbaseモデルおよびSQuADモデルを公開しました

aMLP (Pay Attention to MLPs) とは

gMLPは、Liu, Daiらが、論文「Pay Attention to MLPs」で提案した、Self-Attention機構を排除したTransformerモデルです

BERTのTransformerモデルよりも、1層あたりのパラメーター数が少なく、その代わりに多数の層を重ねることで、同じパラメーター数あたりの性能で見て、BERTを超える性能を発揮します

ざっくりと「BERTと同じように使えてBERTより性能の良いモデル」と捉えて良いでしょう

aMLPは、gMLPにさらにSoft-Attention機構を追加することで、SQuAD等質疑応答タスクにおいてもBERTを超える性能を発揮すると報告されているモデルです

aMLP-japaneseとは、Tensorflow2で実装したaMLPモデルに、40GB超の日本語コーパスを事前学習させた、学習済みモデルです

日本語のエンコードにはJapanese-BPEEncoder_V2を使用し、トークン数は24Kです

TODO

✓baseモデルの公開(2021/11/13)
✓SQuADモデルの公開(2021/11/13)

公開モデル

  • 事前学習モデル
モデル名 ダウンロードURL パラメーター数 学習データサイズ
aMLP-base-ja https://nama.ne.jp/models/aMLP-base-ja.tar.bz2予備URL 67,923,648 40GB~
  • 質疑応答モデル
モデル名 ダウンロードURL パラメーター数 学習データサイズ
aMLP-SQuAD-base-ja https://nama.ne.jp/models/aMLP-SQuAD-base-ja.bz2予備URL 67,924,674 200K文章

質疑応答モデル

使い方

GitHubからコードをクローンします

$ git https://github.com/tanreinama/aMLP-japanese
$ cd aMLP-japanese

学習済みモデルファイルをダウンロードして展開します

$ wget https://www.nama.ne.jp/models/aMLP-SQuAD-base-ja.tar.bz2
$ tar xvfj aMLP-SQuAD-base-ja.tar.bz2

以下のように「run-squad.py」を実行します

学習済みモデルを「--restore_from」に、SQuAD形式のJSONファイルを「--pred_dataset」で指定すると、質問文に対する回答が表示されます

全ての解答の候補は、「squad-predicted.json」という名前で保存されます

$ python run-squad.py --restore_from aMLP-SQuAD-base-ja --pred_dataset squad-testdata.json
Question        Answer
ロッキード・マーティン社とボーイング社が共同開発したステルス戦闘機は?  F-22戦闘機
F-22戦闘機の愛称は?    猛禽類の意味のラプター
F-22戦闘機一機あたりの価格は?  1億5千万ドル
F-22戦闘機の航続距離は?        3200km
F-22戦闘機の巡航速度は?        マッハ1.82
F-22の生産数が削減された理由は?        調達コスト

SQuAD型の質疑応答モデルなので、JSONファイルにコンテキストが含まれている必要があります

ファインチューニング

ファインチューニング用の質疑応答データセットを用意して、SQuAD形式のJSONファイルで保存しておきます

そして、以下のように「run-squad.py」を実行します

学習済みモデルを「--restore_from」に、SQuAD形式のJSONファイルを「--dataset」で指定します

評価用のデータセットがあるときは、「--val_dataset」で指定すると、学習の途中で評価スコアが表示されます

$ python run-squad.py --restore_from aMLP-SQuAD-base-ja --dataset squad-testdata.json

一から学習させる場合は、事前学習済みモデルを「--base_model」に指定します

学習済みモデルは、「checkpoint」以下の、「--run_name」で指定したディレクトリ内に保存されます

なお、公開モデルの学習に使用した質疑応答データセットについては、著作権の関係から公開出来ません

クラス分類モデル

準備

GitHubからコードをクローンします

$ git https://github.com/tanreinama/aMLP-japanese
$ cd aMLP-japanese

事前学習済みモデルファイルをダウンロードして展開します

$ wget https://www.nama.ne.jp/models/aMLP-base-ja.tar.bz2
$ tar xvfj aMLP-base-ja.tar.bz2

学習

クラス分類タスクでは、

dir/<classA>/textA.txt
dir/<classA>/textB.txt
dir/<classB>/textC.txt
・・・

のように、「クラス名/ファイル」という形でテキストファイルが保存されている前提で、テキストファイルをクラス毎に分類するモデルを学習します

ここでは、livedoor ニュースコーパスを使用する例をサンプルとして提示します

まず、コーパスをダウンロードして展開すると、「text」以下に記事の入っているディレクトリが作成されます

$ wget https://www.rondhuit.com/download/ldcc-20140209.tar.gz
$ tar xvfz ldcc-20140209.tar.gz
$ ls text/

学習には、以下のように「run-classifier.py」を実行します

事前学習済みモデルを「--base_model」に、データセットのディレクトリを「--dataset」で指定します

評価用のデータセットがあるときは、「--val_dataset」で指定すると、学習の途中で評価スコアが表示されます

$ python run-classifier.py --dataset text --model aMLP-base-ja --clean_text

以下のようにサブディレクトリ名とクラスIDとの対応が表示された後、学習が進みます

Loading dataset...
livedoor-homme mapped index#0
kaden-channel mapped index#1
movie-enter mapped index#2
it-life-hack mapped index#3
topic-news mapped index#4
sports-watch mapped index#5
dokujo-tsushin mapped index#6
peachy mapped index#7
smax mapped index#8

学習済みモデルは、「checkpoint」以下の、「--run_name」で指定したディレクトリ内に保存されます

推論

推論には、以下のように「run-classifier.py」を実行します

学習済みモデルを「--restore_from」に、データセットのディレクトリを「--pred_dataset」、出力ファイルを「--output」で指定します

$ python run-classifier.py --pred_dataset text --output classifier-pred.csv --restore_from checkpoint/aMLP-classifier-ja/checkpoint-XXXX
$ head -n5 classifier-pred.csv
filename,pred,true
text/livedoor-homme/livedoor-homme-4956491.txt,livedoor-homme,livedoor-homme
text/livedoor-homme/livedoor-homme-5492081.txt,livedoor-homme,livedoor-homme
text/livedoor-homme/livedoor-homme-5818455.txt,livedoor-homme,livedoor-homme
text/livedoor-homme/livedoor-homme-6052744.txt,livedoor-homme,livedoor-homme

実行結果はCSVファイルで保存されます

テキストの穴埋め

Masked Language Modelとして実行します。aMLPのモデルは入力されたテキスト内の「[MASK]」部分を予測します

「run-mlm.py」で、直接穴埋め問題を解かせることが出来ます

「[MASK]」一つでエンコード後のBPE一つなので、「[MASK]」が日本語1文字から3文字になります

$ python run-mlm.py --context "俺の名前は坂本[MASK]。何処にでもいるサラリー[MASK]だ。" --model aMLP-base-ja
俺の名前は坂本だ。何処にでもいるサラリーマンだ。

文章のベクトル化

[CLS]トークンに対応するベクトル表現を得ます。「--output」を指定するとファイルにカンマ区切りのテキストでファイルに保存します

$ python run-vectrize.py --context "こんにちは、世界。" --model aMLP-base-ja
[1.777146577835083, 0.5332596898078918, 0.07858406007289886, 0.5532811880111694, 0.8075544238090515, 1.3260560035705566, 0.6111544370651245, 2.338435173034668, 1.0313552618026733, ・・・

REFERENCE

Hanxiao Liu, Zihang Dai, David R. So, Quoc V. Le "Pay Attention to MLPs" arXiv:2105.08050, 17 May 2021

Owner
tanreinama
日本語を扱うAIプロダクトを開発したいという企業様へ、お手伝いできるのでご連絡お待ちします。プロダクト企画を作成して開発チームを立ち上げるところから①からコミット出来ます。
tanreinama
Fidibo.com comments Sentiment Analyser

Fidibo.com comments Sentiment Analyser Introduction This project first asynchronously grab Fidibo.com books comment data using grabber.py and then sav

Iman Kermani 3 Apr 15, 2022
基于pytorch+bert的中文事件抽取

pytorch_bert_event_extraction 基于pytorch+bert的中文事件抽取,主要思想是QA(问答)。 要预先下载好chinese-roberta-wwm-ext模型,并在运行时指定模型的位置。

西西嘛呦 31 Nov 30, 2022
Translates basic English sentences into the Huna language (hoo-NAH)

huna-translator The Huna Language Translates basic English sentences into the Huna language (hoo-NAH). The Huna constructed language was developed in

Miles Smith 0 Jan 20, 2022
The model is designed to train a single and large neural network in order to predict correct translation by reading the given sentence.

Neural Machine Translation communication system The model is basically direct to convert one source language to another targeted language using encode

Nishant Banjade 7 Sep 22, 2022
Chinese Pre-Trained Language Models (CPM-LM) Version-I

CPM-Generate 为了促进中文自然语言处理研究的发展,本项目提供了 CPM-LM (2.6B) 模型的文本生成代码,可用于文本生成的本地测试,并以此为基础进一步研究零次学习/少次学习等场景。[项目首页] [模型下载] [技术报告] 若您想使用CPM-1进行推理,我们建议使用高效推理工具BMI

Tsinghua AI 1.4k Jan 03, 2023
NLP-based analysis of poor Chinese movie reviews on Douban

douban_embedding 豆瓣中文影评差评分析 1. NLP NLP(Natural Language Processing)是指自然语言处理,他的目的是让计算机可以听懂人话。 下面是我将2万条豆瓣影评训练之后,随意输入一段新影评交给神经网络,最终AI推断出的结果。 "很好,演技不错

3 Apr 15, 2022
Optimal Transport Tools (OTT), A toolbox for all things Wasserstein.

Optimal Transport Tools (OTT), A toolbox for all things Wasserstein. See full documentation for detailed info on the toolbox. The goal of OTT is to pr

OTT-JAX 255 Dec 26, 2022
Machine Psychology: Python Generated Art

Machine Psychology: Python Generated Art A limited collection of 64 algorithmically generated artwork. Each unique piece is then given a title by the

Pixegami Team 67 Dec 13, 2022
Creating a Feed of MISP Events from ThreatFox (by abuse.ch)

ThreatFox2Misp Creating a Feed of MISP Events from ThreatFox (by abuse.ch) What will it do? This will fetch IOCs from ThreatFox by Abuse.ch, convert t

17 Nov 22, 2022
Python library for processing Chinese text

SnowNLP: Simplified Chinese Text Processing SnowNLP是一个python写的类库,可以方便的处理中文文本内容,是受到了TextBlob的启发而写的,由于现在大部分的自然语言处理库基本都是针对英文的,于是写了一个方便处理中文的类库,并且和TextBlob

Rui Wang 6k Jan 02, 2023
This library is testing the ethics of language models by using natural adversarial texts.

prompt2slip This library is testing the ethics of language models by using natural adversarial texts. This tool allows for short and simple code and v

9 Dec 28, 2021
A collection of GNN-based fake news detection models.

This repo includes the Pytorch-Geometric implementation of a series of Graph Neural Network (GNN) based fake news detection models. All GNN models are implemented and evaluated under the User Prefere

SafeGraph 251 Jan 01, 2023
chaii - hindi & tamil question answering

chaii - hindi & tamil question answering This is the solution for rank 5th in Kaggle competition: chaii - Hindi and Tamil Question Answering. The comp

abhishek thakur 33 Dec 18, 2022
ChessCoach is a neural network-based chess engine capable of natural-language commentary.

ChessCoach is a neural network-based chess engine capable of natural-language commentary.

Chris Butner 380 Dec 03, 2022
WIT (Wikipedia-based Image Text) Dataset is a large multimodal multilingual dataset comprising 37M+ image-text sets with 11M+ unique images across 100+ languages.

WIT (Wikipedia-based Image Text) Dataset is a large multimodal multilingual dataset comprising 37M+ image-text sets with 11M+ unique images across 100+ languages.

Google Research Datasets 740 Dec 24, 2022
Ukrainian TTS (text-to-speech) using Coqui TTS

title emoji colorFrom colorTo sdk app_file pinned Ukrainian TTS 🐸 green green gradio app.py false Ukrainian TTS 📢 🤖 Ukrainian TTS (text-to-speech)

Yurii Paniv 85 Dec 26, 2022
Contains descriptions and code of the mini-projects developed in various programming languages

TexttoSpeechAndLanguageTranslator-project introduction A pleasant application where the client will be given buttons like play,reset and exit. The cli

Adarsh Reddy 1 Dec 22, 2021
Finally decent dictionaries based on Wiktionary for your beloved eBook reader.

eBook Reader Dictionaries Finally, decent dictionaries based on Wiktionary for your beloved eBook reader. Dictionaries Catalan 🚧 Ελληνικά (help welco

Mickaël Schoentgen 163 Dec 31, 2022
This repo contains simple to use, pretrained/training-less models for speaker diarization.

PyDiar This repo contains simple to use, pretrained/training-less models for speaker diarization. Supported Models Binary Key Speaker Modeling Based o

12 Jan 20, 2022
This is an incredibly powerful calculator that is capable of many useful day-to-day functions.

Description 💻 This is an incredibly powerful calculator that is capable of many useful day-to-day functions. Such functions include solving basic ari

Jordan Leich 37 Nov 19, 2022