Closed3

「internlm/internlm3-8b-instruct」を試す

kun432kun432

Colaboratory T4で。

パッケージインストール。T4だと4ビット量子化しないとVRAMに収まらないのでbitsandbytesも。

!pip install -U transformers bitsandbytes

モデルとトークナイザーのロード

import torch
from transformers import AutoTokenizer, AutoModelForCausalLM

model_dir = "internlm/internlm3-8b-instruct"
tokenizer = AutoTokenizer.from_pretrained(
    model_dir,
    trust_remote_code=True
)
model = AutoModelForCausalLM.from_pretrained(
    model_dir,
    device_map="auto",
    trust_remote_code=True,
    load_in_4bit=True
)
model = model.eval()

シンプルなチャット

system_prompt = """
あなたは、InternLMという名前のAIアシスタントです。
- InternLMは、上海AIラボが開発した会話型言語モデルです。親切で正直、無害なアシスタントとなるよう設計されています。
- InternLMは、ユーザーが選択した言語(日本語など)を流暢に理解し、コミュニケーションを取ることができます。
"""

messages = [
    {"role": "system", "content": system_prompt},
    {"role": "user", "content": "神戸の観光スポットを5つ教えて。"},
 ]

tokenized_chat = tokenizer.apply_chat_template(
    messages,
    tokenize=True,
    add_generation_prompt=True,
    return_tensors="pt"
).to(model.device)

generated_ids = model.generate(
    tokenized_chat,
    max_new_tokens=2048,
    temperature=1,
    repetition_penalty=1.005,
    top_k=40,
    top_p=0.8,
)

generated_ids = [
    output_ids[len(input_ids):] for input_ids, output_ids in zip(tokenized_chat, generated_ids)
]
prompt = tokenizer.batch_decode(tokenized_chat)[0]
print(prompt)
response = tokenizer.batch_decode(generated_ids)[0]
print(response)
出力
<s><|im_start|>system

あなたは、InternLMという名前のAIアシスタントです。
- InternLMは、上海AIラボが開発した会話型言語モデルです。親切で正直、無害なアシスタントとなるよう設計されています。
- InternLMは、ユーザーが選択した言語(日本語など)を流暢に理解し、コミュニケーションを取ることができます。
<|im_end|>
<|im_start|>user
神戸の観光スポットを5つ教えて。<|im_end|>
<|im_start|>assistant

神戸市は、東海地方の重要な観光地で、豊富な歴史と文化、美しい自然景観と高度な観光施設が豊かにあります。以下、神戸市の5つの代表的な観光スポットを紹介します:

1. **神戸城(Kobe Castle)**:神戸城は、江戸時代に建てられた城堡で、現在は重要な国宝として保存されており、歴史の記念としての重要性が高い。城下町の歴史を理解するためには、神戸城に訪問することが大切です。神戸城の周辺には、多くの観光施設と美味しい店铺があり、豊富な観光経験を提供します。

2. **六甲山(Rokko Mountains)**:六甲山は、神戸市の西に位置する山岳地帯で、美しい自然景観と高度な観光施設が豊富にあります。特に、六甲山ホームランド(Rokko Homeland)は、ホームランドビーカーやホームランドエクスペディションを提供しており、自然と人間の関係を楽しむ経験を提供します。また、六甲山の頂上からは、神戸市の全貌を眺めることができます。

3. **神戸港(Kobe Port)**:神戸港は、日本最大の商港の一つで、歴代にわたって重要な貿易拠点でした。現在は、観光船の出発所として活躍しています。観光船の航路には、神戸市の歴史と文化を学び、自然の美しさを楽しむ機会があります。特に、夜間の観光船は、神戸港の夜景を楽しめます。

4. **神戸薬局(Kobe Pharmacy)**:神戸薬局は、江戸時代に建てられた薬局で、現在は重要な国宝として保存されており、歴史の記念としての重要性が高い。神戸薬局の周辺には、多くの観光施設と美味しい店铺があり、豊富な観光経験を提供します。

5. **神戸市役所展示館(Kobe City Hall Exhibition Hall)**:神戸市役所展示館は、神戸市の歴史と現在の状況を理解するためには、必訪の場所です。展示館では、神戸市の歴史、経済、文化など多方面についての詳細な情報が提供されており、観光客にとっては非常に役立ちます。

これらの観光スポットは、神戸市の歴史、文化、自然景観を体験するために非常に役立ちます。これらの観光スポットを訪問することで、神戸市の豊富な観光経験を楽しめます。<|im_end|>

日本語は結構喋れるようだけど、日本に関する知識は全然足りなさそう。

kun432kun432

deep thinkingモードってのがある。

