-
Notifications
You must be signed in to change notification settings - Fork 0
/
HistoryHashTable.py
155 lines (130 loc) · 6.76 KB
/
HistoryHashTable.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
143
144
145
146
147
148
149
150
151
152
153
154
155
from typing import Callable
from tensorflow import CriticalSection
from tensorflow.python.ops.lookup_ops import MutableHashTable
import tensorflow as tf
class HistoryHashTable:
def __init__(self):
# hashmap default value & empty/deleted key placeholders
self.hashmap_def_val = tf.convert_to_tensor(tf.Variable(0, dtype=tf.int32)) # mutable and dense
self.hashmap_emp_val = tf.constant('<-e->', dtype=tf.string) # dense only
self.hashmap_del_val = tf.constant('<-d->', dtype=tf.string) # dense only
self.hashtable: MutableHashTable = MutableHashTable(key_dtype=tf.string,
value_dtype=tf.int32,
default_value=self.hashmap_def_val,
checkpoint=False,
experimental_is_anonymous=True)
self._rho_normalizer = tf.Variable(0) # if we compute rho distribution on the fly it saves us a lot
# of time later on.
# self.hashtable = tf.lookup.experimental.DenseHashTable(key_dtype=tf.string,
# value_dtype=tf.int32,
# default_value=self.hashmap_def_val,
# empty_key=self.hashmap_emp_val,
# deleted_key=self.hashmap_del_val)
def __call__(self, trajectory):
if trajectory is None:
return tf.constant(0)
self.insert(trajectory)
self._rho_normalizer.assign_add(1)
def lookup_wrapper(self, query):
obs = tf.strings.reduce_join(tf.strings.as_string(query), separator=" ")
return self.hashtable.lookup(obs)
def get_rho(self):
return self._rho_normalizer
def rho_distribution_of(self, x) -> tf.Tensor:
# tf.print("state = ", x)
value = self.lookup_wrapper(x)
normalizer = self._rho_normalizer
# tf.print(f"value = {value}\t normalizer = {normalizer}")
return tf.convert_to_tensor(value/normalizer)
def insert(self, trajectory, is_full_trajectory=True):
# take first element of the observation as it is stored in a 2d tensor.
if is_full_trajectory:
obs = tf.strings.reduce_join(tf.strings.as_string(trajectory.observation), separator=" ")
else:
obs = tf.strings.reduce_join(tf.strings.as_string(trajectory), separator=" ")
# hashtable lookup result is an EagerTensor, so we take 0'th element to get back to tf.Variable form.
if tf.executing_eagerly():
if self.hashtable.lookup(obs).numpy() == self.hashmap_def_val:
val = tf.Variable(1, dtype=tf.int32)
# val = tf.reshape(tf.convert_to_tensor(val), (1,))
# in the continuous environment, this is complaining about being passed a reshaped tensor
# but in the discrete environment, this is complaining about being passed a regular variable!
self.hashtable.insert(obs, val)
else:
new_val = self.hashtable.lookup(obs) + 1
self.hashtable.remove(obs)
self.hashtable.insert(obs, new_val)
else:
if self.hashtable.lookup(obs) == self.hashmap_def_val:
val = tf.Variable(1, dtype=tf.int32)
# val = tf.reshape(tf.convert_to_tensor(val), (1,))
# in the continuous environment, this is complaining about being passed a reshaped tensor
# but in the discrete environment, this is complaining about being passed a regular variable!
self.hashtable.insert(obs, val)
else:
new_val = self.hashtable.lookup(obs).assign_add(1)
self.hashtable.remove(obs)
self.hashtable.insert(obs, new_val)
@staticmethod
def static_insert(table, default_value, trajectory):
# this is a terrible way of doing this, but it's also probably the simplest...
# at least we don't have to do it in eager mode...
obs = tf.strings.reduce_join(tf.strings.as_string(trajectory.observation), separator=" ")
if table.lookup(obs) == default_value:
val = tf.Variable(1, dtype=tf.int32)
# val = tf.reshape(tf.convert_to_tensor(val), (1,))
# in the continuous environment, this is complaining about being passed a reshaped tensor
# but in the discrete environment, this is complaining about being passed a regular variable!
table.insert(obs, val)
else:
new_val = table.lookup(obs) + 1
table.insert(obs, new_val)
return tf.constant(0)
def get_hashtable(self):
return self.hashtable
def export_state_hashmap(self, verbose=False):
"""
Export the state hash-map into a list of states and counts.
:return:
"""
if verbose:
print(f"Exporting hashtable with {self.hashtable.size()} values")
keys, values = self.hashtable.export()
keys = list(map(lambda x: x.numpy().decode('utf-8').split(' '), keys))
values = values.numpy()
kv_pairs = zip(keys, values)
if not verbose:
return kv_pairs
for tup in kv_pairs:
obs = tup[0]
try:
obs = list(map(float, obs))
except ValueError:
obs = list(map(float, obs[:-1]))
if verbose:
print(f"{tup[1]} counts of {obs}")
return kv_pairs
class TranslatedHashTable(HistoryHashTable):
def __init__(self, translation_function: Callable):
super().__init__()
self._tf = translation_function
def __call__(self, trajectory):
if trajectory is None:
return
self.insert(trajectory)
self._rho_normalizer.assign_add(1)
def insert(self, trajectory, is_full_trajectory=True):
# take first element of the observation as it is stored in a 2d tensor.
key = self._tf(trajectory.observation)
super().insert(key, is_full_trajectory=False)
del key
def rho_distribution_of(self, x) -> tf.Tensor:
# tf.print("state = ", x)
value = self.lookup_wrapper(x)
normalizer = self._rho_normalizer
# tf.print(f"value = {value}\t normalizer = {normalizer}")
return tf.convert_to_tensor(value/normalizer)
def lookup_wrapper(self, query):
transformed_query = self._tf(query)
obs = tf.strings.reduce_join(tf.strings.as_string(transformed_query), separator=" ")
return self.hashtable.lookup(obs)