-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
67 lines (60 loc) · 1.96 KB
/
main.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
import torch
from vqvae import VQVAE
from utils import Trajectory
from pathlib import Path
import random
import numpy as np
def main():
# args
emb_dim = 64
vocab_size = 1000
lr = 0.005
batch_size = 64
epoch = 50
device = ("cuda"
if torch.cuda.is_available()
else "cpu")
__dirname = Path(__file__).parent
vqvae = VQVAE(emb_dim,
num_embs=vocab_size,
).to(device=device)
trajectories = Trajectory\
.from_pickle(__dirname / "traj.pkl")
NUM_TOTAL_DATA = len(trajectories.obs)
optimzer = torch.optim.Adam(vqvae.parameters(),
lr=lr)
for j in range(epoch):
observations = random.sample(trajectories.obs,
NUM_TOTAL_DATA)
observations = np.stack(observations)
observations = torch.tensor(observations,
dtype=torch.float32)
observations = observations.permute(0, 3, 1, 2) # (N, C, H, W)
vq_losses = 0
recon_losses = 0
commit_losses = 0
for i in range(0,
len(observations),
batch_size):
obs = observations[i: i + batch_size]
obs = obs.to(device=device)
res = vqvae(obs)
loss = (res.vq_loss
+ 1.5*res.recon_loss
+ 0.4*res.commit_loss)
vq_losses += res.vq_loss.item()
recon_losses += res.recon_loss.item()
commit_losses += res.commit_loss.item()
optimzer.zero_grad()
loss.backward()
optimzer.step()
print(f"{recon_losses:.5f}")
print(f"{commit_losses:.5f}")
print(f"{vq_losses:.5f}")
print("=-"*25)
if j % 5 == 4:
vqvae.viz_recon(obs[0])
torch.save(vqvae,
__dirname/f"epoch_{j}.chpt")
if __name__ == "__main__":
main()