|
| 1 | +# Copyright 2017 reinforce.io. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# ============================================================================== |
| 15 | + |
| 16 | +from __future__ import absolute_import |
| 17 | +from __future__ import print_function |
| 18 | +from __future__ import division |
| 19 | + |
| 20 | +from tensorforce.agents import MemoryAgent |
| 21 | +from tensorforce.models import QNAFModel |
| 22 | + |
| 23 | + |
| 24 | +class NAFAgent(MemoryAgent): |
| 25 | + """ |
| 26 | + NAF: https://arxiv.org/abs/1603.00748 |
| 27 | +
|
| 28 | + ### Configuration options |
| 29 | +
|
| 30 | + #### General: |
| 31 | +
|
| 32 | + * `scope`: TensorFlow variable scope name (default: 'vpg') |
| 33 | +
|
| 34 | + #### Hyperparameters: |
| 35 | +
|
| 36 | + * `batch_size`: Positive integer (**mandatory**) |
| 37 | + * `learning_rate`: positive float (default: 1e-3) |
| 38 | + * `discount`: Positive float, at most 1.0 (default: 0.99) |
| 39 | + * `normalize_rewards`: Boolean (default: false) |
| 40 | + * `entropy_regularization`: None or positive float (default: none) |
| 41 | +
|
| 42 | + #### Optimizer: |
| 43 | +
|
| 44 | + * `optimizer`: Specification dict (default: Adam with learning rate 1e-3) |
| 45 | +
|
| 46 | + #### Pre-/post-processing: |
| 47 | +
|
| 48 | + * `state_preprocessing`: None or dict with (default: none) |
| 49 | + * `exploration`: None or dict with (default: none) |
| 50 | + * `reward_preprocessing`: None or dict with (default: none) |
| 51 | +
|
| 52 | + #### Logging: |
| 53 | +
|
| 54 | + * `log_level`: Logging level, one of the following values (default: 'info') |
| 55 | + + 'info', 'debug', 'critical', 'warning', 'fatal' |
| 56 | +
|
| 57 | + #### TensorFlow Summaries: |
| 58 | + * `summary_logdir`: None or summary directory string (default: none) |
| 59 | + * `summary_labels`: List of summary labels to be reported, some possible values below (default: 'total-loss') |
| 60 | + + 'total-loss' |
| 61 | + + 'losses' |
| 62 | + + 'variables' |
| 63 | + + 'activations' |
| 64 | + + 'relu' |
| 65 | + * `summary_frequency`: Positive integer (default: 1) |
| 66 | + """ |
| 67 | + |
| 68 | + default_config = dict( |
| 69 | + # Agent |
| 70 | + preprocessing=None, |
| 71 | + exploration=None, |
| 72 | + reward_preprocessing=None, |
| 73 | + # MemoryAgent |
| 74 | + # missing, not documented! |
| 75 | + # Model |
| 76 | + optimizer=dict( |
| 77 | + type='adam', |
| 78 | + learning_rate=1e-3 |
| 79 | + ), |
| 80 | + discount=0.99, |
| 81 | + normalize_rewards=False, |
| 82 | + # DistributionModel |
| 83 | + distributions=None, # not documented!!! |
| 84 | + entropy_regularization=None, |
| 85 | + # QModel |
| 86 | + target_sync_frequency=10000, # not documented!!! |
| 87 | + target_update_weight=1.0, # not documented!!! |
| 88 | + double_dqn=False, # not documented!!! |
| 89 | + huber_loss=0.0, # not documented!!! |
| 90 | + # Logging |
| 91 | + log_level='info', |
| 92 | + model_directory=None, |
| 93 | + save_frequency=600, # TensorFlow default |
| 94 | + summary_labels=['total-loss'], |
| 95 | + summary_frequency=120, # TensorFlow default |
| 96 | + # TensorFlow distributed configuration |
| 97 | + cluster_spec=None, |
| 98 | + parameter_server=False, |
| 99 | + task_index=0, |
| 100 | + device=None, |
| 101 | + local_model=False, |
| 102 | + replica_model=False, |
| 103 | + scope='naf' |
| 104 | + ) |
| 105 | + |
| 106 | + def __init__(self, states_spec, actions_spec, network_spec, config): |
| 107 | + self.network_spec = network_spec |
| 108 | + config = config.copy() |
| 109 | + config.default(self.__class__.default_config) |
| 110 | + super(NAFAgent, self).__init__(states_spec, actions_spec, config) |
| 111 | + |
| 112 | + def initialize_model(self, states_spec, actions_spec, config): |
| 113 | + return QNAFModel( |
| 114 | + states_spec=states_spec, |
| 115 | + actions_spec=actions_spec, |
| 116 | + network_spec=self.network_spec, |
| 117 | + config=config |
| 118 | + ) |
0 commit comments