LLM 공부 1일차 :: 무작정 LLM 돌려보기

문주호·2025년 3월 13일

LLM 스터디

목록 보기
1/4

어떤 공부를 해야할지 몰라 일단 llm 모델을 받아 돌려보자는 생각을 하였다.
메모리와 저장공간이 부족해 맥북을 초기화하고 hugging face에서 Qwen 모델을 받아 pycharm 에서 돌려보았다.

음... 뭔지 모르겠다 일단 돌려보자

https://huggingface.co/Qwen/Qwen2.5-7B-Instruct

# transformers 라이브러리에서 필요한 클래스들을 임포트합니다.
from transformers import AutoModelForCausalLM, AutoTokenizer

# 사용할 모델의 이름을 지정합니다. 이는 Hugging Face 모델 허브의 모델 ID입니다.
model_name = "Qwen/Qwen2.5-Math-7B-Instruct"

# AutoModelForCausalLM을 사용하여 지정된 모델을 다운로드하고 로드합니다.
# torch_dtype="auto"는 자동으로 적절한 데이터 타입을 선택합니다.
# device_map="auto"는 사용 가능한 하드웨어에 모델을 자동으로 할당합니다.
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype="auto", device_map="auto")

# 모델에 대응하는 토크나이저를 다운로드하고 초기화합니다.
tokenizer = AutoTokenizer.from_pretrained(model_name)


prompt = "Find the value of $x$ that satisfies the equation $4x+5 = 6x+7$."
messages = [
    {"role": "system", "content": "Please reason step by step, and put your final answer within \\boxed{}."},
    {"role": "user", "content": prompt}
]

text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
generated_ids = model.generate(**model_inputs, max_new_tokens=512)
response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)

#출력
print(response[0])

임포트한 클래스들이 궁금해서 찾아보니 AutoModelForCausalLM,AutoTokenizer 는
hugging face 에서 지원하는 모델을 받아오고 학습 모델에 맞는 적절한 토크나이저를 자동으로 불러와주는 기능을

1시간 이상 기다려서 답변을 받았다. 맥북에서는 효율적인 성능이 나올 수 없겠다는 것을 깨달았다...

이번엔 Qwen 기반으로 학습된 deepseek-r1 모델을 돌려보았다.

코드는 모델 이름빼고 동일하게 했고 무게도 약 1/5배로 가벼워졌다.
또 프롬프트를 입력할때 "최대한 길게 말해줘"를 함께 넣어서 명령할 수 있게 만들었더니 프롬프트를 입력했을때 보다 길게 이야기 해줘서 좋았던것 같다.

답변 생성 시간도 3~5분정도로 1시간이었던 무거운 모델에 비해 빨라진것을 알 수 있다

해당 모델에서는 한국어 입력시 이해를 잘 하지 못해 아쉬웠다.
아래는 위의 프롬프트 메세지를 입력해 생성된 답변이다.

Tell me the answer as long as possible.<|User|>explain rock paper scissors<|Assistant|><think>
Okay, so I'm trying to understand the game of rock-paper-scissors. I've heard about it before, but I'm not exactly sure how it works. Let me think through it step by step.

First, the game has three choices: rock, paper, and scissors. Each choice can potentially beat one of the others. Rock beats scissors, scissors beat paper, and paper beats rock. That part I get. But how does it work when there are more than two choices? I guess if there are more than three options, the game gets more complicated. Maybe it's called something else, but I think it's still rock-paper-scissors. I'm not sure.

Wait, the user mentioned that there are three choices, so I should stick with that. Let me break it down. Each choice has an advantage, but it's based on the other choices. For example, rock is good against scissors and paper. So if I choose rock, I win against scissors and lose against paper. That makes sense. But how does the game end? I think it ends when someone chooses a move that beats another's. So if I choose rock and someone else chooses scissors, I win. But if both of us choose the same thing, it's a draw. So, it's a zero-sum game because one person's win is another's loss.

I'm also trying to remember how the game is played. I think you and a friend take turns choosing one of the three options, and then you look at who beat whom. Sometimes, people use rock-paper-scissors as a way to resolve conflicts, like in sports or social situations. It's a simple game that can be surprisingly strategic. I wonder if there's a way to always win, but I think it's not possible because the choices can cycle. If I choose rock and the opponent chooses paper, I lose. But if I choose paper and they choose rock, I win. So it's all about the strategy and knowing which move beats what.

