GeezerVison is showing github.com — fix policy_sample_mode, speedup linesearch, other cleanup/refactor · cbfinn/gps@5277c6a
File treeExpand file treeCollapse file tree Open diff view settings experiments/mjc_mdgps_example
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: docs/hyperparams.md +4-2Lines changed: 4 additions & 2 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -23,7 +23,6 @@ This page contains all of the config settings that are exposed vi | 23 | 23 | * fixed_lg_step | 24 | 24 | * exp_step_decrease | 25 | 25 | * init_pol_wt | 26 | | -* max_policy_samples | 27 | 26 | * policy_dual_rate | 28 | 27 | * inner_iterations | 29 | 28 | * exp_step_lower | @@ -34,6 +33,10 @@ This page contains all of the config settings that are exposed vi | 34 | 33 | * policy_dual_rate_covar | 35 | 34 | * ent_reg_schedule | 36 | 35 |
| | 36 | +**MDGPS Algorithm** | | 37 | +* policy_sample_mode | | 38 | +* step_rule | | 39 | + | 37 | 40 | **LQR Traj Opt** | 38 | 41 | * del0 | 39 | 42 | * eta_error_threshold | @@ -58,7 +61,6 @@ This page contains all of the config settings that are exposed vi | 58 | 61 |
| 59 | 62 | **Policy Prior & GMM** | 60 | 63 | * strength | 61 | | -* keep_samples | 62 | 64 | * max_clusters | 63 | 65 | * strength | 64 | 66 | * min_samples_per_cluster | |
Collapse file Copy file name to clipboardExpand all lines: experiments/mjc_mdgps_example/hyperparams.py +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 53 | 53 | 'substeps': 5, | 54 | 54 | 'conditions': common['conditions'], | 55 | 55 | 'pos_body_idx': np.array([1]), | 56 | | - 'pos_body_offset': [np.array([-0.08, -0.08, 0]), np.array([-0.08, -0.08, 0]), | | 56 | + 'pos_body_offset': [np.array([-0.08, -0.08, 0]), np.array([-0.08, 0.08, 0]), | 57 | 57 | np.array([0.08, 0.08, 0]), np.array([0.08, -0.08, 0])], | 58 | 58 | 'T': 100, | 59 | 59 | 'sensor_dims': SENSOR_DIMS, | | 69 | 69 | 'conditions': common['conditions'], | 70 | 70 | 'iterations': 12, | 71 | 71 | 'kl_step': 1.0, | 72 | | - 'min_step_mult': 0.05, | | 72 | + 'min_step_mult': 0.5, | 73 | 73 | 'max_step_mult': 3.0, | 74 | 74 | 'policy_sample_mode': 'replace', | 75 | 75 | } | |
Collapse file Copy file name to clipboardExpand all lines: experiments/mjc_mdgps_example/off_classic/hyperparams.py -36Lines changed: 0 additions & 36 deletions Collapse file Copy file name to clipboardExpand all lines: experiments/mjc_mdgps_example/off_policy/hyperparams.py -1Lines changed: 0 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 24 | 24 | algorithm = default.algorithm.copy() | 25 | 25 | algorithm.update({ | 26 | 26 | 'sample_on_policy': False, | 27 | | - 'step_rule': 'global', | 28 | 27 | }) | 29 | 28 |
| 30 | 29 | config = default.config.copy() | |
Collapse file Copy file name to clipboardExpand all lines: experiments/mjc_mdgps_example/on_classic/hyperparams.py -36Lines changed: 0 additions & 36 deletions Collapse file Copy file name to clipboardExpand all lines: experiments/mjc_mdgps_example/on_policy/hyperparams.py -1Lines changed: 0 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 24 | 24 | algorithm = default.algorithm.copy() | 25 | 25 | algorithm.update({ | 26 | 26 | 'sample_on_policy': True, | 27 | | - 'step_rule': 'global', | 28 | 27 | }) | 29 | 28 |
| 30 | 29 | config = default.config.copy() | |
Collapse file Copy file name to clipboardExpand all lines: python/gps/agent/mjc/agent_mjc.py +33-19Lines changed: 33 additions & 19 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -50,9 +50,9 @@ def _setup_world(self, filename): | 50 | 50 | # Initialize Mujoco worlds. If there's only one xml file, create a single world object, | 51 | 51 | # otherwise create a different world for each condition. | 52 | 52 | if not isinstance(filename, list): | 53 | | - self._world = [mjcpy.MJCWorld(filename) | 54 | | - for _ in range(self._hyperparams['conditions'])] | 55 | | - self._model = [self._world[i].get_model() | | 53 | + world = mjcpy.MJCWorld(filename) | | 54 | + self._world = [world for _ in range(self._hyperparams['conditions'])] | | 55 | + self._model = [self._world[i].get_model().copy() | 56 | 56 | for i in range(self._hyperparams['conditions'])] | 57 | 57 | else: | 58 | 58 | for i in range(self._hyperparams['conditions']): | @@ -62,26 +62,22 @@ def _setup_world(self, filename): | 62 | 62 | for i in range(self._hyperparams['conditions']): | 63 | 63 | for j in range(len(self._hyperparams['pos_body_idx'][i])): | 64 | 64 | idx = self._hyperparams['pos_body_idx'][i][j] | | 65 | + # TODO: this should actually add [i][j], but that would break things | 65 | 66 | self._model[i]['body_pos'][idx, :] += \ | 66 | 67 | self._hyperparams['pos_body_offset'][i] | 67 | | - self._world[i].set_model(self._model[i]) | 68 | | - x0 = self._hyperparams['x0'][i] | 69 | | - idx = len(x0) // 2 | 70 | | - data = {'qpos': x0[:idx], 'qvel': x0[idx:]} | 71 | | - self._world[i].set_data(data) | 72 | | - self._world[i].kinematics() | 73 | 68 |
| 74 | 69 | self._joint_idx = list(range(self._model[0]['nq'])) | 75 | 70 | self._vel_idx = [i + self._model[0]['nq'] for i in self._joint_idx] | 76 | 71 |
| 77 | 72 | # Initialize x0. | 78 | 73 | self.x0 = [] | 79 | | - self.eepts0 = [] | 80 | 74 | for i in range(self._hyperparams['conditions']): | 81 | 75 | if END_EFFECTOR_POINTS in self.x_data_types: | 82 | | - self.eepts0.append(self._world[i].get_data()['site_xpos'].flatten()) | | 76 | + # TODO: this assumes END_EFFECTOR_VELOCITIES is also in datapoints right? | | 77 | + self._init(i) | | 78 | + eepts = self._world[i].get_data()['site_xpos'].flatten() | 83 | 79 | self.x0.append( | 84 | | - np.concatenate([self._hyperparams['x0'][i], self.eepts0[i], np.zeros_like(self.eepts0[i])]) | | 80 | + np.concatenate([self._hyperparams['x0'][i], eepts, np.zeros_like(eepts)]) | 85 | 81 | ) | 86 | 82 | else: | 87 | 83 | self.x0.append(self._hyperparams['x0'][i]) | @@ -123,7 +119,7 @@ def sample(self, policy, condition, verbose=True, save=True, noisy=True): | 123 | 119 | var = self._hyperparams['noisy_body_var'][condition][i] | 124 | 120 | self._model[condition]['body_pos'][idx, :] += \ | 125 | 121 | var * np.random.randn(1, 3) | 126 | | - self._world[condition].set_model(self._model[condition]) | | 122 | + # Take the sample. | 127 | 123 | for t in range(self.T): | 128 | 124 | X_t = new_sample.get_X(t=t) | 129 | 125 | obs_t = new_sample.get_obs(t=t) | @@ -142,19 +138,37 @@ def sample(self, policy, condition, verbose=True, save=True, noisy=True): | 142 | 138 | self._samples[condition].append(new_sample) | 143 | 139 | return new_sample | 144 | 140 |
| | 141 | + def _init(self, condition): | | 142 | + """ | | 143 | + Set the world to a given model, and run kinematics. | | 144 | + Args: | | 145 | + condition: Which condition to initialize. | | 146 | + """ | | 147 | + | | 148 | + # Initialize world/run kinematics | | 149 | + self._world[condition].set_model(self._model[condition]) | | 150 | + x0 = self._hyperparams['x0'][condition] | | 151 | + idx = len(x0) // 2 | | 152 | + data = {'qpos': x0[:idx], 'qvel': x0[idx:]} | | 153 | + self._world[condition].set_data(data) | | 154 | + self._world[condition].kinematics() | | 155 | + | 145 | 156 | def _init_sample(self, condition): | 146 | 157 | """ | 147 | 158 | Construct a new sample and fill in the first time step. | 148 | 159 | Args: | 149 | 160 | condition: Which condition to initialize. | 150 | 161 | """ | 151 | 162 | sample = Sample(self) | 152 | | - sample.set(JOINT_ANGLES, | 153 | | - self._hyperparams['x0'][condition][self._joint_idx], t=0) | 154 | | - sample.set(JOINT_VELOCITIES, | 155 | | - self._hyperparams['x0'][condition][self._vel_idx], t=0) | 156 | | - self._data = self._world[condition].get_data() | 157 | | - eepts = self.eepts0[condition] | | 163 | + | | 164 | + # Initialize world/run kinematics | | 165 | + self._init(condition) | | 166 | + | | 167 | + # Initialize sample with stuff from _data | | 168 | + data = self._world[condition].get_data() | | 169 | + sample.set(JOINT_ANGLES, data['qpos'].flatten(), t=0) | | 170 | + sample.set(JOINT_VELOCITIES, data['qvel'].flatten(), t=0) | | 171 | + eepts = data['site_xpos'].flatten() | 158 | 172 | sample.set(END_EFFECTOR_POINTS, eepts, t=0) | 159 | 173 | sample.set(END_EFFECTOR_POINT_VELOCITIES, np.zeros_like(eepts), t=0) | 160 | 174 | jac = np.zeros([eepts.shape[0], self._model[condition]['nq']]) | |
Collapse file Copy file name to clipboardExpand all lines: python/gps/algorithm/algorithm.py +23-17Lines changed: 23 additions & 17 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -29,6 +29,8 @@ def __init__(self, hyperparams): | 29 | 29 | else: | 30 | 30 | self.M = hyperparams['conditions'] | 31 | 31 | self._cond_idx = range(self.M) | | 32 | + self._hyperparams['train_conditions'] = self._cond_idx | | 33 | + self._hyperparams['test_conditions'] = self._cond_idx | 32 | 34 | self.iteration_count = 0 | 33 | 35 |
| 34 | 36 | # Grab a few values from the agent. | @@ -48,9 +50,9 @@ def __init__(self, hyperparams): | 48 | 50 | self.cur = [IterationData() for _ in range(self.M)] | 49 | 51 | self.prev = [IterationData() for _ in range(self.M)] | 50 | 52 |
| | 53 | + dynamics = self._hyperparams['dynamics'] | 51 | 54 | for m in range(self.M): | 52 | 55 | self.cur[m].traj_info = TrajectoryInfo() | 53 | | - dynamics = self._hyperparams['dynamics'] | 54 | 56 | self.cur[m].traj_info.dynamics = dynamics['type'](dynamics) | 55 | 57 | init_traj_distr = extract_condition( | 56 | 58 | self._hyperparams['init_traj_distr'], self._cond_idx[m] | @@ -76,28 +78,29 @@ def _update_dynamics(self): | 76 | 78 | Instantiate dynamics objects and update prior. Fit dynamics to | 77 | 79 | current samples. | 78 | 80 | """ | 79 | | - for cond in range(self.M): | 80 | | - if self.iteration_count >= 1: | 81 | | - self.prev[cond].traj_info.dynamics = \ | 82 | | - self.cur[cond].traj_info.dynamics.copy() | 83 | | - cur_data = self.cur[cond].sample_list | 84 | | - self.cur[cond].traj_info.dynamics.update_prior(cur_data) | 85 | | - | 86 | | - self.cur[cond].traj_info.dynamics.fit(cur_data) | 87 | | - | 88 | | - init_X = cur_data.get_X()[:, 0, :] | 89 | | - x0mu = np.mean(init_X, axis=0) | 90 | | - self.cur[cond].traj_info.x0mu = x0mu | 91 | | - self.cur[cond].traj_info.x0sigma = np.diag( | 92 | | - np.maximum(np.var(init_X, axis=0), | | 81 | + for m in range(self.M): | | 82 | + cur_data = self.cur[m].sample_list | | 83 | + X = cur_data.get_X() | | 84 | + U = cur_data.get_U() | | 85 | + | | 86 | + # Update prior and fit dynamics. | | 87 | + self.cur[m].traj_info.dynamics.update_prior(cur_data) | | 88 | + self.cur[m].traj_info.dynamics.fit(X, U) | | 89 | + | | 90 | + # Fit x0mu/x0sigma. | | 91 | + x0 = X[:, 0, :] | | 92 | + x0mu = np.mean(x0, axis=0) | | 93 | + self.cur[m].traj_info.x0mu = x0mu | | 94 | + self.cur[m].traj_info.x0sigma = np.diag( | | 95 | + np.maximum(np.var(x0, axis=0), | 93 | 96 | self._hyperparams['initial_state_var']) | 94 | 97 | ) | 95 | 98 |
| 96 | | - prior = self.cur[cond].traj_info.dynamics.get_prior() | | 99 | + prior = self.cur[m].traj_info.dynamics.get_prior() | 97 | 100 | if prior: | 98 | 101 | mu0, Phi, priorm, n0 = prior.initial_state() | 99 | 102 | N = len(cur_data) | 100 | | - self.cur[cond].traj_info.x0sigma += \ | | 103 | + self.cur[m].traj_info.x0sigma += \ | 101 | 104 | Phi + (N*priorm) / (N+priorm) * \ | 102 | 105 | np.outer(x0mu-mu0, x0mu-mu0) / (N+n0) | 103 | 106 |
| @@ -167,6 +170,9 @@ def _advance_iteration_variables(self): | 167 | 170 | """ | 168 | 171 | self.iteration_count += 1 | 169 | 172 | self.prev = copy.deepcopy(self.cur) | | 173 | + # TODO: change IterationData to reflect new stuff better | | 174 | + for m in range(self.M): | | 175 | + self.prev[m].new_traj_distr = self.new_traj_distr[m] | 170 | 176 | self.cur = [IterationData() for _ in range(self.M)] | 171 | 177 | for m in range(self.M): | 172 | 178 | self.cur[m].traj_info = TrajectoryInfo() | |
|