GeezerVison is showing github.com — does something reasonable · cbfinn/gps@8f9e15f
File treeExpand file treeCollapse file tree Open diff view settings experiments/reacher_images
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: experiments/reacher_images/hyperparams.py +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 105 | 105 | 'kl_step': 1.0, | 106 | 106 | 'min_step_mult': 0.1, | 107 | 107 | 'max_step_mult': 3.0, | | 108 | + 'max_ent_traj': 0.001, | 108 | 109 | 'policy_sample_mode': 'replace', | 109 | 110 | 'sample_on_policy': True, | 110 | 111 | 'plot_dir': EXP_DIR, | |
Collapse file Copy file name to clipboardExpand all lines: python/gps/agent/mjc/agent_mjc.py +16-6Lines changed: 16 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 12 | 12 | END_EFFECTOR_POINTS, END_EFFECTOR_POINT_VELOCITIES, \ | 13 | 13 | END_EFFECTOR_POINT_JACOBIANS, ACTION, RGB_IMAGE, RGB_IMAGE_SIZE, \ | 14 | 14 | CONTEXT_IMAGE, CONTEXT_IMAGE_SIZE, IMAGE_FEAT, \ | 15 | | - END_EFFECTOR_POINTS_NO_TARGET | | 15 | + END_EFFECTOR_POINTS_NO_TARGET, END_EFFECTOR_POINT_VELOCITIES_NO_TARGET | 16 | 16 |
| 17 | 17 | from gps.sample.sample import Sample | 18 | 18 |
| @@ -185,6 +185,11 @@ def _init_sample(self, condition, feature_fn=None): | 185 | 185 | eepts = data['site_xpos'].flatten() | 186 | 186 | sample.set(END_EFFECTOR_POINTS, eepts, t=0) | 187 | 187 | sample.set(END_EFFECTOR_POINT_VELOCITIES, np.zeros_like(eepts), t=0) | | 188 | + | | 189 | + if (END_EFFECTOR_POINTS_NO_TARGET in self._hyperparams['obs_include']): | | 190 | + sample.set(END_EFFECTOR_POINTS_NO_TARGET, np.delete(eepts, self._hyperparams['target_idx']), t=0) | | 191 | + sample.set(END_EFFECTOR_POINT_VELOCITIES_NO_TARGET, np.delete(np.zeros_like(eepts), self._hyperparams['target_idx']), t=0) | | 192 | + | 188 | 193 | jac = np.zeros([eepts.shape[0], self._model[condition]['nq']]) | 189 | 194 | for site in range(eepts.shape[0] // 3): | 190 | 195 | idx = site * 3 | @@ -235,13 +240,18 @@ def _set_sample(self, sample, mj_X, t, condition, feature_fn=None): | 235 | 240 | """ | 236 | 241 | sample.set(JOINT_ANGLES, np.array(mj_X[self._joint_idx]), t=t+1) | 237 | 242 | sample.set(JOINT_VELOCITIES, np.array(mj_X[self._vel_idx]), t=t+1) | 238 | | - curr_eepts = self._data['site_xpos'].flatten() | 239 | | - sample.set(END_EFFECTOR_POINTS, curr_eepts, t=t+1) | | 243 | + cur_eepts = self._data['site_xpos'].flatten() | | 244 | + sample.set(END_EFFECTOR_POINTS, cur_eepts, t=t+1) | 240 | 245 | prev_eepts = sample.get(END_EFFECTOR_POINTS, t=t) | 241 | | - eept_vels = (curr_eepts - prev_eepts) / self._hyperparams['dt'] | | 246 | + eept_vels = (cur_eepts - prev_eepts) / self._hyperparams['dt'] | 242 | 247 | sample.set(END_EFFECTOR_POINT_VELOCITIES, eept_vels, t=t+1) | 243 | | - jac = np.zeros([curr_eepts.shape[0], self._model[condition]['nq']]) | 244 | | - for site in range(curr_eepts.shape[0] // 3): | | 248 | + | | 249 | + if (END_EFFECTOR_POINTS_NO_TARGET in self._hyperparams['obs_include']): | | 250 | + sample.set(END_EFFECTOR_POINTS_NO_TARGET, np.delete(cur_eepts, self._hyperparams['target_idx']), t=t+1) | | 251 | + sample.set(END_EFFECTOR_POINT_VELOCITIES_NO_TARGET, np.delete(eept_vels, self._hyperparams['target_idx']), t=t+1) | | 252 | + | | 253 | + jac = np.zeros([cur_eepts.shape[0], self._model[condition]['nq']]) | | 254 | + for site in range(cur_eepts.shape[0] // 3): | 245 | 255 | idx = site * 3 | 246 | 256 | jac[idx:(idx+3), :] = self._world[condition].get_jac_site(site) | 247 | 257 | sample.set(END_EFFECTOR_POINT_JACOBIANS, jac, t=t+1) | |
Collapse file Copy file name to clipboardExpand all lines: python/gps/algorithm/policy/tf_policy.py +22-3Lines changed: 22 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -20,7 +20,7 @@ class TfPolicy(Policy): | 20 | 20 | sess: tf session. | 21 | 21 | device_string: tf device string for running on either gpu or cpu. | 22 | 22 | """ | 23 | | - def __init__(self, dU, obs_tensor, act_op, feat_op, var, sess, device_string): | | 23 | + def __init__(self, dU, obs_tensor, act_op, feat_op, var, sess, device_string, copy_param_scope=None): | 24 | 24 | Policy.__init__(self) | 25 | 25 | self.dU = dU | 26 | 26 | self.obs_tensor = obs_tensor | @@ -33,6 +33,15 @@ def __init__(self, dU, obs_tensor, act_op, feat_op, var, sess, device_string): | 33 | 33 | self.bias = None | 34 | 34 | self.x_idx = None | 35 | 35 |
| | 36 | + if copy_param_scope: | | 37 | + self.copy_params = tf.get_collection(tf.GraphKeys.VARIABLES, scope=copy_param_scope) | | 38 | + self.copy_params_assign_placeholders = [tf.placeholder(tf.float32, shape=param.get_shape()) for | | 39 | + param in self.copy_params] | | 40 | + | | 41 | + self.copy_params_assign_ops = [tf.assign(self.copy_params[i], | | 42 | + self.copy_params_assign_placeholders[i]) | | 43 | + for i in range(len(self.copy_params))] | | 44 | + | 36 | 45 | def act(self, x, obs, t, noise): | 37 | 46 | """ | 38 | 47 | Return an action for a state. | @@ -63,10 +72,20 @@ def get_features(self, obs): | 63 | 72 | """ | 64 | 73 | if len(obs.shape) == 1: | 65 | 74 | obs = np.expand_dims(obs, axis=0) | 66 | | - # assume that the features don't depend on the robot config, so don't normalize | | 75 | + # Assume that features don't depend on the robot config, so don't normalize by scale and bias. | 67 | 76 | with tf.device(self.device_string): | 68 | 77 | feat = self.sess.run(self.feat_op, feed_dict={self.obs_tensor: obs}) | 69 | | - return feat[0] | | 78 | + return feat[0] # the DAG computations are batched by default, but we use batch size 1. | | 79 | + | | 80 | + def get_copy_params(self): | | 81 | + param_values = self.sess.run(self.copy_params) | | 82 | + return {self.copy_params[i].name:param_values[i] for i in range(len(self.copy_params))} | | 83 | + | | 84 | + def set_copy_params(self, param_values): | | 85 | + value_list = [param_values[self.copy_params[i].name] for i in range(len(self.copy_params))] | | 86 | + feeds = {self.copy_params_assign_placeholders[i]:value_list[i] for i in range(len(self.copy_params))} | | 87 | + self.sess.run(self.copy_params_assign_ops, feed_dict=feeds) | | 88 | + | 70 | 89 |
| 71 | 90 | def pickle_policy(self, deg_obs, deg_action, checkpoint_path, goal_state=None, should_hash=False): | 72 | 91 | """ | |
Collapse file Copy file name to clipboardExpand all lines: python/gps/algorithm/policy_opt/config.py +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 42 | 42 | '..', 'policy_opt/tf_checkpoint/policy_checkpoint.ckpt')) | 43 | 43 | POLICY_OPT_TF = { | 44 | 44 | # Other hyperparameters. | | 45 | + 'copy_param_scope': 'conv_params', | 45 | 46 | 'checkpoint_prefix': checkpoint_path | 46 | 47 | } | 47 | 48 |
| |
Collapse file Copy file name to clipboardExpand all lines: python/gps/algorithm/policy_opt/policy_opt_tf.py -4Lines changed: 0 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -46,8 +46,6 @@ def __init__(self, hyperparams, dO, dU): | 46 | 46 | self.solver = None | 47 | 47 | self.feat_op = None | 48 | 48 | self.feat_vals = None | 49 | | - self.debug = None | 50 | | - self.debug_vals = None | 51 | 49 | self.init_network() | 52 | 50 | self.init_solver() | 53 | 51 | self.var = self._hyperparams['init_var'] * np.ones(dU) | @@ -81,7 +79,6 @@ def init_network(self): | 81 | 79 | self.act_op = tf_map.get_output_op() | 82 | 80 | self.feat_op = tf_map.get_feature_op() | 83 | 81 | self.loss_scalar = tf_map.get_loss_op() | 84 | | - self.debug = tf_map.debug | 85 | 82 | self.fc_vars = fc_vars | 86 | 83 | self.last_conv_vars = last_conv_vars | 87 | 84 |
| @@ -208,7 +205,6 @@ def update(self, obs, tgt_mu, tgt_prc, tgt_wt, iter_count=None, fc_only=False): | 208 | 205 | feed_dict = {self.obs_tensor: obs} | 209 | 206 | num_values = obs.shape[0] | 210 | 207 | self.feat_vals = self.solver.get_var_values(self.sess, self.feat_op, feed_dict, num_values, self.batch_size) | 211 | | - self.debug_vals = self.solver.get_var_values(self.sess, self.debug, feed_dict, num_values, self.batch_size) | 212 | 208 | # Keep track of tensorflow iterations for loading solver states. | 213 | 209 | self.tf_iter += self._hyperparams['iterations'] | 214 | 210 |
| |
Collapse file Copy file name to clipboardExpand all lines: python/gps/algorithm/policy_opt/tf_model_example.py +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -260,7 +260,7 @@ def multi_modal_network_fp(dim_input=27, dim_output=7, batch_size=25, network_co | 260 | 260 | fc_vars = weights_FC + biases_FC | 261 | 261 |
| 262 | 262 | loss = euclidean_loss_layer(a=action, b=fc_output, precision=precision, batch_size=batch_size) | 263 | | - nnet = TfMap.init_from_lists([nn_input, action, precision], [fc_output], [loss], fp=fp, debug=conv_layer_2) | | 263 | + nnet = TfMap.init_from_lists([nn_input, action, precision], [fc_output], [loss], fp=fp) | 264 | 264 | last_conv_vars = fc_input | 265 | 265 |
| 266 | 266 | return nnet, fc_vars, last_conv_vars | |
Collapse file Copy file name to clipboardExpand all lines: python/gps/algorithm/policy_opt/tf_utils.py +62-13Lines changed: 62 additions & 13 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import tensorflow as tf | | 2 | +import numpy as np | 2 | 3 |
| 3 | 4 |
| 4 | 5 | def check_list_and_convert(the_object): | @@ -11,7 +12,8 @@ class TfMap: | 11 | 12 | """ a container for inputs, outputs, and loss in a tf graph. This object exists only | 12 | 13 | to make well-defined the tf inputs, outputs, and losses used in the policy_opt_tf class.""" | 13 | 14 |
| 14 | | - def __init__(self, input_tensor, target_output_tensor, precision_tensor, output_op, loss_op, fp=None): | | 15 | + def __init__(self, input_tensor, target_output_tensor, | | 16 | + precision_tensor, output_op, loss_op, fp=None): | 15 | 17 | self.input_tensor = input_tensor | 16 | 18 | self.target_output_tensor = target_output_tensor | 17 | 19 | self.precision_tensor = precision_tensor | @@ -20,13 +22,13 @@ def __init__(self, input_tensor, target_output_tensor, precision_tensor, output_ | 20 | 22 | self.img_feat_op = fp | 21 | 23 |
| 22 | 24 | @classmethod | 23 | | - def init_from_lists(cls, inputs, outputs, loss): | | 25 | + def init_from_lists(cls, inputs, outputs, loss, fp=None): | 24 | 26 | inputs = check_list_and_convert(inputs) | 25 | 27 | outputs = check_list_and_convert(outputs) | 26 | 28 | loss = check_list_and_convert(loss) | 27 | 29 | if len(inputs) < 3: # pad for the constructor if needed. | 28 | 30 | inputs += [None]*(3 - len(inputs)) | 29 | | - return cls(inputs[0], inputs[1], inputs[2], outputs[0], loss[0]) | | 31 | + return cls(inputs[0], inputs[1], inputs[2], outputs[0], loss[0], fp=fp) | 30 | 32 |
| 31 | 33 | def get_input_tensor(self): | 32 | 34 | return self.input_tensor | @@ -65,7 +67,8 @@ def set_loss_op(self, loss_op): | 65 | 67 | class TfSolver: | 66 | 68 | """ A container for holding solver hyperparams in tensorflow. Used to execute backwards pass. """ | 67 | 69 | def __init__(self, loss_scalar, solver_name='adam', base_lr=None, lr_policy=None, | 68 | | - momentum=None, weight_decay=None): | | 70 | + momentum=None, weight_decay=None, fc_vars=None, | | 71 | + last_conv_vars=None, vars_to_opt=None): | 69 | 72 | self.base_lr = base_lr | 70 | 73 | self.lr_policy = lr_policy | 71 | 74 | self.momentum = momentum | @@ -76,35 +79,81 @@ def __init__(self, loss_scalar, solver_name='adam', base_lr=None, lr_policy=None | 76 | 79 |
| 77 | 80 | self.weight_decay = weight_decay | 78 | 81 | if weight_decay is not None: | 79 | | - trainable_vars = tf.trainable_variables() | | 82 | + if vars_to_opt is None: | | 83 | + trainable_vars = tf.trainable_variables() | | 84 | + else: | | 85 | + trainable_vars = vars_to_opt | 80 | 86 | loss_with_reg = self.loss_scalar | 81 | 87 | for var in trainable_vars: | 82 | 88 | loss_with_reg += self.weight_decay*tf.nn.l2_loss(var) | 83 | 89 | self.loss_scalar = loss_with_reg | 84 | 90 |
| 85 | 91 | self.solver_op = self.get_solver_op() | | 92 | + if fc_vars is not None: | | 93 | + self.fc_vars = fc_vars | | 94 | + self.last_conv_vars = last_conv_vars | | 95 | + self.fc_solver_op = self.get_solver_op(var_list=fc_vars) | 86 | 96 |
| 87 | | - def get_solver_op(self): | | 97 | + def get_solver_op(self, var_list=None, loss=None): | 88 | 98 | solver_string = self.solver_name.lower() | | 99 | + if var_list is None: | | 100 | + var_list = tf.trainable_variables() | | 101 | + if loss is None: | | 102 | + loss = self.loss_scalar | 89 | 103 | if solver_string == 'adam': | 90 | 104 | return tf.train.AdamOptimizer(learning_rate=self.base_lr, | 91 | | - beta1=self.momentum).minimize(self.loss_scalar) | | 105 | + beta1=self.momentum).minimize(loss, var_list=var_list) | 92 | 106 | elif solver_string == 'rmsprop': | 93 | 107 | return tf.train.RMSPropOptimizer(learning_rate=self.base_lr, | 94 | | - decay=self.momentum).minimize(self.loss_scalar) | | 108 | + decay=self.momentum).minimize(loss, var_list=var_list) | 95 | 109 | elif solver_string == 'momentum': | 96 | 110 | return tf.train.MomentumOptimizer(learning_rate=self.base_lr, | 97 | | - momentum=self.momentum).minimize(self.loss_scalar) | | 111 | + momentum=self.momentum).minimize(loss, var_list=var_list) | 98 | 112 | elif solver_string == 'adagrad': | 99 | 113 | return tf.train.AdagradOptimizer(learning_rate=self.base_lr, | 100 | | - initial_accumulator_value=self.momentum).minimize(self.loss_scalar) | | 114 | + initial_accumulator_value=self.momentum).minimize(loss, var_list=var_list) | 101 | 115 | elif solver_string == 'sgd': | 102 | | - return tf.train.GradientDescentOptimizer(learning_rate=self.base_lr).minimize(self.loss_scalar) | | 116 | + return tf.train.GradientDescentOptimizer(learning_rate=self.base_lr).minimize(loss, var_list=var_list) | 103 | 117 | else: | 104 | 118 | raise NotImplementedError("Please select a valid optimizer.") | 105 | 119 |
| 106 | | - def __call__(self, feed_dict, sess, device_string="/cpu:0"): | | 120 | + def get_last_conv_values(self, sess, feed_dict, num_values, batch_size): | | 121 | + i = 0 | | 122 | + values = [] | | 123 | + while i < num_values: | | 124 | + batch_dict = {} | | 125 | + start = i | | 126 | + end = min(i+batch_size, num_values) | | 127 | + for k,v in feed_dict.iteritems(): | | 128 | + batch_dict[k] = v[start:end] | | 129 | + batch_vals = sess.run(self.last_conv_vars, batch_dict) | | 130 | + values.append(batch_vals) | | 131 | + i = end | | 132 | + i = 0 | | 133 | + final_value = np.concatenate([values[i] for i in range(len(values))]) | | 134 | + return final_value | | 135 | + | | 136 | + def get_var_values(self, sess, var, feed_dict, num_values, batch_size): | | 137 | + i = 0 | | 138 | + values = [] | | 139 | + while i < num_values: | | 140 | + batch_dict = {} | | 141 | + start = i | | 142 | + end = min(i+batch_size, num_values) | | 143 | + for k,v in feed_dict.iteritems(): | | 144 | + batch_dict[k] = v[start:end] | | 145 | + batch_vals = sess.run(var, batch_dict) | | 146 | + values.append(batch_vals) | | 147 | + i = end | | 148 | + i = 0 | | 149 | + final_values = np.concatenate([values[i] for i in range(len(values))]) | | 150 | + return final_values | | 151 | + | | 152 | + def __call__(self, feed_dict, sess, device_string="/cpu:0", use_fc_solver=False): | 107 | 153 | with tf.device(device_string): | 108 | | - loss = sess.run([self.loss_scalar, self.solver_op], feed_dict) | | 154 | + if use_fc_solver: | | 155 | + loss = sess.run([self.loss_scalar, self.fc_solver_op], feed_dict) | | 156 | + else: | | 157 | + loss = sess.run([self.loss_scalar, self.solver_op], feed_dict) | 109 | 158 | return loss[0] | 110 | 159 |
|
|