mirror of
https://github.com/SheffieldML/GPy.git
synced 2026-05-09 12:02:38 +02:00
added caching framework
This commit is contained in:
parent
36456af5d4
commit
1c9151a7d0
1 changed files with 41 additions and 0 deletions
41
GPy/util/caching.py
Normal file
41
GPy/util/caching.py
Normal file
|
|
@ -0,0 +1,41 @@
|
||||||
|
import numpy as np
|
||||||
|
from ..core.parameterization.array_core import ObservableArray
|
||||||
|
class Cacher(object):
|
||||||
|
def __init__(self, operation, limit=5):
|
||||||
|
self.limit = int(limit)
|
||||||
|
self.operation=operation
|
||||||
|
self.cached_inputs = []
|
||||||
|
self.cached_outputs = []
|
||||||
|
self.inputs_changed = []
|
||||||
|
|
||||||
|
def __call__(self, X):
|
||||||
|
assert isinstance(X, ObservableArray)
|
||||||
|
if X in self.cached_inputs:
|
||||||
|
i = self.cached_inputs.index(X)
|
||||||
|
if self.inputs_changed[i]:
|
||||||
|
self.cached_outputs[i] = self.operation(X)
|
||||||
|
self.inputs_changed[i] = False
|
||||||
|
return self.cached_outputs[i]
|
||||||
|
else:
|
||||||
|
if len(self.cached_inputs) == self.limit:
|
||||||
|
X_ = self.cached_inputs.pop(0)
|
||||||
|
X_.remove_observer(self)
|
||||||
|
self.inputs_changed.pop(0)
|
||||||
|
self.cached_outputs.pop(0)
|
||||||
|
|
||||||
|
self.cached_inputs.append(X)
|
||||||
|
self.cached_outputs.append(self.operation(X))
|
||||||
|
self.inputs_changed.append(False)
|
||||||
|
X.add_observer(self, self.on_cache_changed)
|
||||||
|
return self.cached_outputs[-1]
|
||||||
|
|
||||||
|
def on_cache_changed(self, X):
|
||||||
|
print id(X)
|
||||||
|
i = self.cached_inputs.index(X)
|
||||||
|
self.inputs_changed[i] = True
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue