-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathutils.py
49 lines (39 loc) · 1.32 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
import numpy as np
import json
def softmax(x):
"""Compute softmax values for each sets of scores in x."""
e_x = np.exp(x - np.max(x))
temp=e_x / e_x.sum(axis=0) # only difference
if np.isnan(temp).any()==True:
return [0.0,1.0,0.0]
else:
return temp
class CharVal(object):
def __init__(self, char, val):
self.char = char
self.val = val
def __str__(self):
return self.char
def rgb_to_hex(rgb):
return '#%02x%02x%02x' % rgb
def color_charvals_lime(s):
r = 255-int(s.val*255)
color = rgb_to_hex((255, r, r))
return 'background-color: %s' % color
def color_charvals_rationale(s):
r = 255-int(s.val*255)
color = rgb_to_hex((255, r, r))
return 'background-color: %s' % color
class NumpyEncoder(json.JSONEncoder):
""" Special json encoder for numpy types """
def default(self, obj):
if isinstance(obj, (np.int_, np.intc, np.intp, np.int8,
np.int16, np.int32, np.int64, np.uint8,
np.uint16, np.uint32, np.uint64)):
return int(obj)
elif isinstance(obj, (np.float_, np.float16, np.float32,
np.float64)):
return float(obj)
elif isinstance(obj,(np.ndarray,)): #### This is the fix
return obj.tolist()
return json.JSONEncoder.default(self, obj)