This repository has been archived by the owner on Dec 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
random_features.py
67 lines (51 loc) · 2.13 KB
/
random_features.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
import collections
import pickle
import numpy as np
class RandomFeatures:
def __init__(self, num_features=50):
self.dynamic_mode = True
self.random_components = collections.defaultdict(
self._generate_component)
self.num_features = num_features
self.reset_features()
def _generate_component(self):
rv = np.random.randn(self.num_features, 1)
rv /= np.sqrt(np.dot(rv.T, rv)) # normalize to unit length
return rv
def load_components(self, filename):
with open(filename, 'r') as f:
self.set_components(pickle.load(f))
def set_components(self, component_dict):
"""Updates the set of components, and disables creation of any new
components. You can turn creation of new components back on by setting
dynamic_mode = True.
"""
if not all(
self.num_features == len(v) for v in component_dict.values()):
raise ValueError("Feature vectors were not all of %d length.",
self.num_features)
self.num_features = component_dict[component_dict.keys()[0]].size
self.random_components.update(component_dict)
self.dynamic_mode = False
self.reset_features()
def write_components(self, filename):
with open(filename, 'w') as f:
# conversion of defaultdict to dict is advisable for pickling
pickle.dump(dict(self.random_components), f)
def contains_component(self, component_key):
return component_key in self.random_components
def reset_features(self):
self.feature_vector = np.zeros((self.num_features, 1))
def get_features(self):
return self.feature_vector[:, 0]
def increment_component(self, component_key, scale=1.0):
if not self.dynamic_mode and (
component_key not in self.random_components):
return False
self.feature_vector += scale * self.random_components[component_key]
return True
if __name__ == '__main__':
# Example usage
rf = RandomFeatures(5)
rf.increment_feature("monkey")
print rf.feature_vector