GPT-2, BERT を使ってみる(huggingface/transformers を利用)(Google Colab あるいは Windows あるいは Ubuntu 上)

Google Colab へのリンク

Google Colaboratory のページ:

次のリンクをクリックすると,Google Colaboratoryノートブックが開く. そして,Google アカウントでログインすると,Google Colaboratory のノートブック内のコード等を編集したり再実行したりができる.編集した場合でも,他の人に影響が出たりということはない.そして,編集後のものを,各自の Google ドライブ内に保存することもできる.

https://colab.research.google.com/drive/1mBFygBbUk4lBOov7TVyiat441MHwla3j?usp=sharing

前準備

Python のインストール(Windows上)

注:既にPython(バージョン3.12を推奨)がインストール済みの場合は,この手順は不要である.

winget(Windowsパッケージマネージャー)を使用してインストールを行う

  1. Windowsで,コマンドプロンプト管理者権限で起動する(例:Windowsキーを押し,「cmd」と入力し,「管理者として実行」を選択)
  2. winget(Windowsパッケージマネージャー)が利用可能か確認する:
    winget --version
    
  3. Pythonのインストール(下のコマンドにより Python 3.12 がインストールされる).
    winget install --scope machine Python.Launcher
    winget install --scope machine Python.Python.3.12
    
  4. 【関連する外部サイト】

    【サイト内の関連ページ】

    TensorFlow,Keras のインストール

    Windows での TensorFlowKeras のインストール: 別ページ »で説明

    (このページで,Build Tools for Visual Studio 2022,NVIDIA ドライバ, NVIDIA CUDA ツールキットNVIDIA cuDNNのインストールも説明している.)

    Graphviz のインストール

    Windows での Graphviz のインストール: 別ページ »で説明

    numpy,matplotlib, seaborn, scikit-learn, pandas, pydot のインストール

    1. Windows で,コマンドプロンプト管理者権限で起動する(例:Windowsキーを押し,「cmd」と入力し,「管理者として実行」を選択)

      次のコマンドを実行する.

      python -m pip install -U numpy matplotlib seaborn scikit-learn pandas pydot
      

    GraphViz のインストール

    huggingface/transformers の GPT-2 を動かしてみる

    huggingface/transformers の URL: https://huggingface.co/transformers/ huggingface/transformers の GitHub の URL: https://github.com/huggingface/transformers

    1. transformers のインストール

      次のページに記載の手順に従う:https://huggingface.co/transformers/installation.html

      Windows では,コマンドプロンプトを管理者として開き次のコマンドを実行する.

      python -m pip install transformers
      
    2. 英語で学習済みの GPT-2 を使ってみる

      次のページに記載のソースコードからビルドして,インストールする.(詳細説明も次のページにある): https://huggingface.co/gpt2

      Python プログラムを動かすために, pythonpython3などのコマンドを使う. あるいは, 開発環境や Python コンソール(Jupyter Qt ConsoleSpyderPyCharmPyScripter など)も便利である.

      次のプログラムは,テキスト生成(与えられた文章から,続きのトークンを生成)を行っている.

      from transformers import pipeline, set_seed
      generator = pipeline('text-generation', model='gpt2')
      set_seed(42)
      generator("Hello, I'm a language model,", max_length=30, num_return_sequences=5)
      

      次のプログラムは,特徴(features )の取得を行っている.

      from transformers import GPT2Tokenizer, TFGPT2Model
      tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
      model = TFGPT2Model.from_pretrained('gpt2')
      text = "Replace me by any text you'd like."
      encoded_input = tokenizer(text, return_tensors='tf')
      output = model(encoded_input)
      
    3. 英語で学習済みの BERT を使ってみる

      次のページに記載のソースコードからビルドして,インストールする.(詳細説明も次のページにある): https://huggingface.co/bert-base-uncased

      Python プログラムを動かすために, pythonpython3などのコマンドを使う. あるいは, 開発環境や Python コンソール(Jupyter Qt ConsoleSpyderPyCharmPyScripter など)も便利である.

      次のプログラムは,マスクを埋める(fill mask)ことを行っている.

      from transformers import pipeline
      unmasker = pipeline('fill-mask', model='bert-base-uncased')
      unmasker("Hello I'm a [MASK] model.")