Last active
March 7, 2019 08:29
-
-
Save MTDzi/297d2bdb16de7873551c0d11b8e8c2d1 to your computer and use it in GitHub Desktop.
Multi-task output layers built on top of an embedder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def add_throttle_upon_steer_w_odometry( | |
| outputs_spec, embed_getter, | |
| act='elu', l2_reg=1e-3, num_dense_neurons=512, | |
| ): | |
| """Build output layers on top of the embedding layer, and return a model. | |
| An example of the `outputs_spec` argument is an OrderedDict specifying | |
| the names of the output layers, their respective: activation function, | |
| loss, and weight (multiplicative constant modifying the loss), e.g.: | |
| outputs_spec = OrderedDict( | |
| [('steer', {'act': 'linear', 'loss': 'mse', 'weight': 1.0})] | |
| + [('steer__{}__last'.format(i), {'act': 'linear', 'loss': 'mse', 'weight': 1.0}) for i in STEPS_INTO_NEAR_FUTURE] | |
| + [('throttle', {'act': 'sigmoid', 'loss': 'mse', 'weight': 1.0})] | |
| + [('throttle__{}__last'.format(i), {'act': 'sigmoid', 'loss': 'mse', 'weight': 1.0}) for i in STEPS_INTO_NEAR_FUTURE] | |
| ) | |
| where `STEPS_INTO_NEAR_FUTURE` is for example `range(1, 11)` if we'd like to | |
| additionally predict 10 steps into the future. | |
| """ | |
| inp, emb = embed_getter() | |
| inp_speed = Input((1, ), name='speed') | |
| # First, each steering angle gets its own hidden layer + a prediction neuron | |
| steer_outputs = [] | |
| for layer_name in outputs_spec.keys(): | |
| if 'steer' in layer_name: | |
| x = Dense( | |
| num_dense_neurons//4, | |
| kernel_regularizer=l2(l2_reg), | |
| activation=act, | |
| )(emb) | |
| steer_outputs.append( | |
| Dense( | |
| 1, | |
| kernel_regularizer=l2(l2_reg), | |
| activation=outputs_spec[layer_name]['act'], | |
| name=layer_name, | |
| )(x) | |
| ) | |
| # Now, we concatenate the embedding layer with the speed (provided as input) | |
| # and the outputs for the steering angles | |
| emb = concatenate([emb, inp_speed] + steer_outputs) | |
| throttle_outputs = [] | |
| for layer_name in outputs_spec.keys(): | |
| if 'throttle' in layer_name: | |
| x = Dense( | |
| num_dense_neurons//4, | |
| kernel_regularizer=l2(l2_reg), | |
| activation=act, | |
| )(emb) | |
| throttle_outputs.append( | |
| Dense( | |
| 1, | |
| kernel_regularizer=l2(l2_reg), | |
| activation=outputs_spec[layer_name]['act'], | |
| name=layer_name, | |
| )(x) | |
| ) | |
| return Model([inp, inp_speed], steer_outputs+throttle_outputs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment