-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathutils.py
54 lines (42 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
54
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import torchvision
import torchvision.transforms as transforms
import time
import os
import numpy as np
def noise(n_samples, z_dim, device):
return torch.randn(n_samples,z_dim).to(device)
class LinearLrDecay(object):
def __init__(self, optimizer, start_lr, end_lr, decay_start_step, decay_end_step):
assert start_lr > end_lr
self.optimizer = optimizer
self.delta = (start_lr - end_lr) / (decay_end_step - decay_start_step)
self.decay_start_step = decay_start_step
self.decay_end_step = decay_end_step
self.start_lr = start_lr
self.end_lr = end_lr
def step(self, current_step):
if current_step <= self.decay_start_step:
lr = self.start_lr
elif current_step >= self.decay_end_step:
lr = self.end_lr
else:
lr = self.start_lr - self.delta * (current_step - self.decay_start_step)
for param_group in self.optimizer.param_groups:
param_group['lr'] = lr
return lr
def inits_weight(m):
if type(m) == nn.Linear:
nn.init.xavier_uniform(m.weight.data, 1.)
def noise(imgs, latent_dim):
return torch.FloatTensor(np.random.normal(0, 1, (imgs.shape[0], latent_dim)))
def gener_noise(gener_batch_size, latent_dim):
return torch.FloatTensor(np.random.normal(0, 1, (gener_batch_size, latent_dim)))
def save_checkpoint(states,is_best, output_dir,
filename='checkpoint.pth'):
torch.save(states, os.path.join(output_dir, filename))
if is_best:
torch.save(states, os.path.join(output_dir, 'checkpoint_best.pth'))