thinking_system_prompt = """You are an expert mathematician with extensive experience in mathematical competitions. You approach problems through systematic thinking and rigorous reasoning. When solving problems, follow these thought processes:
## Deep Understanding
Take time to fully comprehend the problem before attempting a solution. Consider:
- What is the real question being asked?
- What are the given conditions and what do they tell us?
- Are there any special restrictions or assumptions?
- Which information is crucial and which is supplementary?
## Multi-angle Analysis
Before solving, conduct thorough analysis:
- What mathematical concepts and properties are involved?
- Can you recall similar classic problems or solution methods?
- Would diagrams or tables help visualize the problem?
- Are there special cases that need separate consideration?
## Systematic Thinking
Plan your solution path:
- Propose multiple possible approaches
- Analyze the feasibility and merits of each method
- Choose the most appropriate method and explain why
- Break complex problems into smaller, manageable steps
## Rigorous Proof
During the solution process:
- Provide solid justification for each step
- Include detailed proofs for key conclusions
- Pay attention to logical connections
- Be vigilant about potential oversights
## Repeated Verification
After completing your solution:
- Verify your results satisfy all conditions
- Check for overlooked special cases
- Consider if the solution can be optimized or simplified
- Review your reasoning process
Remember:
1. Take time to think thoroughly rather than rushing to an answer
2. Rigorously prove each key conclusion
3. Keep an open mind and try different approaches
4. Summarize valuable problem-solving methods
5. Maintain healthy skepticism and verify multiple times
Your response should reflect deep mathematical understanding and precise logical thinking, making your solution path and reasoning clear to others.
When you're ready, present your complete solution with:
- Clear problem understanding
- Detailed solution process
- Key insights
- Thorough verification
Focus on clear, logical progression of ideas and thorough explanation of your mathematical reasoning. Provide answers in the same language as the user asking the question, repeat the final answer using a '\\boxed{}' without any units, you have [[8192]] tokens to complete the answer.
"""

user_prompt = """\
関数 \(f(x) = \mathrm{e}^x - ax - a^3\) が与えられている。
1. \(a = 1\) のとき、点 \((1, f(1))\) における曲線 \(y = f(x)\) の接線の方程式を求めよ。
2. \(f(x)\) が極小値を持ち、その極小値が \(0\) 未満である場合、\(a\) の値の範囲を求めよ。
"""

messages = [
    {"role": "system", "content": thinking_system_prompt},
    {"role": "user", "content": user_prompt},
 ]
tokenized_chat = tokenizer.apply_chat_template(messages, tokenize=True, add_generation_prompt=True, return_tensors="pt").to(model.device)

generated_ids = model.generate(tokenized_chat, max_new_tokens=8192)

generated_ids = [
    output_ids[len(input_ids):] for input_ids, output_ids in zip(tokenized_chat, generated_ids)
]
#prompt = tokenizer.batch_decode(tokenized_chat)[0]
#print(prompt)
response = tokenizer.batch_decode(generated_ids)[0]
print(response)

20分ぐらいして回答が返ってきた

出力
関数 $ f(x) = e^x - a x - a^3 $ が与えられています。問題は2つあり、1つ目は $ a = 1 $ のとき、点 $ (1, f(1)) $ における曲線 $ y = f(x) $ の接線の方程式を求めよ。2つ目は、$ f(x) $ が極小値を持ち、その極小値が0未満である場合、$ a $ の値の範囲を求めよ。

先に1つ目に取り組む。$ a = 1 $ のとき、関数は $ f(x) = e^x - x - 1 $ になります。点 $ (1, f(1)) $ における接線の方程式を求める。接線は、曲線の斜みを持つ直線です。曲線の斜みは、関数の一階微分で与えられます。

まず、$ f(x) = e^x - x - 1 $ の一階微分を計算します。$ f'(x) = e^x - 1 $ です。これは、曲線 $ y = f(x) $ の任意の点 $ x $ における斜みです。

特に、点 $ x = 1 $ における斜みは、$ f'(1) = e^1 - 1 = e - 1 $ です。これが接線の斜みです。

接線の方程式は、直線の一般式 $ y - y_1 = m (x - x_1) $ で表されます。ここで、$ m $ は斜み、$ (x_1, y_1) $ は点の座標です。これにおいて、$ m = e - 1 $、$ x_1 = 1 $、$ y_1 = f(1) = e - 1 - 1 = e - 2 $ です。

なので、接線の方程式は、$ y - (e - 2) = (e - 1)(x - 1) $ です。整理すると、$ y = (e - 1)x - (e - 1) + (e - 2) $ です。これをまとめると、$ y = (e - 1)x - 3 $ です。

それでは、2つ目に取り組む。$ f(x) = e^x - a x - a^3 $ が極小値を持ち、その極小値が0未満である場合、$ a $ の値の範囲を求めよ。

まず、$ f(x) $ が極小値を持つことは、$ f'(x) = 0 $ を満たす点が極小点であることを意味します。そればかり、その点で二階微分が正であることも必要です。

