From 39e55ad176883bc03fe122f7f87e5465f7b382a9 Mon Sep 17 00:00:00 2001 From: jgrizou Date: Mon, 19 Dec 2016 19:05:24 +0000 Subject: [PATCH 1/2] adding update frequency as a parameter to gmm_progress --- explauto/interest_model/gmm_progress.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/explauto/interest_model/gmm_progress.py b/explauto/interest_model/gmm_progress.py index 8e3e463..a9d4fa7 100644 --- a/explauto/interest_model/gmm_progress.py +++ b/explauto/interest_model/gmm_progress.py @@ -9,7 +9,7 @@ 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): InterestModel.__init__(self, expl_dims) self.measure = measure @@ -24,6 +24,7 @@ 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 for _ in range(n_samples): self.update(rand_bounds(conf.bounds), rand_bounds(conf.bounds)) @@ -42,8 +43,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 From 773036a2982bcadee56ab0d519f8385d83a26c1a Mon Sep 17 00:00:00 2001 From: jgrizou Date: Wed, 4 Jan 2017 15:20:08 +0000 Subject: [PATCH 2/2] in gmm_progress , add option to resample motor command while the point sample is not within bounds. This fixes a bias issue in the sampling distribution, where more probability was allocated to the boundary than desired. --- explauto/interest_model/gmm_progress.py | 27 +++++++++++++++++++------ explauto/utils/utils.py | 10 ++++++++- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/explauto/interest_model/gmm_progress.py b/explauto/interest_model/gmm_progress.py index a9d4fa7..de90400 100644 --- a/explauto/interest_model/gmm_progress.py +++ b/explauto/interest_model/gmm_progress.py @@ -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, update_frequency=10): + 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 @@ -25,16 +27,29 @@ def __init__(self, conf, expl_dims, measure, n_samples=40, n_components=6, updat 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) diff --git a/explauto/utils/utils.py b/explauto/utils/utils.py index d0f8c36..b78b1f8 100644 --- a/explauto/utils/utils.py +++ b/explauto/utils/utils.py @@ -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)) @@ -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)