-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
87 lines (74 loc) · 2.6 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# -*- coding: utf-8 -*-
from numpy.random import uniform
from numpy import zeros, array
from tensorflow import constant_initializer
from os import popen
# Create a matrix of shape (...x, y...) initialized
# with random_uniform values from min_ to max_
def random_uniform_custom(shape, min_, max_):
i = uniform(min_, max_, size=shape)
if isinstance(shape, tuple) and len(shape) >= 2:
i[0] = zeros(shape[1])
return constant_initializer(i)
# TODO: refactor
# Credits: Shimaoka et al. (2017)
# Create a sparse binary matrix where each
# type is mapped along each column
def create_prior(label2id_file):
nodes = []
num_label = 0
with open(label2id_file) as f:
for line in f:
num_label += 1
(id,label,freq) = line.strip().split()
nodes += [label]
prior = zeros((num_label,len(nodes)))
with open(label2id_file) as f:
for line in f:
(id,label,freq) = line.strip().split()
temp_ = label.split("/")[1:]
temp = ["/"+"/".join(temp_[:q+1]) for q in range(len(temp_))]
code = []
for i,node in enumerate(nodes):
if node in temp:
code.append(1)
else:
code.append(0)
prior[int(id),:] = array(code)
return prior
def get_terminal_dims():
rows, columns = popen('stty size', 'r').read().split()
return rows, columns
def print_centered(to_print):
r,c = get_terminal_dims()
pattern = str("{: ^" + c + "s}")
for x in to_print.splitlines():
print pattern.format(x)
# Cool!
def keras_logo():
keras_logo = '''
::::::::::::::::::::::::::::::::::::::::
::::/oooo+/::::::::::::::::/oooooo/:::::
:::/NMMMMMh::::::::::::::/yNMMMMMMd/::::
:::+MMMMMMd::::::::::::/yNMMMMMMMd+:::::
:::+MMMMMMd:::::::::/+yNMMMMMMNh+:::::::
:::+MMMMMMd:::::::/+hNMMMMMMNh+:::::::::
:::+MMMMMMd:::::/+hNMMMMMMNy+:::::::::::
:::+MMMMMMd:::/+hMMMMMMMNy/:::::::::::::
:::+MMMMMMd:/odMMMMMMMmy/:::::::::::::::
:::+MMMMMMdodMMMMMMMMy/:::::::::::::::::
:::+MMMMMMMMMMMMMMMMMmo:::::::::::::::::
:::+MMMMMMMMMMMMMMMMMMNy/:::::::::::::::
:::+MMMMMMMMMMdosNMMMMMMd+::::::::::::::
:::+MMMMMMMMdo/::+mMMMMMMNs/::::::::::::
:::+MMMMMMm+/:::::/yMMMMMMMd+:::::::::::
:::+MMMMMMd::::::::/oNMMMMMMNs::::::::::
:::+MMMMMMd::::::::::/dMMMMMMMh/::::::::
:::+MMMMMMd:::::::::::/sNMMMMMMmo:::::::
:::+MMMMMMd:::::::::::::+mMMMMMMMh/:::::
:::+MMMMMMd::::::::::::::/yMMMMMMMm+::::
:::+NMMMMMh:::::::::::::::/oNMMMMMMm/:::
::::/sssso/:::::::::::::::::/ssssss/::::
::::::::::::::::::::::::::::::::::::::::
'''
print_centered(keras_logo)