forked from Sohl-Dickstein/Diffusion-Probabilistic-Models
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
28 lines (25 loc) · 1.01 KB
/
util.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
import numpy as np
import os
import theano
import theano.tensor as T
import time
logit = lambda u: T.log(u / (1.-u))
logit_np = lambda u: np.log(u / (1.-u)).astype(theano.config.floatX)
def get_norms(model, gradients):
"""Compute norm of weights and their gradients divided by the number of elements"""
norms = []
grad_norms = []
for param_name, param in model.params.iteritems():
norm = T.sqrt(T.sum(T.square(param))) / T.prod(param.shape.astype(theano.config.floatX))
norm.name = 'norm_' + param_name
norms.append(norm)
grad = gradients[param]
grad_norm = T.sqrt(T.sum(T.square(grad))) / T.prod(grad.shape.astype(theano.config.floatX))
grad_norm.name = 'grad_norm_' + param_name
grad_norms.append(grad_norm)
return norms, grad_norms
def create_log_dir(args, model_id):
model_id += args.suffix + time.strftime('-%y%m%dT%H%M%S')
model_dir = os.path.join(os.path.expanduser(args.output_dir), model_id)
os.makedirs(model_dir)
return model_dir