I'm also thinking about how this game applies to real life. Maybe in some board games or online games where you have to make choices without knowing the other player's intent. It's a classic example of game theory, where players make decisions based on the possible outcomes. It's also used in conflict resolution, where understanding each other's choices can lead to a favorable outcome.

I'm a bit confused about what happens if there are more than three choices. I think it gets more complex, maybe involving more elements or a different
**한국어로 번역된 deepseek 생성 답변**

가위바위보 게임에 대해 이해하려고 노력 중입니다. 전에 들어본 적은 있지만 어떻게 작동하는지 정확히 모르겠습니다. 차근차근 생각해 보겠습니다.

먼저, 게임에는 세 가지 선택지가 있습니다: 가위, 종이, 가위. 각 선택지는 다른 선택지 중 하나를 이길 수 있습니다. 가위는 가위를 이기고, 가위는 종이를 이기고, 종이는 바위를 이겼습니다. 그 부분을 이해합니다. 하지만 두 가지 이상의 선택지가 있을 때 어떻게 작동하나요? 세 가지 이상의 선택지가 있으면 게임이 더 복잡해집니다. 다른 이름일 수도 있지만 여전히 가위바위보라고 생각합니다. 잘 모르겠습니다.

잠깐만요, 사용자가 세 가지 선택지가 있다고 언급했으니 저는 그 선택지를 고수해야 합니다. 요약해 보겠습니다. 각 선택지에는 장점이 있지만 다른 선택지에 따라 달라집니다. 예를 들어 가위와 종이에 바위가 좋습니다. 그래서 제가 가위를 선택하면 가위에 이기고 종이에 지죠. 그게 말이 되죠. 하지만 게임은 어떻게 끝나나요? 누군가 다른 사람의 것을 이기는 동작을 선택하면 끝나는 것 같아요. 그래서 제가 바위를 선택하고 다른 사람이 가위를 선택하면 제가 이기는 거죠. 하지만 우리 둘 다 같은 것을 선택하면 무승부입니다. 따라서 한 사람의 승리는 다른 사람의 패배이기 때문에 제로섬 게임입니다.

저는 또한 게임이 어떻게 진행되는지 기억하려고 노력하고 있습니다. 여러분과 친구가 번갈아 가며 세 가지 옵션 중 하나를 선택한 다음 누가 이겼는지 살펴봅니다. 때때로 사람들은 스포츠나 사회적 상황에서 갈등을 해결하는 방법으로 가위바위보를 사용하기도 합니다. 의외로 전략적일 수 있는 간단한 게임입니다. 항상 이길 수 있는 방법이 있을까 싶지만 선택의 사이클이 반복될 수 있기 때문에 불가능하다고 생각합니다. 제가 바위를 선택했는데 상대방이 종이를 선택하면 지는 것입니다. 하지만 제가 종이를 선택했는데 그들이 바위를 선택하면 제가 이기는 것입니다. 따라서 전략과 어떤 움직임이 무엇을 이기는지 아는 것이 전부입니다.

또한 이 게임이 실제 생활에 어떻게 적용되는지에 대해서도 생각하고 있습니다. 아마도 다른 플레이어의 의도를 모른 채 선택을 해야 하는 보드 게임이나 온라인 게임의 경우일 것입니다. 플레이어가 가능한 결과에 따라 결정을 내리는 게임 이론의 고전적인 예입니다. 또한 서로의 선택을 이해하면 유리한 결과로 이어질 수 있는 갈등 해결에도 사용됩니다.

선택지가 세 개 이상일 경우 어떻게 되는지 조금 혼란스럽습니다. 더 복잡해져서 더 많은 요소가 포함되거나 다른 요소가 포함될 수 있다고 생각합니다

확실히 기존의 qwen " 7B 모델은 기본적으로 수식 위주의 설명을 하였고, deepseek는 1차 방정식을 설명해달라는 프롬프트를 입력할시 설명위주의 프롬프트를 자동으로 생성했다.

다음 포스트에서는 transformers 에 대해 알아보자

0개의 댓글