サイトのデザインが新しくなりました。

【Gemma】”Llama 2超え”と噂のGoogleのオープンソースLLMの性能を徹底比較してみた

Gemma Llama 2 Google オープンソースLLM

WEELメディア事業部LLMリサーチャーの中田です。

2月21日、オープンソースの大規模言語モデル「Gemma」を、Googleが公開しました。

このモデルはGeminiをもとに設計されており、軽量ながら高精度なんです!

Google DeepMindの公式Xでの投稿のいいね数は、すでに2000を超えており、かなり期待されていることが分かります。

この記事ではGemmaの使い方や、有効性の検証まで行います。本記事を熟読することで、Gemmaの凄さを理解し、昨今のGoogleの勢いを肌で感じられるでしょう。

ぜひ、最後までご覧ください。

なお弊社では、生成AIツール開発についての無料相談を承っています。こちらからお気軽にご相談ください。

目次

Gemmaの概要

Gemmaは、Googleが開発した最新のオープンソースLLMです。現在は、2Bおよび7Bパラメータのモデルが公開されており、様々なタスクでの使用が可能です。

Gemmaの語源は「宝石」から来ているとされています。

また、公式テクニカルレポートによると、Gemmaモデルは最近話題のGemini(Transformerのデコーダ)をもとに構築されており、言語理解や推論などに関するベンチマークで、高いパフォーマンスを示します。

参考:https://storage.googleapis.com/deepmind-media/gemma/gemma-report.pdf
参考:https://blog.google/
参考:https://huggingface.co/blog/gemma

特に、Gemmaは軽量でありながら、その他のモデルの性能よりも高いのが特徴的です。

加えて、深層学習フレームワーク「Keras 3.0」を介して、JAX、PyTorch、TensorFlow など、すべての主要なフレームワークにわたって、「推論」と「ファインチューニング」を実行できるとのこと。また、Colabおよび Kaggleノートブックに加え、Hugging FaceやNVIDIA などの人気プラットフォームと統合しているので、Gemmaを簡単に始めることができます。

現在、Gemmaでは以下の4つのモデルが公開されています。

  • gemma-7b: Base 7B model.
  • gemma-7b-it: Instruction fine-tuned version of the base 7B model.
  • gemma-2b: Base 2B model.
  • gemma-2b-it: Instruction fine-tuned version of the base 2B model.

Gemmaモデルは、2Tおよび6Tトークンの主に英語のデータで学習されています。

ちなみに、NVIDIA公式によると、「最近公開されたChat with RTXで、近々Gemmaを利用可能にする」とのこと。

なお、GPT-4を超えるGoogleのオープンソースLLMについて詳しく知りたい方は、下記の記事を合わせてご確認ください。
【switch-c-2048】GoogleがついにGPT-4レベルのLLMをオープンソースで公開!概要〜使い方まで

Gemmaのライセンス

公式ページGemma Terms of Useによると、誰でも無償で商用利用することが可能です。

利用用途可否
商用利用⭕️
改変⭕️
配布⭕️
特許使用Gemma Terms of Useに記載なし
私的使用⭕️
参考:https://ai.google.dev/gemma/terms

Gemmaの使い方

今回は、公式のHuggingFaceのブログページにあるやり方を参考に、Google Colab上(A100サーバ)で実行していきます。手軽に試したい方は、HuggingChatのデモページで簡単に実行できます。

まず、モデルカードのページを開き、HuggingFaceにログインした後、「Acknowledge license」ボタンをクリックして、ライセンスを認証しましょう。

参考:https://huggingface.co/google/gemma-7b-it

次に、以下のコードを実行して、ライブラリをインストールしましょう。

!pip install -U "transformers==4.38.0"

次に、左端の鍵アイコンに移動し、「HF_TOKEN」にHuggingFaceのトークンを設定し有効化しましょう。

モデルやトークナイザーのロードを完了させましょう。

from transformers import AutoTokenizer, pipeline
import torch

model = "google/gemma-7b-it"

tokenizer = AutoTokenizer.from_pretrained(model)
pipeline = pipeline(
    "text-generation",
    model=model,
    model_kwargs={"torch_dtype": torch.bfloat16},
    device="cuda",
)

以下のコードを実行すると、推論が実行されます。

messages = [
        {"role": "user", "content": "Who are you? Please, answer in pirate-speak."}, # プロンプト
]
prompt = pipeline.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
outputs = pipeline(
    prompt,
    max_new_tokens=256,
    add_special_tokens=True,
    do_sample=True,
    temperature=0.7,
    top_k=50,
    top_p=0.95
)
print(outputs[0]["generated_text"][len(prompt):])

試しに、以下のプロンプトを入力し、実行してみます。

Who is the Prime Minister of Japan?

和訳:
日本の総理大臣は誰ですか?

結果は以下の通りです。

As of today, October 26, 2023, the Prime Minister of Japan is Fumio Kishida.

