forked from hclhkbu/gtopkssgd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
40 lines (33 loc) · 1.08 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
import hashlib
import time
import os
import numpy as np
def gen_random_id():
id_ = hashlib.sha256()
id_.update(str(time.time()))
return id_.hexdigest()
def create_path(relative_path):
dirname = os.path.dirname(__file__)
filename = os.path.join(dirname, relative_path)
if not os.path.isdir(filename):
try:
#os.mkdir(filename)
os.makedirs(filename)
except:
pass
def update_fontsize(ax, fontsize=12.):
for item in ([ax.title, ax.xaxis.label, ax.yaxis.label] +
ax.get_xticklabels() + ax.get_yticklabels()):
item.set_fontsize(fontsize)
def autolabel(rects, ax, label, rotation=90):
"""
Attach a text label above each bar displaying its height
"""
for rect in rects:
height = rect.get_y() + rect.get_height()
ax.text(rect.get_x() + rect.get_width()/2., 1.03*height,
label,
ha='center', va='bottom', rotation=rotation)
def topk(tensor, k):
indexes = np.abs(tensor).argsort()[-k:]
return indexes, tensor[indexes]