-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproj_o.py
More file actions
64 lines (52 loc) · 1.93 KB
/
Copy pathproj_o.py
File metadata and controls
64 lines (52 loc) · 1.93 KB
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
import torch
import torch.nn as nn
from torch.utils.data import DataLoader, TensorDataset
from tqdm import tqdm
import os
SEED = 0
torch.manual_seed(SEED)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
class ProjO(nn.Module):
def __init__(self, dim):
super().__init__()
self.linear = nn.Linear(dim, dim, bias=False)
def forward(self, x):
return self.linear(x)
def train_proj_o(x_train, y_train, x_test, y_test, dim, device, output_path, ckpt, epochs=5):
if os.path.exists(os.path.join(output_path, ckpt)):
print(f"[SKIP] Proj_O model already exists")
return
dataset = TensorDataset(x_train, y_train)
generator = torch.Generator().manual_seed(SEED)
loader = DataLoader(dataset, batch_size=256, shuffle=True, generator=generator)
# Define model
proj_o_model = ProjO(dim=dim).to(device)
optimizer = torch.optim.AdamW(proj_o_model.parameters(), lr=1e-4, weight_decay=1e-2)
loss_fn = nn.MSELoss()
# Train
proj_o_model.train()
for _ in tqdm(range(epochs)):
for xb, yb in loader:
xb, yb = xb.to(device), yb.to(device)
pred = proj_o_model(xb)
loss = loss_fn(pred, yb)
optimizer.zero_grad()
loss.backward()
optimizer.step()
# Test
proj_o_model.eval()
with torch.no_grad():
x_test, y_test = x_test.to(device), y_test.to(device)
pred = proj_o_model(x_test)
test_loss = loss_fn(pred, y_test).item()
print(f"Test loss for Proj_o: {test_loss:.4f}")
os.makedirs(output_path, exist_ok=True)
torch.save(proj_o_model.state_dict(), f'{output_path}/{ckpt}')
return proj_o_model
def load_proj_o_model(dim, output_path, ckpt, device):
model = ProjO(dim=dim).to(device)
with torch.no_grad():
model.load_state_dict(torch.load(f'{output_path}/{ckpt}', map_location=device))
model.eval()
return model