-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
50 lines (46 loc) · 1.29 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
import math
from collections import Counter
# check and update dictionary with counters
# dict: dictionary to search in
# id: key in the dictionary to search for
# c_id: id of the element in the counter to look for
def update_dict(dict, id, c_id):
# update dict
if id not in dict.keys():
dict[id] = Counter()
# update counter
if c_id in dict[id].keys():
dict[id][c_id] += 1
else:
dict[id][c_id] = 1
# print(dict)
return dict
# update probability values instead of count
def update_dict_prob(dict, ids, c_id, prob):
for id in ids:
# update dict
if id not in dict.keys():
dict[id] = {}
# update counter
if c_id in dict[id].keys():
dict[id][c_id] += prob
else:
dict[id][c_id] = prob
# print(dict)
return dict
def initLinear(linear, val = None):
if val is None:
fan = linear.in_features + linear.out_features
spread = math.sqrt(2.0) * math.sqrt( 2.0 / fan )
else:
spread = val
linear.weight.data.uniform_(-spread,spread)
linear.bias.data.uniform_(-spread,spread)
def unique(pair):
result = []
pair_set = set(pair)
# convert the set to the list
unique_pair = (list(pair_set))
for x in unique_pair:
result.append(x)
return result