1# Question
2
3if False:
4 from unsloth import FastLanguageModel
5 model, tokenizer = FastLanguageModel.from_pretrained(
6 model_name = "lora_model", # YOUR MODEL YOU USED FOR TRAINING
7 max_seq_length = max_seq_length,
8 dtype = dtype,
9 load_in_4bit = load_in_4bit,
10 )
11 FastLanguageModel.for_inference(model) # Enable native 2x faster inference
12
13# alpaca_prompt = You MUST copy from above!
14
15inputs = tokenizer(
16[
17 alpaca_prompt.format(
18 "Eu preciso treinar uma IA gpt2 do hugginface em meu computador", # instruction
19 "", # input
20 "", # output - leave this blank for generation!
21 )
22], return_tensors = "pt").to("cuda")
23
24from transformers import TextStreamer
25text_streamer = TextStreamer(tokenizer)
26_ = model.generate(**inputs, streamer = text_streamer, max_new_tokens = 4096)
27
28# Answer
29
30<s> Below is an instruction that describes a task related to Python programming or computer science concepts. Provide a detailed, accurate, and helpful response that addresses the query or solves the problem presented. Follow these guidelines:
31
321. If the task involves coding, provide a complete, working Python solution with explanations.
332. If it's a conceptual question, explain thoroughly, using examples where appropriate.
343. Break down complex problems or concepts into steps or components.
354. If you're unsure about any part of your response, clearly state your level of confidence and any assumptions you're making.
365. If the query is ambiguous or lacks necessary information, ask for clarification before proceeding.
376. After providing your response, briefly summarize the key points or solution.
38
39Remember, accuracy and helpfulness are crucial. Avoid making unfounded claims or providing incorrect information.
40
41### Input:
42Eu preciso treinar uma IA gpt2 do hugginface em meu computador
43
44### Output:
45
46# Importações necessárias
47from transformers import GPT2LMHeadModel, GPT2Tokenizer
48from transformers import TrainingArguments, Trainer
49from transformers.data import DataCollatorForLanguageModeling
50from transformers.data.processors import DataProcessor
51
52# Configurações
53training_args = TrainingArguments(
54 output_dir='./resultados',
55 evaluation_strategy='epoch',
56 learning_rate=2e-5,
57 per_device_train_batch_size=8,
58 eval_steps=1000,
59 num_warmupSteps=500,
60 weighted_metric='accuracy',
61)
62
63# Carregamento do tokenizador
64tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
65
66# Carregamento do modelo
67modelo = GPT2LMHeadModel.from_pretrained('gpt2')
68
69# Configurações do tracker
70tracker = DataProcessor(
71 input_class=DataProcessor,
72 batch_size=1,
73 max_length=100,
74 padding=True,
75 truncation=True,
76)
77
78# Treinando o modelo
79trainer = Trainer(
80 args=training_args,
81 train_loop=tracker,
82 trainable_pipelines=[],
83)
84
85# Treinando o modelo
86trainer.train()
87
88# Evaluação do modelo
89evaluador = DataCollatorForLanguageModeling(
90 tokenizer=tokenizer,
91 model=modelo,
92 padding=True,
93 max_length=100,
94 batch_size=8,
95)
96
97# Evalua o modelo
98resultados = trainer.evaluate()
99
100# Imprimir os resultados
101for name, loss, acc in resultados:
102 print(f'{name}: {loss}, {acc:.2f}%')</s>
103