Canstralian
commited on
Update train_and_save_model.py
Browse files- train_and_save_model.py +54 -99
train_and_save_model.py
CHANGED
@@ -1,99 +1,54 @@
|
|
1 |
-
import
|
2 |
-
import
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
#
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
#
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
#
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
# Step 6: Training Loop
|
56 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
57 |
-
model.to(device) # Move model to the correct device
|
58 |
-
|
59 |
-
for epoch in range(num_epochs):
|
60 |
-
for inputs, targets in data_loader:
|
61 |
-
# Move data to the same device as the model
|
62 |
-
inputs, targets = inputs.to(device), targets.to(device)
|
63 |
-
|
64 |
-
# Forward pass
|
65 |
-
outputs = model(inputs)
|
66 |
-
loss = criterion(outputs, targets)
|
67 |
-
|
68 |
-
# Backward pass and optimization
|
69 |
-
optimizer.zero_grad()
|
70 |
-
loss.backward()
|
71 |
-
optimizer.step()
|
72 |
-
|
73 |
-
# Print loss for every batch
|
74 |
-
print(f'Epoch [{epoch+1}/{num_epochs}], Batch Loss: {loss.item():.4f}')
|
75 |
-
|
76 |
-
# Print epoch summary
|
77 |
-
print(f'Epoch [{epoch+1}/{num_epochs}], Loss: {loss.item():.4f}')
|
78 |
-
|
79 |
-
# Step 7: Save the Model
|
80 |
-
model_save_path = "path/to/save/model_directory" # Change this to your desired path
|
81 |
-
os.makedirs(model_save_path, exist_ok=True) # Create the directory if it doesn't exist
|
82 |
-
|
83 |
-
# Save the model weights
|
84 |
-
torch.save(model.state_dict(), os.path.join(model_save_path, "pytorch_model.bin"))
|
85 |
-
|
86 |
-
# Step 8: Create and Save the Configuration File
|
87 |
-
config = {
|
88 |
-
"input_size": input_size,
|
89 |
-
"hidden_size": hidden_size,
|
90 |
-
"output_size": output_size,
|
91 |
-
"num_layers": 1, # Add more parameters as needed
|
92 |
-
"dropout": 0.2
|
93 |
-
}
|
94 |
-
|
95 |
-
# Save the configuration to a JSON file
|
96 |
-
with open(os.path.join(model_save_path, "config.json"), "w") as f:
|
97 |
-
json.dump(config, f)
|
98 |
-
|
99 |
-
print("Model and configuration saved successfully!")
|
|
|
1 |
+
from datasets import load_dataset
|
2 |
+
from transformers import AutoAdapterModel, AutoTokenizer, Trainer, TrainingArguments
|
3 |
+
|
4 |
+
# Load datasets
|
5 |
+
dataset_pentesting = load_dataset("canstralian/pentesting-ai")
|
6 |
+
dataset_redpajama = load_dataset("togethercomputer/RedPajama-Data-1T")
|
7 |
+
|
8 |
+
# Tokenizer
|
9 |
+
tokenizer = AutoTokenizer.from_pretrained("canstralian/rabbitredeux")
|
10 |
+
|
11 |
+
def tokenize_function(examples):
|
12 |
+
return tokenizer(examples['text'], padding="max_length", truncation=True)
|
13 |
+
|
14 |
+
# Tokenize datasets
|
15 |
+
tokenized_dataset_pentesting = dataset_pentesting.map(tokenize_function, batched=True)
|
16 |
+
tokenized_dataset_redpajama = dataset_redpajama.map(tokenize_function, batched=True)
|
17 |
+
|
18 |
+
# Prepare datasets
|
19 |
+
train_dataset_pentesting = tokenized_dataset_pentesting["train"]
|
20 |
+
validation_dataset_pentesting = tokenized_dataset_pentesting["validation"]
|
21 |
+
|
22 |
+
# Load model and adapter
|
23 |
+
model = AutoAdapterModel.from_pretrained("canstralian/rabbitredeux")
|
24 |
+
model.load_adapter("Canstralian/RabbitRedux", set_active=True)
|
25 |
+
|
26 |
+
# Training arguments
|
27 |
+
training_args = TrainingArguments(
|
28 |
+
output_dir="./results",
|
29 |
+
num_train_epochs=3,
|
30 |
+
per_device_train_batch_size=8,
|
31 |
+
per_device_eval_batch_size=8,
|
32 |
+
warmup_steps=500,
|
33 |
+
weight_decay=0.01,
|
34 |
+
logging_dir="./logs",
|
35 |
+
logging_steps=10,
|
36 |
+
evaluation_strategy="epoch",
|
37 |
+
)
|
38 |
+
|
39 |
+
# Trainer setup
|
40 |
+
trainer = Trainer(
|
41 |
+
model=model,
|
42 |
+
args=training_args,
|
43 |
+
train_dataset=train_dataset_pentesting,
|
44 |
+
eval_dataset=validation_dataset_pentesting,
|
45 |
+
)
|
46 |
+
|
47 |
+
# Training
|
48 |
+
trainer.train()
|
49 |
+
|
50 |
+
# Evaluate model
|
51 |
+
trainer.evaluate()
|
52 |
+
|
53 |
+
# Save the fine-tuned model
|
54 |
+
model.save_pretrained("./fine_tuned_model")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|