1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Baichuan2

Last updated at Posted at 2023-09-25

はじめに

今回はBaichuan2の紹介になります.Baichuan Intelligenceという中国の企業が開発した新しいオープンソースのLLMになります.中国語,英語,多言語において幾つかのデータセットで同じパラメータ数を持つモデルの中で最高精度を出したモデルになります.

記事に誤り等ありましたらご指摘いただけますと幸いです。

目次

1. Baichuan2

ライセンス:Apache-2.0(研究では自由に使ってよく,商用利用したい場合はメールで問い合わせる必要があるそうです)
リポジトリ:https://github.com/baichuan-inc/Baichuan2/tree/main
公式サイト:https://www.baichuan-ai.com/home
論文:https://arxiv.org/abs/2309.10305

英語に特化しているモデルが多い中,中国語に特化させたモデルを開発.数学やコード生成,医療や法律などの専門的な領域で高い性能を発揮しているそうです.日本語も学習データに含まれているため日本語の出力も行うことができます.

公開されているモデル(2023/9/26)

2. 使い方

すぐに試したい方はData Science WikiのページまたはColabのリンクから実行してみてください

Data Science Wiki

Open In Colab

今回はBaichuan2-13B-Chatを使います.GPUは30GBほどのメモリ消費でしたのでGoogle ColabのA100(40G)などで動かせます.

必要なライブラリをインストールします.

!pip install transformers_stream_generator sentencepiece bitsandbytes accelerate
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer

モデルのダウンロード

tokenizer = AutoTokenizer.from_pretrained("baichuan-inc/Baichuan2-13B-Chat", use_fast=False, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained("baichuan-inc/Baichuan2-13B-Chat", device_map="auto", torch_dtype=torch.bfloat16, trust_remote_code=True)
model.generation_config = GenerationConfig.from_pretrained("baichuan-inc/Baichuan2-13B-Chat")

日本語での質問

messages = []
messages.append({"role": "user", "content": "機械学習について教えてください"})
response = model.chat(tokenizer, messages)
print(response)

# 出力
機械学習は人工智能の一つの分野ですその目的はデータからプログラミングによる手動の介入を必要としないアルゴリズムやモデルを作成することです機械学習は画像認識音声認識自然言語処理推論などさまざまな分野で活用されています

機械学習の基盤は数学特に線形代数概率論統計学)、電子情報工学物理学心理学生物学社会科学など多くの学問が交差しています

機械学習の主要な手法の1つは回帰分析ですこれは2次元の平面図上の点群から直線または回帰線を引くことで各点の特徴量x座標y座標と予測したい特徴量yの間の関係を捉える方法です

また機械学習の手法の1つは決定木Decision Treeです決定木は木の幹葉の形を擬ねたものでデータの中でどのような決定を下すかを表現するための方法です決定木は予測結果が理解できるような明確な形で予測結果を出します

最も知られている機械学習手法の1つは神经網Neural Networkですこれは人の神経細胞の組み合わせを参考にしたもので複数の層が存在し各層のノードは相手のノードと通信し合い情報を伝達することです神经網は画像認識音声認識自然言語処理などさまざまな分野で活用されています

機械学習の実行にはPythonのScikit-learnTensorFlowKerasなどの機械学習ライブラリが利用できます

よい出力が得られました.日本語で利用する上でも十分に使える精度だと思われます.一部漢字の出力について日本語に無い自体で出力されました.

英語での質問

messages = []
messages.append({"role": "user", "content": "Please tell me about quantum computers."})
response = model.chat(tokenizer, messages)
print(response)

# 出力
Quantum computers are a relatively new and rapidly developing technology that uses the principles of quantum mechanics to perform calculations and solve problems. They are different from traditional digital computers, which use binary bits (0s and 1s) to represent information, in that they use qubits (quantum bits) as their basic units.

Here are some key features and differences between classical and quantum computers:

1. Speed: Quantum computers are believed to be exponentially faster than classical computers for certain tasks, such as factoring large numbers or searching large databases. This is because they can exploit the superposition and entanglement properties of qubits to perform complex calculations in parallel.

2. Simultaneous calculation: In a quantum computer, all qubits can be in multiple states at the same time, allowing them to process and analyze vast amounts of data simultaneously. This is known as quantum parallelism and can significantly increase the computational power of the machine.

3. Entanglement: Quantum computers can entangle qubits together, meaning that changing the state of one qubit will instantaneously affect the state of the other qubits. This allows for more efficient and accurate computations, but also makes them more vulnerable to interference and noise.

4. Stability: Due to their delicate nature, quantum computers require extremely low temperatures and specialized environments to prevent external interference. This makes them more difficult to build and maintain than classical computers.

5. Applications: Quantum computers have the potential to revolutionize various fields, including cryptography (with the ability to break existing encryption schemes), optimization problems, drug discovery, and artificial intelligence. However, it is still early days for the technology, and many challenges need to be overcome before it can be widely deployed.

In summary, quantum computers are an exciting new development with the potential to transform computing and solve problems that are currently considered unsolvable by classical machines. However, they are still in their infancy and require significant research and development before they can be fully realized.

非常に精度よく回答できています.この辺りの回答が得意なのは科学技術に関するデータを多く学習できていることによるのかも知れません.

コード生成(日本語)

messages = []
messages.append({"role": "user", "content": "ロジスティック回帰を学習するためのPythonのコードを生成してください."})
response = model.chat(tokenizer, messages)
print(response)

# 出力
以下はPythonでロジスティック回帰を学習するための簡単なサンプルコードですこのコードは`sklearn` パッケージを使用します

# 必要なライブラリのインポート
import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, classification_report

# データの読み込み
# ここでは、データフレーム形式の入力データを想定
X = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9],
    [10, 11, 12]
]
y = [0, 1, 0, 1]  # 0: クラスA, 1: クラスB

# 学習データとテストデータに分割
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# ロジスティック回帰モデルの作成
log_reg = LogisticRegression()

# モデルを学習させる
log_reg.fit(X_train, y_train)

# 予測結果を出力
y_pred = log_reg.predict(X_test)
print("予測結果:", y_pred)

# 評価指標の算出
accuracy = accuracy_score(y_test, y_pred)
print("準確率:", accuracy)
print("分類報告:")
print(classification_report(y_test, y_pred))


このコードは入力データ `X` と目標変数 `y` から学習データとテストデータを作成しロジスティック回帰モデルを学習させそれらの評価指標を算出します

こちらについても非常にうまく生成できています.コード生成に関してはGPT-4レベルで行えている気がします.

計算

messages = []
messages.append({"role": "user", "content": "4367+3291="})
response = model.chat(tokenizer, messages)
print(response)

# 出力
4367 + 3291 = 7658

messages = []
messages.append({"role": "user", "content": "310×19="})
response = model.chat(tokenizer, messages)
print(response)

# 出力
310乘以19等于5890

足し算はかなり大きい桁まで,掛け算は3桁×3桁ぐらいまでなら計算はできました.

3. おわりに

今回はBaichuan2の紹介でした.中国語がメインのLLMですが日本語でも十分に精度が高い出力を得られることができました.一部,漢字については日本にない漢字を使って出力されるケースがありましたが問題なく出力された文は読むことができます.コード生成については非常に出力の精度が高く,学習データで多く使っている影響が出ていると思われます.
記事に誤り等ありましたらご指摘いただけますと幸いです。

4. 参考文献

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?