-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathutils.py
67 lines (52 loc) · 1.84 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
55
56
57
58
59
60
61
62
63
64
65
66
67
import datetime
import os
from pathlib import Path
import numpy as np
import tensorflow as tf
from sklearn.metrics import roc_auc_score
def print_curtime(note=None):
now = datetime.datetime.now()
current_time = now.strftime("%H:%M:%S")
if note is not None:
print(f"{note}: {current_time}")
else:
print(f"Current time: {current_time}")
def tf_allow_growth():
gpus = tf.config.experimental.list_physical_devices("GPU")
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
def create_logdir(root="logs/", args=None):
log_dir = root + datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
Path(log_dir).mkdir(parents=True, exist_ok=True)
with open(os.path.join(log_dir, "config.txt"), "w") as f:
print(args, file=f)
print(f"LOG_DIR: {log_dir}")
summary_writer = tf.summary.create_file_writer(
os.path.join(log_dir, "train"))
summary_writer.set_as_default()
return log_dir
def auc_score(y_true, y_pred):
if len(np.unique(y_true[:, 0])) == 1:
return 0.5
else:
return roc_auc_score(y_true, y_pred)
def auc(y_true, y_pred):
return tf.numpy_function(auc_score, (y_true, y_pred), tf.double)
def num_params(model):
total_parameters = 0
embed_parameters = 0
dense_parameters = 0
for variable in model.trainable_variables:
shape = variable.get_shape()
variable_parameters = 1
for dim in shape:
variable_parameters *= dim
total_parameters += variable_parameters
if 'embedding' in variable.name:
embed_parameters += variable_parameters
else:
dense_parameters += variable_parameters
print(f"Total Params: {total_parameters}")
print(f"Dense Params: {dense_parameters}")
print(f"Embed Params: {embed_parameters}")
return total_parameters