Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions explauto/interest_model/gmm_progress.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
from sklearn.preprocessing import StandardScaler

from ..utils import rand_bounds
from ..utils import bounds_min_max
from ..utils import is_within_bounds
from ..models.gmminf import GMM
from .competences import competence_exp
from .interest_model import InterestModel


class GmmInterest(InterestModel):
def __init__(self, conf, expl_dims, measure, n_samples=40, n_components=6):
def __init__(self, conf, expl_dims, measure, n_samples=40, n_components=6, update_frequency=10, resample_if_out_of_bounds=False):
InterestModel.__init__(self, expl_dims)

self.measure = measure
Expand All @@ -24,16 +26,30 @@ def __init__(self, conf, expl_dims, measure, n_samples=40, n_components=6):
self.data = numpy.zeros((n_samples, len(expl_dims) + 2))
self.n_samples = n_samples
self.scaler = StandardScaler()
self.update_frequency = update_frequency
self.resample_if_out_of_bounds = resample_if_out_of_bounds

for _ in range(n_samples):
self.update(rand_bounds(conf.bounds), rand_bounds(conf.bounds))

def sample(self):
x = self.gmm_choice.sample()
x = self.scaler.inverse_transform(numpy.hstack(([0.], x.flatten(), [0.])))[1:-1]
x = numpy.maximum(x, self.bounds[0, :])
x = numpy.minimum(x, self.bounds[1, :])
return x.T
out_of_bounds = True
while out_of_bounds:
# sample from gmm
x = self.gmm_choice.sample()
# inverse the transform applied on data
x = self.scaler.inverse_transform(numpy.hstack(([0.], x.flatten(), [0.])))[1:-1]
# if resample_if_out_of_bounds is True, we check if x is in bounds and resample while not within bounds
if self.resample_if_out_of_bounds:
# if within bound let go out of the while loop
if is_within_bounds(x, self.bounds[0, :], self.bounds[1, :]):
out_of_bounds = False
#else we keep out_of_bounds to True
else:
# just bound the result and let go out of while loop
x = bounds_min_max(x, self.bounds[0, :], self.bounds[1, :])
out_of_bounds = False
return x

def update(self, xy, ms):
measure = self.measure(xy, ms)
Expand All @@ -42,8 +58,9 @@ def update(self, xy, ms):
self.data[self.t % self.n_samples, 1:-1] = xy.flatten()[self.expl_dims]

self.t += self.scale_t
if abs(self.t % (self.n_samples * self.scale_t / 4.)) < self.scale_t:
self.update_gmm()
if self.t >= 0:
if self.t % self.update_frequency == 0:
self.update_gmm()

return self.t, xy.flatten()[self.expl_dims], measure

Expand Down
10 changes: 9 additions & 1 deletion explauto/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,20 @@ def bounds_min_max(v, mins, maxs):
res = np.maximum(res, mins)
return res


def is_within_bounds(v, mins, maxs):
res = bounds_min_max(v, mins, maxs)
return np.array_equal(v, res)


def prop_choice(v, eps=0.):
if np.sum(v) == 0 or np.random.rand() < eps:
return np.random.randint(np.size(v))
else:
probas = np.array(v) / np.sum(v)
return np.where(np.random.multinomial(1, probas) == 1)[0][0]



def softmax_choice(v, temperature=1.):
if np.sum(v) == 0:
return np.random.randint(np.size(v))
Expand All @@ -43,6 +50,7 @@ def softmax_choice(v, temperature=1.):
probas = probas / np.sum(probas)
return np.where(np.random.multinomial(1, probas) == 1)[0][0]


def discrete_random_draw(data, nb=1):
''' Code from Steve Nguyen'''
data = np.array(data)
Expand Down