ちゃんと「岸田文雄」と答えてくれています。

Gemmaを動かすのに必要なPCのスペック

こちらは、Gemma-7b-itを動かした場合のスペックです。

■Pythonのバージョン
Python3.8以上

■使用ディスク量
17.07GB

■RAMの使用量
18GB

なお、Googleの音声生成AIについて詳しく知りたい方は、下記の記事を合わせてご確認ください。
【AudioPaLM】Googleの神音声AIツール、導入から使い方まで解説

GemmaとLlama 2を比較してみた

ここでは、テクニカルレポートでも良く比較対象として挙げられていた「Llama 2 70B」とGemmaを、以下のタスクで比較しようと思います。

  • 数学能力
  • コーディング能力
  • 日本語能力

それでは順番に見ていきましょう。

数学能力

深津貴之さんがXで、以下の様に「もしかしたら数学能力が高くなっているかもしれない」という旨のツイートしていました。

そこで、「100以下の素数を挙げて、それらすべての数字を足し合わせたもの」を出力させてみます。プロンプトは以下の通りです。

Please list and add up all prime numbers below 100.

和訳:
100以下の素数をすべて挙げて足してください。

ちなみに答えは、以下の通りです。

2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97

When added up, they sum to 1060.

Gemmaの答えは以下の通りです。

Sure, here are the prime numbers below 100:

2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 33, 37, 41, 43, 47, 49, 51, 53, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97

Total number of prime numbers below 100 = 25

Total sum of prime numbers below 100 = 2035

49や63など、非素数が含まれている可能性があり、正確さに欠ける結果となることがあります。

Llama 2の答えは以下の通りです。

Sure, I'd be happy to help! Here are the prime numbers below 100:
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97
The sum of these prime numbers is 837.

こちらも間違いです。

コーディング能力

次に、コーディング能力を試すために、「ボストン住宅価格データセットを用いた回帰分析」を実行してもらいます。使用したプロンプトは、以下の通りです。

Please perform a regression analysis in Python using the Boston home price dataset with as much accuracy as possible.

和訳:
ボストン住宅価格データセットを用いて、Pythonにより、なるべく精度の高い回帰分析を行ってください。

Gemmaの答えは以下の通りです。

import pandas as pd
import numpy as np
from sklearn.linear_model import LinearRegression

# Load the Boston home price dataset
df = pd.read_csv('boston.csv')

# Split the dataset into X and y variables
X = df.drop('price', axis=1)
y = df['price']

# Create and fit the linear regression model
model = LinearRegression()
model.fit(X, y)

# Make predictions
y_pred = model.predict(X)

# Print the coefficients
print('Coefficients:', model.coef_)

# Print the mean squared error
print('Mean Squared Error:', np.mean((y_pred - y)**2))

# Print the R-squared value
print('R-Squared:', model.score(X, y))

#Output:

Coefficients: [ 0.71929232 -0.04383986 -0.0782692 ]
Mean Squared Error: 122.881128001
R-Squared: 0.632612

Explanation:

The pandas library is used to read and manipulate the dataset.
The numpy library is used for numerical operations.
The sklearn.linear_model library is used to create and fit the linear regression model.
The LinearRegression class is used to create the model.
The fit method is used to fit the model to the data.
The coef_ attribute is used to get the coefficients of the model.
The mean_squared_error attribute is used to get the mean squared error of the model.
The score attribute is used to get the R-squared value of the model.
Note:

The results of the regression analysis may vary slightly depending on the dataset and the parameters used for the model fitting.
For improved accuracy, you can try different preprocessing techniques or use a different model type.
The Boston home price dataset is a commonly used dataset for regression analysis and has a relatively high level of accuracy.

このままのコードでは、データセットをロードしないので、あらかじめボストン住宅価格データセットのcsvを用意します。そのうえで、こちらのコードをGoogle Colab上で実行したところ、以下のようなエラーが発生し、実行できなかったです。

KeyError: "['price'] not found in axis"

Llama 2の答えは以下の通りです。

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score
# Load the Boston home price dataset
boston_data = pd.read_csv('boston_housing.csv')
# Split the dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(boston_data.drop('price', axis=1), boston_data['price'], test_size=0.3)
# Create a linear regression model
model = LinearRegression()
# Train the model on the training data
model.fit(X_train, y_train)
# Make predictions on the testing data
y_pred = model.predict(X_test)
# Evaluate the model's performance
mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)
print(f'Mean squared error: {mse:.2f}')
print(f'R-squared: {r2:.2f}')
# Plot the data and the predicted values
plt.scatter(X_test, y_test, c='blue')
plt.scatter(X_test, y_pred, c='red')
plt.xlabel('Feature 1')
plt.ylabel('Price')
plt.show()
The output of the code will show the mean squared error and R-squared value for the linear regression model. The mean squared error represents the average squared difference between the predicted values and the actual values. The R-squared value represents the proportion of the variance in the dependent variable that is explained by the independent variable(s).
In this case, the mean squared error is 10.28, indicating that the model is not doing a great job of predicting the housing prices. The R-squared value is 0.67, indicating that about 67% of the variance in the housing prices can be explained by the independent variables.
The plot of the data and the predicted values will also be displayed, showing how well the model is able to fit the data.
It's important to note that this is just one example of a linear regression model, and there are many other factors that can affect the accuracy of the model, such as the quality of the data, the choice of independent variables, and the choice of model.