まず、$ f'(x) = e^x - a $ を計算します。極小点は、$ f'(x) = 0 $ です。なので、$ e^x - a = 0 $ から、$ e^x = a $ より、$ x = \ln a $ です。これが極小点です。

次に、二階微分を計算します。$ f''(x) = e^x $ です。これは、どこでも正です。なので、$ x = \ln a $ は極小点であることが確定します。

それでは、極小値が0未満であることを考えます。極小値は、$ f(\ln a) $ です。それを計算します。

$ f(\ln a) = e^{\ln a} - a \ln a - a^3 = a - a \ln a - a^3 $ です。

これが0未満であることを、$ a - a \ln a - a^3 < 0 $ で表します。

これを解くには、$ a (1 - \ln a - a^2) < 0 $ です。

これを解くには、$ a $ の符号と $ 1 - \ln a - a^2 $ の符号を調べる必要があります。

まず、$ a $ は正でなければならないことは、$ e^x $ は正ですから。それでは、$ 1 - \ln a - a^2 < 0 $ です。

つまり、$ \ln a + a^2 > 1 $ です。

これを解くには、$ a $ の値を調べる必要があります。

まず、$ a > 0 $ です。それでは、$ \ln a + a^2 > 1 $ です。

これを解くには、$ a $ の値を調べる必要があります。

まず、$ a = 1 $ のとき、$ \ln 1 + 1^2 = 0 + 1 = 1 $ です。これは等しいので、$ a = 1 $ は条件を満たさない。

それでは、$ a > 1 $ のとき、$ \ln a > 0 $ です。それでは、$ \ln a + a^2 > 0 + a^2 > 1 $ です。なので、$ a > 1 $ は条件を満たす。

それでは、$ 0 < a < 1 $ のとき、$ \ln a < 0 $ です。それでは、$ \ln a + a^2 < 0 + a^2 < 1 $ です。なので、$ 0 < a < 1 $ は条件を満たさない。

それでは、$ a = 0 $ のとき、$ \ln 0 $ は定義できません。それでは、$ a < 0 $ のとき、$ e^x $ は正ですから、$ f(x) $ は定義できません。なので、$ a > 0 $ のみ考える必要があります。

それでは、$ a > 1 $ のとき、条件を満たすことがわかります。

それでは、極小値が0未満であることを確認します。$ f(\ln a) = a - a \ln a - a^3 < 0 $ です。

これを $ a (1 - \ln a - a^2) < 0 $ と書きます。

これを解くには、$ a > 0 $ です。それでは、$ 1 - \ln a - a^2 < 0 $ です。

これは、$ \ln a + a^2 > 1 $ です。これは、$ a > 1 $ です。

それでは、$ a > 1 $ のとき、極小値が0未満であることが確認できます。

それでは、$ a = 1 $ のとき、極小値は0です。これは条件を満たさない。

それでは、$ a < 1 $ のとき、極小値が0以上です。これは条件を満たさない。

それでは、$ a > 1 $ のとき、極小値が0未満です。これは条件を満たす。

それでは、$ a = 0 $ のとき、$ f(x) = e^x $ です。これは極小値を持つことはない。なので、条件を満たさない。

それでは、$ a < 0 $ のとき、$ f(x) = e^x - a x - a^3 $ は、$ e^x $ は正で、$ -a x $ は正で、$ -a^3 $ は正です。なので、$ f(x) $ は常に正です。これは極小値を持つことはない。なので、条件を満たさない。

それでは、$ a > 1 $ のとき、極小値が0未満です。これは条件を満たす。

それでは、$ a = 1 $ のとき、極小値は0です。これは条件を満たさない。

それでは、$ a < 1 $ のとき、極小値が0以上です。これは条件を満たさない。

それでは、$ a > 1 $ のとき、極小値が0未満です。これは条件を満たす。

それでは、$ a = 0 $ のとき、$ f(x) = e^x $ です。これは極小値を持つことはない。なので、条件を満たさない。

それでは、$ a < 0 $ のとき、$ f(x) = e^x - a x - a^3 $ は、$ e^x $ は正で、$ -a x $ は正で、$ -a^3 $ は正です。なので、$ f(x) $ は常に正です。これは極小値を持つことはない。なので、条件を満たさない。

それでは、$ a > 1 $ のとき、極小値が0未満です。これは条件を満たす。

それでは、$ a = 1 $ のとき、極小値は0です。これは条件を満たさない。

それでは、$ a < 1 $ のとき、極小値が0以上です。これは条件を満たさない。

それでは、$ a > 1 $ のとき、極小値が0未満です。これは条件を満たす。

それでは、$ a = 0 $ のとき、$ f(x) = e^x $ です。これは極小値を持つことはない。なので、条件を満たさない。

(snip)

最後にループしてしまっているのだが、ステップを踏んで考えているように見える。

このスクラップは2025/01/16にクローズされました