LoginSignup
16
19

More than 1 year has passed since last update.

AutoTrainでテキスト分類

Last updated at Posted at 2022-04-09

AutoTrain🚂🚂🚂とは

ノーコードでテキスト分類や要約などがstate-of-the-artできるサービスです。AutoNLPだとググラビリティが低かったので名称が変わったのだと思います。
image.png

データ準備

livedoorニュースコーパスのタイトルと本文を結合して、9つのカテゴリを分類しようと思います。

!wget https://www.rondhuit.com/download/ldcc-20140209.tar.gz
!tar xf ldcc-20140209.tar.gz

import glob
import pandas as pd

data = []

for path in glob.glob('text/**/*-*.txt'):
  with open(path) as f:
    data.append({
        'url': next(f).strip(),
        'datetime': next(f).strip(),
        'title': next(f).strip(),
        'body': '\n'.join([line.strip() for line in f if line.strip()]),
        'label': path.split('/')[1]
    })

df = pd.DataFrame(data)
df['title_body'] = df["title"] + '\n' + df["body"]
df.to_csv('livedoor_news.csv', index=False)

コードはこちら

プロジェクトの作成

Text Classification (Multi-class)Japaneseを選択し、プロジェクトを作成します。

image.png

CSVファイルをアップロード

CSVファイルをドラッグ・アンド・ドロップし、text:title_bodytarget:labelカラムを選択し、プロジェクトに追加します。

image.png
image.png
image.png

Go to trainingsをクリックします。
image.png

学習

Start models trainingsをクリックします。
image.png

推論

94.5%の精度で分類できました。modelをクリックし、model hubで推論できます。
image.png
image.png

右側の入力ボックスで簡単に試すことができます。

https://huggingface.co/jicoc22578/autotrain-livedoor_news-722922024

image.png
image.png

モデルをダウンロード

モデルをダウンロードすることでオフラインでも推論できます。

モデルをダウンロード

!curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | sudo bash
!sudo apt-get -qq install git-lfs
!git lfs install
#!git clone "https://$USERNAME:$PASSWORD@huggingface.co/jicoc22578/autotrain-livedoor_news-722922024" # set username/password for private model
!git clone https://huggingface.co/jicoc22578/autotrain-livedoor_news-722922024

pipelineで推論

!pip install -q transformers fugashi unidic_lite
from transformers import pipeline

text = 'Windows 11搭載PCを買ったら最低限やっておきたいこと'
pipeline("text-classification", "autotrain-livedoor_news-722922024")(text)
#[{'label': 'it-life-hack', 'score': 0.8320314884185791}]

2値分類

!git clone https://huggingface.co/abhishek/autonlp-japanese-sentiment-59363
predict = pipeline("text-classification", "autonlp-japanese-sentiment-59363")
predict('何もしてないのに壊れた')
# [{'label': 'negative', 'score': 0.9975911378860474}]
predict('この商品はとても良いです🤗')
# [{'label': 'positive', 'score': 0.9995431900024414}]

image.png
image.png

まとめ

  • クレカ登録しない限り無料で使えてとても便利
  • curlでpython環境がなくても使える
curl -X POST -H "Authorization: Bearer $YOUR_API_KEY" -H "Content-Type: application/json" -d '{"inputs": "raytrek、Core i7-12700HとRTX 3070 Tiを搭載するノートPC"}' https://api-inference.huggingface.co/models/jicoc22578/autotrain-livedoor_news-722922024
  • AutoML Natural LanguageだとモデルをダウンロードできないがAutoTrainならダウンロードできてオフライン環境で動かせる。predictにお金がかからない。

AutoML Natural Language

同じデータをAutoML Natural Languageに入れてみました。
6時間の学習したところ、平均適合率が0.994になりました:rocket:
2,400円かかりますが、精度を求めるのであらばオススメです。
image.png

image.png
image.png

more

image.png
image.png
image.png
image.png
image.png

16
19
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
19