谷歌发布了日语版的 Gemma2 模型——gemma-2-2b-jpn-it

在这里插入图片描述

Gemma 是一系列同类最佳的开放式模型,其灵感和技术源自 Gemini 系列模型。 它们是具有开放权重的文本到文本、纯解码器大型语言模型。 Gemma 模型非常适合各种文本生成任务,包括问题解答、摘要和推理。

Gemma-2-JPN 是一个针对日语文本进行微调的 Gemma 2B 模型。 它支持日语,其性能与 Gemma 2 上的英语查询性能相同。

使用

下面我们分享一些如何快速开始运行模型的代码片段。 首先,用以下命令安装 Transformers 库:

pip install -U transformers

pipeline API

import torch
from transformers import pipeline

pipe = pipeline(
    "text-generation",
    model="google/gemma-2-2b-jpn-it",
    model_kwargs={"torch_dtype": torch.bfloat16},
    device="cuda",  # replace with "mps" to run on a Mac device
)

messages = [
    {"role": "user", "content": "マシーンラーニングについての詩を書いてください。"},
]

outputs = pipe(messages, return_full_text=False, max_new_tokens=256)
assistant_response = outputs[0]["generated_text"].strip()
print(assistant_response)

输出

## マシーンラーニングの詩

**1.** 
データの海、深淵の広がり、
複雑なパターン、隠された知識。
機械学習、その力強さ、
未来を予測、その道を開く。

**2.** 
ニューラルネットワーク、複雑な枝、
学習の旅、その過程は静か。
データから学び、進化する姿、
予測の精度、その力強さ。

**3.** 
教師あり学習、正解を導く、
教師なし学習、未知の世界へ。
機械学習、その進化は止まらない、
未来の扉を開く、新たな時代へ。

**4.** 
画像認識、音声認識、
複雑なタスク、その答えを見つける。
機械学習、その力強さ、
未来の技術、その可能性を語る。

它还可用于翻译,具体如下:

translation_input_text = f"Translate the following poem from Japanese to English:\n\n{assistant_response}"
messages = [
    {"role": "user", "content": translation_input_text},
]

outputs = pipe(messages, return_full_text=False, max_new_tokens=1024)
translated_response = outputs[0]["generated_text"].strip()
print(translated_response)

输出

## A Poem About Machine Learning

**1.**
A vast ocean of data, a deep expanse,
Complex patterns, hidden knowledge.
Machine learning, its strength so vast,
Predicting the future, opening the way.

**2.**
A neural network, with branches intricate,
A journey of learning, its process serene.
Learning from data, evolving in its form,
The precision of prediction, its strength.

**3.**
Supervised learning, guiding the correct answer,
Unsupervised learning, venturing into the unknown.
Machine learning, its evolution never ends,
Opening the doors to the future, a new era.

**4.**
Image recognition, speech recognition,
Complex tasks, finding the answer.
Machine learning, its strength so vast,
The possibilities of future technology, a story to be told.




**Explanation:**

The poem uses vivid imagery and metaphors to describe the power and potential of machine learning. 

* **Data as an ocean:**  Represents the vast amount of information available for learning.
* **Complex patterns:**  Highlights the intricate nature of data and the challenges of extracting meaningful insights.
* **Future prediction:**  Emphasizes the ability of machine learning to analyze data and make predictions about the future.
* **Neural network as a tree:**  Represents the interconnectedness and complexity of the learning process.
* **Learning from data:**  Focuses on the core principle of machine learning, where algorithms learn from data to improve their performance.



The poem concludes by highlighting the diverse applications of machine learning, such as image and speech recognition, and emphasizes its potential to shape the future of technology.

在单/多 GPU 上运行模型

# pip install accelerate
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch

tokenizer = AutoTokenizer.from_pretrained("google/gemma-2-2b-jpn-it")
model = AutoModelForCausalLM.from_pretrained(
    "google/gemma-2-2b-jpn-it",
    device_map="auto",
    torch_dtype=torch.bfloat16,
)

messages = [
    {"role": "user", "content": "マシーンラーニングについての詩を書いてください。"},
]
inputs = tokenizer.apply_chat_template(messages, return_tensors="pt", add_generation_prompt=True, return_dict=True).to(model.device)

outputs = model.generate(**inputs, max_new_tokens=256)
generated_text = tokenizer.batch_decode(outputs[:, inputs['input_ids'].shape[1]:], skip_special_tokens=True)[0]
print(generated_text.strip())

使用不同精度在 GPU 上运行模型

该模型的原始权重是以 bfloat16 的精度导出的。 如果跳过 dtype,也可以使用 float32,但精度不会提高(模型权重只是上推到 float32)。 请看下面的示例。

# pip install accelerate
from transformers import AutoTokenizer, AutoModelForCausalLM

tokenizer = AutoTokenizer.from_pretrained("google/gemma-2-2b-jpn-it")
model = AutoModelForCausalLM.from_pretrained(
    "google/gemma-2-2b-jpn-it",
    device_map="auto",
)

messages = [
    {"role": "user", "content": "マシーンラーニングについての詩を書いてください。"},
]
inputs = tokenizer.apply_chat_template(messages, return_tensors="pt", add_generation_prompt=True, return_dict=True).to(model.device)

outputs = model.generate(**inputs, max_new_tokens=256)
generated_text = tokenizer.batch_decode(outputs[:, inputs['input_ids'].shape[1]:], skip_special_tokens=True)[0]
print(generated_text.strip())

输入和输出

输入:文本字符串,如问题、提示或需要摘要的文档。
输出: 根据输入内容生成的日语文本,如问题答案或文件摘要。

模型数据

用于模型训练的数据以及数据的处理方式。

训练数据集

这些模型是在一个文本数据集上训练的,该数据集包含各种来源的文本数据,总计 8 万亿个标记。 以下是关键组成部分:

  • 网络文档: 多种多样的网络文本,确保模型能接触到广泛的语言风格、主题和词汇。 主要为英语内容。
  • 代码: 让模型接触代码有助于它学习编程语言的语法和模式,从而提高其生成代码或理解代码相关问题的能力。
  • 数学 对数学文本的训练有助于模型学习逻辑推理、符号表示和解决数学问题。
  • 教学数据集:大规模、高质量的日语和多语种教学数据。

将这些不同的数据源结合起来,对于训练出一个能处理各种不同任务和文本格式的强大语言模型至关重要。

数据预处理

这里是应用于训练数据的主要数据清理和过滤方法:

  • CSAM 过滤: 在数据准备过程的多个阶段都采用了严格的 CSAM(儿童性虐待材料)过滤,以确保排除有害和非法内容。
  • 敏感数据过滤: 为了使 Gemma 预训练模型安全可靠,我们使用自动技术从训练集中过滤掉某些个人信息和其他敏感数据。
  • 其他方法: 根据Google的政策,基于内容质量和安全性进行过滤。

架构支持多样性

众所周知,Google的模型会在自家的TPU上发挥的更好,而现在Gemma开始提供 Flax 和 Pytorch 的版本。可以在HF上查阅。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值