forked from R0BiN00Z/Multimodal_Sentiment_Analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
53 lines (46 loc) · 1.71 KB
/
utils.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
import os
import json
import torch
import numpy as np
from typing import Dict, List, Any
def save_config(config: Any, path: str):
"""保存配置到文件"""
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, 'w') as f:
json.dump(config.__dict__, f, indent=4)
def load_config(path: str) -> Dict:
"""从文件加载配置"""
with open(path, 'r') as f:
return json.load(f)
def set_seed(seed: int):
"""设置随机种子"""
np.random.seed(seed)
torch.manual_seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
def get_device() -> torch.device:
"""获取可用的设备"""
return torch.device("cuda" if torch.cuda.is_available() else "cpu")
def count_parameters(model: torch.nn.Module) -> int:
"""计算模型参数数量"""
return sum(p.numel() for p in model.parameters() if p.requires_grad)
def save_checkpoint(model: torch.nn.Module, optimizer: torch.optim.Optimizer,
epoch: int, loss: float, path: str):
"""保存检查点"""
os.makedirs(os.path.dirname(path), exist_ok=True)
torch.save({
'epoch': epoch,
'model_state_dict': model.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
'loss': loss,
}, path)
def load_checkpoint(model: torch.nn.Module, optimizer: torch.optim.Optimizer,
path: str) -> Dict:
"""加载检查点"""
checkpoint = torch.load(path)
model.load_state_dict(checkpoint['model_state_dict'])
optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
return checkpoint