-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
46 lines (32 loc) · 1.17 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
import time
from prettytable import PrettyTable
class Timer:
def __init__(self, start_msg = "", end_msg = ""):
self.start_msg = start_msg
self.end_msg = end_msg
def __enter__(self):
if self.start_msg != "":
print(self.start_msg)
self.start_time = time.time()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
elapsed_time = time.time() - self.start_time
print(self.end_msg, f"{elapsed_time:.3f} sec")
def count_parameters(model, print_table = False):
total_params = 0
if(print_table):
table = PrettyTable(["Modules", "Parameters"])
for name, parameter in model.named_parameters():
if not parameter.requires_grad:
continue
params = parameter.numel()
if(print_table):
table.add_row([name, params])
total_params += params
if(print_table):
print(table)
if total_params/1e9 > 1:
print(f"Total Trainable Params: {total_params/1e9} B")
else:
print(f"Total Trainable Params: {total_params/1e6} M")
return total_params