示例#1
0
def run_nns(testset, s_vectors, ticks=None, verbose=True):
    if ticks is None:
        ticks = range(len(s_vectors))
    ticks, N = set(ticks), len(ticks)

    avgs, stds = [], []
    nnset = learners.NNSet()
    for t, s_vector in enumerate(s_vectors):

        nnset.add((), s_vector)

        if t in ticks:
            if verbose:
                gfx.print_progress(len(avgs), N)
            errors = []
            for s_vector_goal in testset:
                distances, idx = nnset.nn_y(s_vector_goal, k=1)
                s_vector = nnset.ys[idx[0]]
                errors.append(dist(s_vector_goal, s_vector))
            avgs.append(np.mean(errors))
            stds.append(np.std(errors))

    if verbose:
        gfx.print_progress(N, N)

    return avgs, stds
示例#2
0
 def nearest_neighbor(self, goal, history):
     """Return the motor command of the nearest neighbor of the goal"""
     if len(history) < len(self._nn_set):  # HACK
         self._nn_set = learners.NNSet()
     for m_command, effect in history[len(self._nn_set):]:
         self._nn_set.add(m_command, y=effect)
     idx = self._nn_set.nn_y(goal)[1][0]
     return history[idx][0]
示例#3
0
 def nearest_neighbor_fast(goal, history):
     """Same as nearest_neighbors, using the `learners` library."""
     global _nn_set
     if len(history) < len(_nn_set):  # HACK
         _nn_set = learners.NNSet()
     for m_command, effect in history[len(_nn_set):]:
         _nn_set.add(m_command, y=effect)
     idx = _nn_set.nn_y(goal)[1][0]
     return history[idx][0]
 def __init__(self, action_noise, num_retries, task):
     self.noise = action_noise
     self.retries = num_retries
     self.task = task
     if task == 'pusher':
         self.robot = PusherRobotNoisy()
     else:
         self.robot = SingleRobot(debug=False)
     self.action_len = len(self.robot.motor_ids)
     self._nn_set = learners.NNSet()
     np.random.seed(seed=225)
     random.seed(225)
示例#5
0
 def __init__(self, action_noise, num_retries, task):
     self.noise = action_noise
     self.retries = num_retries
     self.task = task
     if task == 'pusher':
         self.env = gym.make('Nas-Pusher-3dof-Vanilla-v1')
         self.action_len = self.env.action_space.shape[0]
     else:
         self.robot = SingleRobot(debug=False)
         self.action_len = len(self.robot.motor_ids)
     self._nn_set = learners.NNSet()
     np.random.seed(seed=225)
     random.seed(225)
示例#6
0
def run_nn(testset, s_vectors):

    nnset = learners.NNSet()
    for s_vector in s_vectors:
        nnset.add((), s_vector)

    errors = []
    for s_vector_goal in testset:
        distances, idx = nnset.nn_y(s_vector_goal, k=1)
        s_vector = nnset.ys[idx[0]]
        errors.append(dist(s_vector_goal, s_vector))

    return errors
示例#7
0
def nn_test(cfg):

    ## Loading exploration data ##

    data_history = chrono.ChronoHistory(cfg.hardware.exploration.datafile,
                                        extralog=False,
                                        verbose=True)
    data_cfg = data_history.core.meta['jobcfg'].exploration

    testset_chr = chrono.ChronoHistory(cfg.hardware.testset.datafile,
                                       extralog=False,
                                       verbose=True)
    testset = {
        's_channels': testset_chr.core.meta['s_channels'],
        's_goals': testset_chr.core.meta['testset']
    }

    history = chrono.ChronoHistory(cfg.hardware.datafile,
                                   core_keys=['errors'],
                                   meta={
                                       'jobcfg': cfg,
                                       'testset': testset
                                   },
                                   extralog=False)

    ticks = set(cfg.test.ticks)

    nnset = learners.NNSet()

    for tick, entry in enumerate(data_history):
        if tick in ticks and len(nnset) > 0:
            if len(history) <= tick or history.core.entries[tick] is None:
                measure_perf(tick, testset, nnset, data_cfg, history)

        exploration = entry['data']['exploration']
        feedback = entry['data']['feedback']
        m_vector = tools.to_vector(exploration['m_signal'],
                                   data_cfg.explorer.m_channels)
        s_vector = tools.to_vector(feedback['s_signal'],
                                   data_cfg.explorer.s_channels)
        nnset.add(m_vector, s_vector)

    if len(data_history) in ticks:
        measure_perf(len(data_history), testset, nnset, data_cfg, history)

    history.save(done=True, verbose=True)
示例#8
0
            min_i,
            max_i))  # create a random perturbation inside the legal values.

    return new_command


# #### Optional: Fast Nearest-Neighbors

# Now, the nearest neighbor implementation we have is fine, but it is too slow for some of the experiments we will do here. We replace it by a fast implementation from the [learners](https://github.com/benureau/learners) library. If you want to keep the slow but simple implementation, skip the next three code cells.

# In[ ]:

try:  # if learners is not present, the change is not made.
    import learners

    _nn_set = learners.NNSet()

    def nearest_neighbor_fast(goal, history):
        """Same as nearest_neighbors, using the `learners` library."""
        global _nn_set
        if len(history) < len(_nn_set):  # HACK
            _nn_set = learners.NNSet()
        for m_command, effect in history[len(_nn_set):]:
            _nn_set.add(m_command, y=effect)
        idx = _nn_set.nn_y(goal)[1][0]
        return history[idx][0]

    print('Using the fast implementation.')

except ImportError:
    nearest_neighbor_fast = nearest_neighbor