-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_model_v2.py
68 lines (58 loc) · 1.98 KB
/
train_model_v2.py
1
2
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
56
57
58
59
60
61
62
63
64
65
66
67
68
import os
import torch
from torch.utils.data import DataLoader
from timeit import default_timer as timer
try:
from pytorch_functions import engine, utils
except ImportError:
os.system("git clone https://github.com/Andresmup/pytorch_functions")
from pytorch_functions import engine, utils
def train_model(model,
train_dataloader,
test_dataloader,
model_save_name,
optimizer=None,
NUM_EPOCHS=5,
LEARNING_RATE=0.001):
"""
Trains a PyTorch image classification model.
Args:
model: PyTorch model to be trained.
train_dataloader: torch DataLoaders with training data.
test_dataloader: torch DataLoaders with testing data.
model_save_name: Name to save the trained model.
optimizer: PyTorch optimizer (optional, default is None).
NUM_EPOCHS: Number of training epochs (default is 5).
LEARNING_RATE: Learning rate for the optimizer (default is 0.001).
"""
# Set random seeds
torch.manual_seed(42)
torch.cuda.manual_seed(42)
# Setup target device
device = "cuda" if torch.cuda.is_available() else "cpu"
# Set loss and optimizer
loss_fn = torch.nn.CrossEntropyLoss()
optimizer = optimizer or torch.optim.Adam(model.parameters(), lr=LEARNING_RATE)
# Start the timer
start_time = timer()
# Train model
results = engine.train(
model=model,
train_dataloader=train_dataloader,
test_dataloader=test_dataloader,
optimizer=optimizer,
loss_fn=loss_fn,
epochs=NUM_EPOCHS,
device=device
)
# End the timer and print out how long it took
end_time = timer()
execution_time = end_time - start_time
print(f"[INFO] Total training time: {execution_time:.3f} seconds")
# Save the model
utils.save_model(
model=model,
target_dir="models",
model_name=model_save_name
)
return results, execution_time