-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathEC_functions.py
143 lines (114 loc) · 4.64 KB
/
EC_functions.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
__author__ = 'sudeep raja'
import numpy as np
import cPickle
import heapq
from sklearn.neighbors import BallTree, KDTree
class LRU_KNN:
def __init__(self, capacity, dimension_result):
self.capacity = capacity
self.states = np.zeros((capacity, dimension_result))
self.q_values = np.zeros(capacity)
self.lru = np.zeros(capacity)
self.weights = np.ones(capacity)
self.curr_capacity = 0
self.tm = 0.0
self.tree = None
def nn(self, key, knn):
dist, ind = self.tree.query([key], k=knn)
for index in ind[0]:
self.lru[index] = self.tm
self.tm+=0.01
embs = self.states[ind[0]]
values = self.q_values[ind[0]]
weights = self.weights[ind[0]]
return embs, values, weights
def add(self, keys, values):
skip_indices = []
if self.curr_capacity >= 1:
dist, ind = self.tree.query(keys, k=1)
for i, d in enumerate(dist):
if d[0] < 0.001:
new_value = values[i]
index = ind[i][0]
self.q_values[index] = self.q_values[index]*0.9 + new_value*0.1
skip_indices.append(i)
for i, _ in enumerate(keys):
if i in skip_indices: continue
if self.curr_capacity >= self.capacity:
# find the LRU entry
old_index = np.argmin(self.lru)
self.states[old_index] = keys[i]
self.q_values[old_index] = values[i]
self.lru[old_index] = self.tm
else:
self.states[self.curr_capacity] = keys[i]
self.q_values[self.curr_capacity] = values[i]
self.lru[self.curr_capacity] = self.tm
self.curr_capacity+=1
self.tm += 0.01
self.tree = KDTree(self.states[:self.curr_capacity])
class alltheNN:
def __init__(self, capacity, dimension_result, alpha=0.05):
self.capacity = capacity
self.states = np.zeros((capacity, dimension_result))
self.q_values = np.zeros(capacity)
self.weights = np.zeros(capacity)
self.curr_capacity = 0
self.last_added = 0
def nn(self, key, knn=0):
ind = range(self.curr_capacity)
embs = self.states[ind]
values = self.q_values[ind]
weights = self.weights[ind]
return embs, values, weights
def add(self, keys, values):
for i, _ in enumerate(keys):
if self.curr_capacity >= self.capacity:
# find the LRU entry
index = self.last_added
else:
index = self.curr_capacity
self.curr_capacity+=1
self.last_added = (self.last_added + 1) % self.capacity
self.states[index] = keys[i]
self.q_values[index] = values[i]
self.weights[index] = 1.0
class Weighted_KNN:
def __init__(self, capacity, dimension_result, alpha=0.05):
self.capacity = capacity
self.states = np.zeros((capacity, dimension_result))
self.q_values = np.zeros(capacity)
self.weights = np.zeros(capacity)
self.curr_capacity = 0
self.tm = 0.0
self.alpha = alpha
self.tree = None
def nn(self, key, knn):
dist, ind = self.tree.query(np.pad([key], ((0,0),(0,1)), 'constant', constant_values=1.0), k=knn)
#dist, ind = self.tree.query([key], k=knn)
embs = self.states[ind[0]]
values = self.q_values[ind[0]]
weights = self.weights[ind[0]]
return embs, values, weights
def add(self, keys, values):
if self.curr_capacity >= 5:
dist, ind = self.tree.query(np.pad(keys, ((0,0),(0,1)), 'constant', constant_values=1.0), k=5)
#dist, ind = self.tree.query(keys, k=50)
for i, ind_ in enumerate(ind):
stren = 1 - self.alpha
self.weights[ind_] = self.weights[ind_] * stren
for i, _ in enumerate(keys):
low_w = 1.0
if self.curr_capacity >= self.capacity:
# find the LRU entry
old_index = np.argmin(self.weights)
low_w = min(low_w, self.weights[old_index])
index = old_index
else:
index = self.curr_capacity
self.curr_capacity+=1
self.states[index] = keys[i]
self.q_values[index] = values[i]
self.weights[index] = 1.0
self.tree = KDTree(np.concatenate((self.states[:self.curr_capacity], np.expand_dims(self.weights[:self.curr_capacity], axis=1)),axis=1))
#self.tree = KDTree(self.states[:self.curr_capacity])