Added project

This commit is contained in:
2025-01-13 11:18:03 +03:00
parent ec8e1c49de
commit 45150662a3
9 changed files with 175 additions and 0 deletions
+24
View File
@@ -0,0 +1,24 @@
import torch
def save_model(model, path):
"""Сохраняет модель в указанный файл."""
torch.save(model.state_dict(), path)
def load_model(model, path):
"""Загружает веса модели из файла."""
model.load_state_dict(torch.load(path))
return model
def calculate_accuracy(model, data_loader, device):
"""Подсчитывает точность модели на заданном наборе данных."""
model.eval()
correct = 0
total = 0
with torch.no_grad():
for inputs, labels in data_loader:
inputs, labels = inputs.to(device), labels.to(device)
outputs = model(inputs)
_, predicted = torch.max(outputs, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
return 100 * correct / total