Views
No views yet
pip install -U transformerspipeline API1import torch
2from transformers import pipeline
3
4pipe = pipeline(
5 "text-generation",
6 model="google/gemma-2-2b-jpn-it",
7 model_kwargs={"torch_dtype": torch.bfloat16},
8 device="cuda", # replace with "mps" to run on a Mac device
9)
10
11messages = [
12 {"role": "user", "content": "マシーンラーニングについての詩を書いてください。"},
13]
14
15outputs = pipe(messages, return_full_text=False, max_new_tokens=256)
16assistant_response = outputs[0]["generated_text"].strip()
17print(assistant_response)## マシーンラーニングの詩
**1.**
データの海、深淵の広がり、
複雑なパターン、隠された知識。
機械学習、その力強さ、
未来を予測、その道を開く。
**2.**
ニューラルネットワーク、複雑な枝、
学習の旅、その過程は静か。
データから学び、進化する姿、
予測の精度、その力強さ。
**3.**
教師あり学習、正解を導く、
教師なし学習、未知の世界へ。
機械学習、その進化は止まらない、
未来の扉を開く、新たな時代へ。
**4.**
画像認識、音声認識、
複雑なタスク、その答えを見つける。
機械学習、その力強さ、
未来の技術、その可能性を語る。1translation_input_text = f"Translate the following poem from Japanese to English:\n\n{assistant_response}"
2messages = [
3 {"role": "user", "content": translation_input_text},
4]
5
6outputs = pipe(messages, return_full_text=False, max_new_tokens=1024)
7translated_response = outputs[0]["generated_text"].strip()
8print(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.1# pip install accelerate
2from transformers import AutoTokenizer, AutoModelForCausalLM
3import torch
4
5tokenizer = AutoTokenizer.from_pretrained("google/gemma-2-2b-jpn-it")
6model = AutoModelForCausalLM.from_pretrained(
7 "google/gemma-2-2b-jpn-it",
8 device_map="auto",
9 torch_dtype=torch.bfloat16,
10)
11
12messages = [
13 {"role": "user", "content": "マシーンラーニングについての詩を書いてください。"},
14]
15inputs = tokenizer.apply_chat_template(messages, return_tensors="pt", add_generation_prompt=True, return_dict=True).to(model.device)
16
17outputs = model.generate(**inputs, max_new_tokens=256)
18generated_text = tokenizer.batch_decode(outputs[:, inputs['input_ids'].shape[1]:], skip_special_tokens=True)[0]
19print(generated_text.strip())bfloat16 precision.float32 if you skip the dtype, but no precision increase will occur (model weights will just be upcasted to float32). See examples below.torch.float321# pip install accelerate
2from transformers import AutoTokenizer, AutoModelForCausalLM
3
4tokenizer = AutoTokenizer.from_pretrained("google/gemma-2-2b-jpn-it")
5model = AutoModelForCausalLM.from_pretrained(
6 "google/gemma-2-2b-jpn-it",
7 device_map="auto",
8)
9
10messages = [
11 {"role": "user", "content": "マシーンラーニングについての詩を書いてください。"},
12]
13inputs = tokenizer.apply_chat_template(messages, return_tensors="pt", add_generation_prompt=True, return_dict=True).to(model.device)
14
15outputs = model.generate(**inputs, max_new_tokens=256)
16generated_text = tokenizer.batch_decode(outputs[:, inputs['input_ids'].shape[1]:], skip_special_tokens=True)[0]
17print(generated_text.strip())