forked from tensorforce/tensorforce
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdqn_nstep_agent.py
More file actions
156 lines (144 loc) · 5.47 KB
/
dqn_nstep_agent.py
File metadata and controls
156 lines (144 loc) · 5.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# Copyright 2017 reinforce.io. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
from __future__ import absolute_import
from __future__ import print_function
from __future__ import division
from tensorforce.agents import LearningAgent
from tensorforce.models import QNstepModel
class DQNNstepAgent(LearningAgent):
"""
DQN n-step agent.
"""
def __init__(
self,
states,
actions,
network,
batched_observe=True,
batching_capacity=1000,
scope='dqn-nstep',
device=None,
saver=None,
summarizer=None,
distributed=None,
variable_noise=None,
states_preprocessing=None,
actions_exploration=None,
reward_preprocessing=None,
update_mode=None,
memory=None,
optimizer=None,
discount=0.99,
distributions=None,
entropy_regularization=None,
target_sync_frequency=10000,
target_update_weight=1.0,
double_q_model=False,
huber_loss=None
):
"""
Initializes the DQN n-step agent.
Args:
update_mode (spec): Update mode specification, with the following attributes:
- unit: 'episodes' if given (default: 'episodes').
- batch_size: integer (default: 10).
- frequency: integer (default: batch_size).
memory (spec): Memory specification, see core.memories module for more information
(default: {type='latest', include_next_states=true, capacity=1000*batch_size}).
optimizer (spec): Optimizer specification, see core.optimizers module for more
information (default: {type='adam', learning_rate=1e-3}).
target_sync_frequency (int): Target network sync frequency (default: 10000).
target_update_weight (float): Target network update weight (default: 1.0).
double_q_model (bool): Specifies whether double DQN mode is used (default: false).
huber_loss (float): Huber loss clipping (default: none).
"""
# Update mode
if update_mode is None:
update_mode = dict(
unit='episodes',
batch_size=10
)
elif 'unit' in update_mode:
assert update_mode['unit'] == 'episodes'
else:
update_mode['unit'] = 'episodes'
# Memory
if memory is None:
# Assumed episode length of 1000 timesteps.
memory = dict(
type='latest',
include_next_states=True,
capacity=(1000 * update_mode['batch_size'])
)
else:
assert memory['include_next_states']
# Optimizer
if optimizer is None:
optimizer = dict(
type='adam',
learning_rate=1e-3
)
self.target_sync_frequency = target_sync_frequency
self.target_update_weight = target_update_weight
self.double_q_model = double_q_model
self.huber_loss = huber_loss
super(DQNNstepAgent, self).__init__(
states=states,
actions=actions,
batched_observe=batched_observe,
batching_capacity=batching_capacity,
scope=scope,
device=device,
saver=saver,
summarizer=summarizer,
distributed=distributed,
variable_noise=variable_noise,
states_preprocessing=states_preprocessing,
actions_exploration=actions_exploration,
reward_preprocessing=reward_preprocessing,
update_mode=update_mode,
memory=memory,
optimizer=optimizer,
discount=discount,
network=network,
distributions=distributions,
entropy_regularization=entropy_regularization
)
def initialize_model(self):
return QNstepModel(
states=self.states,
actions=self.actions,
scope=self.scope,
device=self.device,
saver=self.saver,
summarizer=self.summarizer,
distributed=self.distributed,
batching_capacity=self.batching_capacity,
variable_noise=self.variable_noise,
states_preprocessing=self.states_preprocessing,
actions_exploration=self.actions_exploration,
reward_preprocessing=self.reward_preprocessing,
update_mode=self.update_mode,
memory=self.memory,
optimizer=self.optimizer,
discount=self.discount,
network=self.network,
distributions=self.distributions,
entropy_regularization=self.entropy_regularization,
target_sync_frequency=self.target_sync_frequency,
target_update_weight=self.target_update_weight,
double_q_model=self.double_q_model,
huber_loss=self.huber_loss
)