-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelpers.py
48 lines (40 loc) · 1.13 KB
/
helpers.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
import os
import shutil
import tensorflow as tf
def rm_dir(path):
if os.path.isdir(path):
shutil.rmtree(path)
def lazy(fn):
# Decorator that makes a property lazy-evaluated.
attr_name = '_lazy_' + fn.__name__
@property
def _lazy_property(self):
if not hasattr(self, attr_name):
setattr(self, attr_name, fn(self))
return getattr(self, attr_name)
return _lazy_property
def get_loss_op(fn):
loss_name = fn.__name__
@property
def _scoped_loss(self):
with tf.variable_scope(loss_name):
loss = fn(self)
if "losses" in self.hparams.log:
loss = tf.Print(
loss,
[loss, self.global_step],
"-- (%s) %s " % (self.mode, loss_name)
)
return loss
return _scoped_loss
def run_from_ipython():
try:
__IPYTHON__
return True
except NameError:
return False
def pr(obj, msg=""):
return tf.Print(obj, [obj], "-- %s --" % msg, summarize=10)
class Bunch:
def __init__(self, **kwds):
self.__dict__.update(kwds)