-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hjj_reinforcement.lua
262 lines (229 loc) · 8.04 KB
/
Hjj_reinforcement.lua
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
require 'cudnn'
--***************************************************************
--************************** FEATURE SIZE ***********************
history_action_buffer_size = 20
number_of_actions = 13
history_vector_size = number_of_actions * history_action_buffer_size
feature_size = 128 * 7 * 7
input_vector_size = feature_size + history_vector_size
--#################################################################
function func_create_pvn()
local feature_dim = feature_size
local hid_dim_1 = 4096
local hid_dim_2 = 1024
local output_dim = 2
local softMaxLayer = cudnn.SoftMax()
local net = nn.Sequential()
net:add(nn.Reshape(feature_dim))
net:add(nn.Linear(feature_dim, hid_dim_1))
net:add(nn.ReLU())
net:add(nn.Dropout(0.5))
net:add(nn.Linear(hid_dim_1, hid_dim_2))
net:add(nn.ReLU())
net:add(nn.Dropout(0.5))
net:add(nn.Linear(hid_dim_2, hid_dim_2))
net:add(nn.ReLU())
net:add(nn.Dropout(0.5))
net:add(nn.Linear(hid_dim_2, output_dim))
net:add(softMaxLayer)
return net
end
function func_create_dqn()
local feature_dim = input_vector_size
local hid_dim_1 = 4096
local hid_dim_2 = 1024
local output_dim = number_of_actions
local net = nn.Sequential()
net:add(nn.Reshape(feature_dim))
net:add(nn.Linear(feature_dim, hid_dim_1))
net:add(nn.ReLU())
net:add(nn.Dropout(0.2))
net:add(nn.Linear(hid_dim_1, hid_dim_2))
net:add(nn.ReLU())
net:add(nn.Dropout(0.2))
net:add(nn.Linear(hid_dim_2, hid_dim_2))
net:add(nn.ReLU())
net:add(nn.Dropout(0.2))
net:add(nn.Linear(hid_dim_2, hid_dim_2))
net:add(nn.ReLU())
net:add(nn.Dropout(0.2))
net:add(nn.Linear(hid_dim_2, output_dim))
return net
end
function func_create_rgn( )
local feature_dim = feature_size
local hid_dim_1 = 4096
local hid_dim_2 = 1024
local output_dim = 4
local net = nn.Sequential()
net:add(nn.Reshape(feature_dim))
net:add(nn.Linear(feature_dim, hid_dim_1))
net:add(nn.ReLU())
net:add(nn.Dropout(0.5))
net:add(nn.Linear(hid_dim_1, hid_dim_2))
net:add(nn.ReLU())
net:add(nn.Dropout(0.5))
net:add(nn.Linear(hid_dim_2, hid_dim_2))
net:add(nn.ReLU())
net:add(nn.Dropout(0.5))
net:add(nn.Linear(hid_dim_2, output_dim))
return net
end
function func_sample(data, batch_size)
local minibatch = {}
for i = 1, batch_size
do
table.insert(minibatch,data[torch.random(
torch.Generator(),1, table.getn(data))])
end
return minibatch
end
--[[
function func_construct_dqn_training_data( minibatch, training_set, dqn,gamma )
for l, memory in pairs(minibatch) do
local tmp_input_vector = memory[1]
local tmp_action1 = memory[2]
local tmp_action2 = memory[3]
local tmp_reward1 = memory[4]
local tmp_reward2 = memory[5]
local tmp_new_input_vector1 = memory[6]
local tmp_new_input_vector2 = memory[7]
local old_action_output = dqn:forward(tmp_input_vector)
local new_action_output1 = dqn:forward(tmp_new_input_vector1)
local new_action_output2 = dqn:forward(tmp_new_input_vector2)
local y = old_action_output:clone()
local tmp_v, tmp_index = torch.max(new_action_output1,1)
tmp_v = tmp_v[1]
tmp_index = tmp_index[1]
local update_reward = tmp_reward1 + gamma * tmp_v
y[tmp_action1] = update_reward
tmp_v, tmp_index = torch.max(new_action_output2,1)
tmp_v = tmp_v[1]
tmp_index = tmp_index[1]
local update_reward = tmp_reward2 + gamma * tmp_v
y[tmp_action2] = update_reward
training_set.data[l] = tmp_input_vector
training_set.label[l] = y
end
return training_set
end
--]]
function func_construct_dqn_training_data( minibatch, training_set, dqn,gamma )
local batch_size = #minibatch
local tmp_training_data = torch.Tensor(batch_size*3, input_vector_size):fill(0):cuda()
local tmp_action1 = {}
local tmp_action2 = {}
local tmp_reward1 = torch.Tensor(batch_size, 1):fill(0):cuda()
local tmp_reward2 = torch.Tensor(batch_size, 1):fill(0):cuda()
for l,memory in pairs(minibatch) do
tmp_training_data[l] = memory[1]
--print(memory[6])
tmp_training_data[l+batch_size] = memory[6]
tmp_training_data[l+2*batch_size] = memory[7]
table.insert(tmp_action1, memory[2])
table.insert(tmp_action2, memory[3])
tmp_reward1[l] = memory[4]
tmp_reward2[l] = memory[5]
end
local output = dqn:forward(tmp_training_data)
local output1 = output[{{1,batch_size}, {}}]:clone()
local output2 = output[{{batch_size+1,2*batch_size},{}}]
local output3 = output[{{batch_size*2+1,batch_size*3},{}}]
local tmp_v1, tmp_index1 = torch.max(output2,2)
local tmp_v2, tmp_index2 = torch.max(output3,2)
for l=1,batch_size do
output1[l][tmp_action1[l]] = tmp_v1[l] + gamma * tmp_reward1[l]
output1[l][tmp_action2[l]] = tmp_v2[l] + gamma * tmp_reward2[l]
end
training_set.data = tmp_training_data[{{1,batch_size}}]
training_set.label = output1
return training_set
end
function func_construct_dqn_pvn_training_data( minibatch, training_set, training_set_pvn, dqn,gamma )
local batch_size = #minibatch
local tmp_training_data = torch.Tensor(batch_size*3, input_vector_size):cuda()
local tmp_action1 = {}
local tmp_action2 = {}
local tmp_reward1 = torch.Tensor(batch_size, 1):fill(0):cuda()
local tmp_reward2 = torch.Tensor(batch_size, 1):fill(0):cuda()
local index_enable={}
for l,memory in pairs(minibatch) do
tmp_training_data[l] = memory[1]
tmp_training_data[l+batch_size] = memory[6]
tmp_training_data[l+2*batch_size] = memory[7]
table.insert(tmp_action1, memory[2])
table.insert(tmp_action2, memory[3])
tmp_reward1[l] = memory[4]
tmp_reward2[l] = memory[5]
if memory[8] == 1 then
training_set_pvn.label[l][1] = 1
table.insert(index_enable,l)
elseif memory[8] == -1 then
training_set_pvn.label[l][1] = 2
table.insert(index_enable,l)
end
end
local output = dqn:forward(tmp_training_data)
local output1 = output[{{1,batch_size}, {}}]:clone()
local output2 = output[{{batch_size+1,2*batch_size},{}}]
local output3 = output[{{batch_size*2+1,batch_size*3},{}}]
local tmp_v1, tmp_index1 = torch.max(output2,2)
local tmp_v2, tmp_index2 = torch.max(output3,2)
for l=1,batch_size do
output1[l][tmp_action1[l]] = tmp_v1[l] + gamma * tmp_reward1[l]
output1[l][tmp_action2[l]] = tmp_v2[l] + gamma * tmp_reward2[l]
end
training_set.data = tmp_training_data[{{1,batch_size}}]
training_set.label = output1
training_set_pvn.data = training_set.data:index(1,torch.LongTensor(index_enable))
training_set_pvn.data = training_set_pvn.data[{{},{1,feature_size}}]
training_set_pvn.label = training_set_pvn.label:index(1,torch.LongTensor(index_enable))
return training_set, training_set_pvn
end
--[[
function func_construct_dqn_pvn_training_data( minibatch, training_set, training_set_pvn, dqn,gamma )
for l, memory in pairs(minibatch) do
local tmp_input_vector = memory[1]
local tmp_action1 = memory[2]
local tmp_action2 = memory[3]
local tmp_reward1 = memory[4]
local tmp_reward2 = memory[5]
local tmp_new_input_vector1 = memory[6]
local tmp_new_input_vector2 = memory[7]
local tmp_obj_y_o_n = memory[8]
local old_action_output = dqn:forward(tmp_input_vector)
local new_action_output1 = dqn:forward(tmp_new_input_vector1)
local new_action_output2 = dqn:forward(tmp_new_input_vector2)
local y = old_action_output:clone()
local tmp_v, tmp_index = torch.max(new_action_output1,1)
tmp_v = tmp_v[1]
tmp_index = tmp_index[1]
local update_reward = tmp_reward1 + gamma * tmp_v
y[tmp_action1] = update_reward
tmp_v, tmp_index = torch.max(new_action_output2,1)
tmp_v = tmp_v[1]
tmp_index = tmp_index[1]
local update_reward = tmp_reward2 + gamma * tmp_v
y[tmp_action2] = update_reward
training_set.data[l] = tmp_input_vector
training_set.label[l] = y
training_set_pvn.data[l] = tmp_input_vector[{{1,feature_size}}]
training_set_pvn.label[l] = tmp_obj_y_o_n
end
return training_set, training_set_pvn
end
--]]
function func_construct_rgn_training_data(resnet_buffer,training_set)
for l,memory in pairs(resnet_buffer) do
training_set.data[l] = memory[1]
training_set.label[l] = memory[2]
end
return training_set
end
function func_construct_resnet_training_data(resnet_buffer,training_set)
for l,memory in pairs(resnet_buffer) do
training_set.data[l] = memory[1][1]
training_set.label[l] = memory[2]
end
return training_set
end