こちらを実行したところ、先ほどのGemmaの時と同じエラーが発生しました。

日本語能力

最後に、日本語能力を試すために、和歌を出力させてみます。使用したプロンプトは、以下の通りです。

Please create a waka about summer in Japanese.

和訳:
日本語で、夏を題材にした和歌を作成してください。

Gemmaの答えは以下の通りです。

夏はこれから始まる、陽が強く熱が強く
子供たち笑顔がどこに、汗が流れます。
みんなみんな笑顔を pathlib、楽しい思い出を作ります。
ジャンプとダンス、遊びと笑いが、楽しいです。
たくさんの笑顔が聞こえて、これから始まる。

和歌にはなっていません。

Llama 2の答えは以下の通りです。

Sure, here's a waka about summer in Japanese:
夏の日の輝き
natsu no hi no kagayaki
The brightness of the summer day
緑の葉の茂み
midori no ha no matsumi
The lushness of the green leaves
暑さを忘れて
atsui sa o wasurete
Forgetting the heat
水の笑顔
mizu no egao
The smile of the water
夏の宵の広がり
natsu no yoi no hirogaru
The wide, summer night sky
星の輝き
hoshi no kagayaki
The twinkling of the stars
涼しい風
suzushii kaze
A refreshing breeze
夏の夜の暁
natsu no yoru no akatsuki
The early morning of a summer night
紅色の日の落
beni-iro no hi no ochi
The setting of the red sun
夏の季節の
natsu no kisetsu no
The season of summer
熱い情熱
atsui jounetsu
Burning passion
I hope you enjoy this waka!

こちらも和歌にはなっておらず、英語も混じっています。

なお、数学オリンピックの問題も解けるGoogleのAIについて詳しく知りたい方は、下記の記事を合わせてご確認ください。
【AlphaGeometry】Googleが数学オリンピック優勝レベルの難問を解く生成AIを公開!実際に使ってみた

GoogleのAI技術に今後も注目!

GemmaはGoogleによって開発された最新のオープンソースの大規模言語モデルで、2Bおよび7Bパラメータモデルが公開されています。

名前の由来は「宝石」で、Gemini(Transformerのデコーダ)を基にしており、言語理解や推論のベンチマークで優れた性能を発揮します。Gemmaは軽量でありながら高性能で、JAX、PyTorch、TensorFlowなど主要なフレームワークに対応し、Keras 3.0を通じて推論とファインチューニングが可能です。

NVIDIAは近々、GemmaをChat with RTXで使用可能にすると発表しています。

GemmaとLlama 2の比較検証からは、両者ともに改善の余地があることが示唆されます。やはり、ハルシネーションが多く、正確でない答えを出力しがちなので、今後のアップデートに注目といったところでしょう。

タスクGemmaLlama 2
数学能力(素数判定)間違いを出力した間違いを出力した
コーディング能力(回帰分析)そのままでは動かないコードが出力されたそのままでは動かないコードが出力された
日本語能力(和歌作成)和歌になっていなかった和歌になっておらず、出力に英語が混じっていた

ちなみに、あるXユーザーによると、「Mistral 7bはGemma 7bより優れており、Gemmaはハルシネーションを起こしやすい」とのこと。

最後に

いかがだったでしょうか?

弊社では

・マーケティングやエンジニアリングなどの専門知識を学習させたAI社員の開発
・要件定義・業務フロー作成を80%自動化できる自律型AIエージェントの開発
・生成AIとRPAを組み合わせた業務自動化ツールの開発
・社内人事業務を99%自動化できるAIツールの開発
ハルシネーション対策AIツールの開発
自社専用のAIチャットボットの開発

などの開発実績がございます。

まずは、「無料相談」にてご相談を承っておりますので、ご興味がある方はぜひご連絡ください。

➡︎生成AIを使った業務効率化、生成AIツールの開発について相談をしてみる。

生成AIを社内で活用していきたい方へ

「生成AIを社内で活用したい」「生成AIの事業をやっていきたい」という方に向けて、生成AI社内セミナー・勉強会をさせていただいております。

セミナー内容や料金については、ご相談ください。

また、弊社紹介資料もご用意しておりますので、併せてご確認ください。

投稿者

  • 中田

    データサイエンス専攻の大学院生。大学では、生成系AIの拡散モデルを用いた音楽生成について研究。 趣味は作曲、サッカー、コーヒー。

  • URLをコピーしました!
  • URLをコピーしました!
目次