-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDynaMoE_lesion.py
1468 lines (1275 loc) · 73.9 KB
/
DynaMoE_lesion.py
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import tensorflow as tf
import tensorflow.contrib.slim as slim
import numpy as np
import scipy.signal
import threading
import time
import os
import multiprocessing
import itertools
import sys
from python_modules import WCST_env as We #need env file ('WCST_env.py'); here it is in directory python_modules/
#####Helper fxns
#Function to specify which gpu to use
def set_gpu(gpu, frac):
"""
Function to specify which GPU to use
gpu: string for gpu (i.e. '0')
frac: memory fraction (i.e. 0.3 for 30%)
returns tf sess config
example usage:
sess = tf.Session(config=tf.ConfigProto(gpu_options=set_gpu('0', 0.5)))
"""
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = gpu
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=frac)
return gpu_options
#create fxn that allows worker to make a working copy of the central_network
def get_network_vars(from_scope,to_scope):
from_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, from_scope) #get the values of the collection from from_scope
to_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, to_scope) #get values from to_scope
op_holder = [] #list to hold the from_scope values
for from_var,to_var in zip(from_vars,to_vars): #for each corresponding pair of values in from_scope and to_scope
op_holder.append(to_var.assign(from_var)) #assign the from_scope value to the to_scope value and append it to op_holder
return op_holder #returns the from_scope values in a list
#for LESIONS - set some weights to zero; takes the central network weights and sets ones that should be zero to zero
def resetzero_network_vars(from_scope,to_scope):
global v_mask, pi_mask, input_w_mask
from_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, from_scope) #get the values of the collection from from_scope
to_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, to_scope) #get values from to_scope
resetfrom_vars_1 = from_vars
resetfrom_vars_1[15] = tf.multiply(from_vars[15],v_mask) #sets masked weights to zero
resetfrom_vars_1[14] = tf.multiply(from_vars[14],pi_mask)
resetfrom_vars_1[12] = tf.multiply(from_vars[12],input_w_mask)
op_holder = [] #list to hold the from_scope values
for from_var,to_var in zip(resetfrom_vars_1,to_vars): #for each corresponding pair of values in from_scope and to_scope
op_holder.append(to_var.assign(from_var)) #assign the from_scope value to the to_scope value and append it to op_holder
return op_holder #returns the from_scope values in a list
def worker_choose_action(policy_rec):
action_chosen_index = np.argmax(policy_rec) #choose action with highest prob
action_chosen = np.zeros(policy_rec.shape[1])
action_chosen[action_chosen_index] = 1
return(action_chosen) #1-hot action vector
def worker_act(env, state, action):
r_cur, s_new, ep_term, correct_ind, action = We.do_action_in_environment(env,state,action)
return r_cur, s_new, ep_term, correct_ind, action
def worker_decide(decision,pr_list):
chosen_policy = pr_list[np.argmax(decision)] #in dnet n1 is 0 and n2 is 1; action is 1-hot so n1 is [1,0], n2 is [0,1]
action_chosen = worker_choose_action(chosen_policy)
return(action_chosen) #1-hot action vector
#worker has a fxn to discount rewards - he'll save rewards as is until he needs to calculate gradients at the end of
#the episode, then he'll calculate the discounted rewards looking from each state to end of ep
#and use these as the rewards for the advantage calculation in defining the loss function (policy and value loss components
#both need the advantage)
# Discounting function used to calculate discounted returns in the form below when given [r0, r1, r2, r3, ..., rn-1, Vb]
# [y(0), y(1), y(2), ...], where
# e.g. y(0) = r_0 + gamma*r_1 + gamma^2 *r_2 + gamma^3 *r_3 + ... + gamma^(n-1)*r_n-1 + gamma^n*Vb
# e.g. y(2) = r_2 + gamma*r_3 + gamma^2 *r_4 + gamma^3 *r_5 + ... + gamma^(n-3)*r_n-1 + gamma^(n-2)*Vb
# last term is y(n) = Vb
def worker_discount(x, gamma):
return scipy.signal.lfilter([1], [1, -gamma], x[::-1], axis=0)[::-1]
###
#central network's goal is to optimize to task
#recruit bunch of workers to help
#work process: each worker takes a copy of the network as they get to work; go explore in environment using the copy
#based on their experience in environment, calculate updates they would make
#send the updates back to the central network - central network applies them; applies from each worker
#when worker send in update recommendations, worker also tosses copy of network and takes new copy of newest version
#repeat until told to stop
class the_network():
def __init__(self,state_space, action_space, name, trainer):
global NETSZ_D, NETSZ_E, LTYPE, rnnout_Lm, v_mask
with tf.variable_scope(name):
self.name = name #defines if its the central_network or a worker's working_copy_network
#placeholder for inputs
self.inputs = tf.placeholder(shape=[None,state_space],dtype=tf.float32) #environmental inputs (first just inputs from state status)
#if no lesion, LTYPE = 0
if LTYPE==1: #input of previous reward is lost
input_L = tf.constant([1,1,1,1,0],shape=[1,state_space],dtype=tf.float32)
elif LTYPE==2: #input of previous reward is lost
input_L = tf.constant([1,1,1,0,1],shape=[1,state_space],dtype=tf.float32)
elif LTYPE==3: #input of previous reward is lost
input_L = tf.constant([1,1,1,0,0],shape=[1,state_space],dtype=tf.float32)
else:
input_L = tf.constant([1,1,1,1,1],shape=[1,state_space],dtype=tf.float32) #aka don't do anything to inputs
self.inputs_p = tf.multiply(input_L, self.inputs)
rnn_in = tf.expand_dims(self.inputs_p,[0])
# network 1 (n1)
with tf.variable_scope('n1'):
#make the LSTM - receives from input and outputs to two fully connected layers, 1 for policy and 1 for value
n1_sizeoflstmcell = NETSZ_E
n1_lstm = tf.contrib.rnn.BasicLSTMCell(n1_sizeoflstmcell,state_is_tuple=True) #inputs feed to lstm cell
#define the lstm states ct and ht
n1_c_start = np.zeros((1,n1_lstm.state_size.c), np.float32)
n1_h_start = np.zeros((1,n1_lstm.state_size.h), np.float32)
self.n1_lstm_state_init = [n1_c_start, n1_h_start] #this is an attribute of self because it will be called when a network is made
n1_c_in = tf.placeholder(tf.float32, [1,n1_lstm.state_size.c])
n1_h_in = tf.placeholder(tf.float32, [1,n1_lstm.state_size.h])
self.n1_state_in = (n1_c_in, n1_h_in) # attribute of self because it will be called when using the network to predict
n1_state_in = tf.nn.rnn_cell.LSTMStateTuple(n1_c_in, n1_h_in) #form of c and h that can be passed back into the LSTM
n1_batch_size = tf.shape(self.inputs)[:1]
#connect inputs to lstm and parse lstm outputs
n1_lstm_outputs, n1_lstm_state = tf.nn.dynamic_rnn(n1_lstm,rnn_in,initial_state=n1_state_in, sequence_length=n1_batch_size)
n1_lstm_c, n1_lstm_h = n1_lstm_state
self.n1_state_out = (n1_lstm_c[:1, :], n1_lstm_h[:1, :]) #will call this to keep track of c and h states
n1_rnn_out = tf.reshape(n1_lstm_outputs, [-1,n1_sizeoflstmcell]) #output of each of the units in the LSTM
#fully connected layers at end to give policy and value
self.n1_policy_layer_output = slim.fully_connected(n1_rnn_out,action_space,
activation_fn=tf.nn.softmax,
weights_initializer=tf.contrib.layers.xavier_initializer(),
biases_initializer=None)
self.n1_value_layer_output = slim.fully_connected(n1_rnn_out,1,
activation_fn=None,
weights_initializer=tf.contrib.layers.xavier_initializer(),
biases_initializer=None)
if name != 'central_network':
#to calc gradients need
#state, action, policy, value, discounted_reward
#to get gradients:
#will give network s, ch_state_in -> these will generate
#self.policy_layer_output and self.value_layer_output
#then also give action, discounted_R
self.n1_A = tf.placeholder(shape=[None,action_space],dtype=tf.float32) #1-hot action taken from this state
self.n1_R = tf.placeholder(shape=[None,1],dtype=tf.float32) #reward estimate of this state based on rest of episode experience: rt + gamma**1 * rt+1 +...+gamma**k * V(s_end)
n1_selection_from_policy = tf.reduce_sum(self.n1_policy_layer_output * self.n1_A, [1]) #this is pi(A,S)
n1_sfp = tf.reshape(n1_selection_from_policy,[-1,1]) #makes it (batch_size, 1)
n1_advantage = self.n1_R - self.n1_value_layer_output
#define loss function: Total_loss = Policy_loss + Value_loss + Entropy_loss
n1_Policy_loss = - tf.log(n1_sfp + 1e-10) * tf.stop_gradient(n1_advantage)
#advantage tells the magnitude that you should move toward this policy choice
#movement of weights toward the policy taken, i.e. this policy maximizes the reward from this action so move toward it
#aka maximize this policy in this step
#aka - log (policy), i.e. minimize the negative
n1_Value_loss = tf.square(n1_advantage)
#entropy term to encourage exploration: H = - sum(p * log p); this term will be subtracted from total loss function
# so that if entropy is large (big H), the total loss will be lower
n1_Entropy_loss = - tf.reduce_sum(self.n1_policy_layer_output * tf.log(self.n1_policy_layer_output + 1e-10))
n1_c_V = 0.05
n1_c_E = 0.05
n1_Total_loss = n1_Policy_loss + n1_c_V*n1_Value_loss - n1_c_E*n1_Entropy_loss
self.n1_tl = n1_Total_loss
#calculate the gradient of the loss function - use this to update th network
n1_local_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, self.name+'/n1')
self.n1_gradient_loss = tf.gradients(n1_Total_loss,n1_local_vars) #worker will send these gradients (recommended updates) to central_network's gradient_list
n1_grads_to_apply = self.n1_gradient_loss
#worker can then apply the gradients to the central_network
n1_global_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, 'central_network/n1')
self.n1_apply_gradients = trainer.apply_gradients(zip(n1_grads_to_apply,n1_global_vars))
# network 2 (n2)
with tf.variable_scope('n2'):
n2_sizeoflstmcell = NETSZ_E
n2_lstm = tf.contrib.rnn.BasicLSTMCell(n2_sizeoflstmcell,state_is_tuple=True)
n2_c_start = np.zeros((1,n2_lstm.state_size.c), np.float32)
n2_h_start = np.zeros((1,n2_lstm.state_size.h), np.float32)
self.n2_lstm_state_init = [n2_c_start, n2_h_start]
n2_c_in = tf.placeholder(tf.float32, [1,n2_lstm.state_size.c])
n2_h_in = tf.placeholder(tf.float32, [1,n2_lstm.state_size.h])
self.n2_state_in = (n2_c_in, n2_h_in)
n2_state_in = tf.nn.rnn_cell.LSTMStateTuple(n2_c_in, n2_h_in)
n2_batch_size = tf.shape(self.inputs)[:1]
n2_lstm_outputs, n2_lstm_state = tf.nn.dynamic_rnn(n2_lstm,rnn_in,initial_state=n2_state_in, sequence_length=n2_batch_size)
n2_lstm_c, n2_lstm_h = n2_lstm_state
self.n2_state_out = (n2_lstm_c[:1, :], n2_lstm_h[:1, :])
n2_rnn_out = tf.reshape(n2_lstm_outputs, [-1,n2_sizeoflstmcell])
self.n2_policy_layer_output = slim.fully_connected(n2_rnn_out,action_space,
activation_fn=tf.nn.softmax,
weights_initializer=tf.contrib.layers.xavier_initializer(),
biases_initializer=None)
self.n2_value_layer_output = slim.fully_connected(n2_rnn_out,1,
activation_fn=None,
weights_initializer=tf.contrib.layers.xavier_initializer(),
biases_initializer=None)
if name != 'central_network':
self.n2_A = tf.placeholder(shape=[None,action_space],dtype=tf.float32)
self.n2_R = tf.placeholder(shape=[None,1],dtype=tf.float32)
n2_selection_from_policy = tf.reduce_sum(self.n2_policy_layer_output * self.n2_A, [1])
n2_sfp = tf.reshape(n2_selection_from_policy,[-1,1])
n2_advantage = self.n2_R - self.n2_value_layer_output
n2_Policy_loss = - tf.log(n2_sfp + 1e-10) * tf.stop_gradient(n2_advantage)
n2_Value_loss = tf.square(n2_advantage)
n2_Entropy_loss = - tf.reduce_sum(self.n2_policy_layer_output * tf.log(self.n2_policy_layer_output + 1e-10))
n2_c_V = 0.05
n2_c_E = 0.05
n2_Total_loss = n2_Policy_loss + n2_c_V*n2_Value_loss - n2_c_E*n2_Entropy_loss
self.n2_tl = n2_Total_loss
n2_local_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, self.name+'/n2')
self.n2_gradient_loss = tf.gradients(n2_Total_loss,n2_local_vars)
n2_grads_to_apply = self.n2_gradient_loss
n2_global_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, 'central_network/n2')
self.n2_apply_gradients = trainer.apply_gradients(zip(n2_grads_to_apply,n2_global_vars))
# network 3 (n3)
with tf.variable_scope('n3'):
n3_sizeoflstmcell = NETSZ_E
n3_lstm = tf.contrib.rnn.BasicLSTMCell(n3_sizeoflstmcell,state_is_tuple=True)
n3_c_start = np.zeros((1,n3_lstm.state_size.c), np.float32)
n3_h_start = np.zeros((1,n3_lstm.state_size.h), np.float32)
self.n3_lstm_state_init = [n3_c_start, n3_h_start]
n3_c_in = tf.placeholder(tf.float32, [1,n3_lstm.state_size.c])
n3_h_in = tf.placeholder(tf.float32, [1,n3_lstm.state_size.h])
self.n3_state_in = (n3_c_in, n3_h_in)
n3_state_in = tf.nn.rnn_cell.LSTMStateTuple(n3_c_in, n3_h_in)
n3_batch_size = tf.shape(self.inputs)[:1]
n3_lstm_outputs, n3_lstm_state = tf.nn.dynamic_rnn(n3_lstm,rnn_in,initial_state=n3_state_in, sequence_length=n3_batch_size)
n3_lstm_c, n3_lstm_h = n3_lstm_state
self.n3_state_out = (n3_lstm_c[:1, :], n3_lstm_h[:1, :])
n3_rnn_out = tf.reshape(n3_lstm_outputs, [-1,n3_sizeoflstmcell])
self.n3_policy_layer_output = slim.fully_connected(n3_rnn_out,action_space,
activation_fn=tf.nn.softmax,
weights_initializer=tf.contrib.layers.xavier_initializer(),
biases_initializer=None)
self.n3_value_layer_output = slim.fully_connected(n3_rnn_out,1,
activation_fn=None,
weights_initializer=tf.contrib.layers.xavier_initializer(),
biases_initializer=None)
if name != 'central_network':
self.n3_A = tf.placeholder(shape=[None,action_space],dtype=tf.float32)
self.n3_R = tf.placeholder(shape=[None,1],dtype=tf.float32)
n3_selection_from_policy = tf.reduce_sum(self.n3_policy_layer_output * self.n3_A, [1])
n3_sfp = tf.reshape(n3_selection_from_policy,[-1,1])
n3_advantage = self.n3_R - self.n3_value_layer_output
n3_Policy_loss = - tf.log(n3_sfp + 1e-10) * tf.stop_gradient(n3_advantage)
n3_Value_loss = tf.square(n3_advantage)
n3_Entropy_loss = - tf.reduce_sum(self.n3_policy_layer_output * tf.log(self.n3_policy_layer_output + 1e-10))
n3_c_V = 0.05
n3_c_E = 0.05
n3_Total_loss = n3_Policy_loss + n3_c_V*n3_Value_loss - n3_c_E*n3_Entropy_loss
self.n3_tl = n3_Total_loss
n3_local_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, self.name+'/n3')
self.n3_gradient_loss = tf.gradients(n3_Total_loss,n3_local_vars)
n3_grads_to_apply = self.n3_gradient_loss
n3_global_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, 'central_network/n3')
self.n3_apply_gradients = trainer.apply_gradients(zip(n3_grads_to_apply,n3_global_vars))
with tf.variable_scope('dnet'):
dnet_sizeoflstmcell = NETSZ_D
dnet_lstm = tf.contrib.rnn.BasicLSTMCell(dnet_sizeoflstmcell,state_is_tuple=True)
dnet_c_start = np.zeros((1,dnet_lstm.state_size.c), np.float32)
dnet_h_start = np.zeros((1,dnet_lstm.state_size.h), np.float32)
self.dnet_lstm_state_init = [dnet_c_start, dnet_h_start]
dnet_c_in = tf.placeholder(tf.float32, [1,dnet_lstm.state_size.c])
dnet_h_in = tf.placeholder(tf.float32, [1,dnet_lstm.state_size.h])
self.dnet_state_in = (dnet_c_in, dnet_h_in)
dnet_state_in = tf.nn.rnn_cell.LSTMStateTuple(dnet_c_in, dnet_h_in)
dnet_batch_size = tf.shape(self.inputs)[:1]
dnet_lstm_outputs, dnet_lstm_state = tf.nn.dynamic_rnn(dnet_lstm,rnn_in,initial_state=dnet_state_in, sequence_length=dnet_batch_size)
dnet_lstm_c, dnet_lstm_h = dnet_lstm_state
self.dnet_state_out = (dnet_lstm_c[:1, :], dnet_lstm_h[:1, :])
dnet_rnn_out = tf.reshape(dnet_lstm_outputs, [-1,dnet_sizeoflstmcell])
rnnout_L = tf.constant(rnnout_Lm,shape=[1,dnet_sizeoflstmcell],dtype=tf.float32)
dnet_rnn_out_p = tf.multiply(rnnout_L, dnet_rnn_out)
self.dnet_policy_layer_output = slim.fully_connected(dnet_rnn_out_p,3,
activation_fn=tf.nn.softmax,
weights_initializer=tf.contrib.layers.xavier_initializer(),
biases_initializer=None)
self.dnet_value_layer_output = slim.fully_connected(dnet_rnn_out_p,1,
activation_fn=None,
weights_initializer=tf.contrib.layers.xavier_initializer(),
biases_initializer=None)
if name != 'central_network':
self.dnet_D = tf.placeholder(shape=[None,3],dtype=tf.float32)
self.dnet_R = tf.placeholder(shape=[None,1],dtype=tf.float32)
dnet_selection_from_policy = tf.reduce_sum(self.dnet_policy_layer_output * self.dnet_D, [1])
dnet_sfp = tf.reshape(dnet_selection_from_policy,[-1,1])
dnet_advantage = self.dnet_R - self.dnet_value_layer_output
dnet_Policy_loss = - tf.log(dnet_sfp + 1e-10) * tf.stop_gradient(dnet_advantage)
dnet_Value_loss = tf.square(dnet_advantage)
dnet_Entropy_loss = - tf.reduce_sum(self.dnet_policy_layer_output * tf.log(self.dnet_policy_layer_output + 1e-10))
dnet_c_V = 0.05
dnet_c_E = 0.05
dnet_Total_loss = dnet_Policy_loss + dnet_c_V*dnet_Value_loss - dnet_c_E*dnet_Entropy_loss
self.dnet_tl = dnet_Total_loss
dnet_local_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, self.name+'/dnet')
self.lv = dnet_local_vars
self.dnet_gradient_loss = tf.gradients(dnet_Total_loss,dnet_local_vars)
dnet_grads_to_apply = self.dnet_gradient_loss
dnet_global_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, 'central_network/dnet')
self.dnet_apply_gradients = trainer.apply_gradients(zip(dnet_grads_to_apply,dnet_global_vars))
self.gv = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, 'central_network')
#central network isn't held by a worker - doesn't need to calculate loss and gradient; each worker has to do this
#workers then send the updates they would make to the central network
#central network uses the gradients to update itself
#after each episode, worker throws away the old copy of network and takes a copy of the uptodate global network to go work on
#this is a section of the network that only workers have - it takes the data they've collected from an episode and
# uses it to calculate losses and from these, gradients for each parameter (weight)
# the worker will then send these gradients to the central network and apply them to tune it
#worker can only be created if there is a global the_network object defined called central_network and a global variables
#defined: GLOBAL_EPISODE_COUNTER, STATE_SPACE, ACTION_SPACE, GAMMA, MAX_EPISODE_LEN
class worker():
def __init__(self,name,trainer,actorenv):
self.name = 'worker' + str(name)
self.actorenv = actorenv
self.env = We.make_new_env(actorenv=self.actorenv) #each worker creates own instance of environment to interact with
self.trainer = trainer
self.working_copy_network = the_network(state_space=STATE_SPACE, action_space=ACTION_SPACE, name=self.name, trainer=trainer)
self.working_copy_network_params = get_network_vars('central_network',self.name)
self.default_graph = tf.get_default_graph()
self.assesser = []
#when episode is done, worker gathers data and processes it
#passes processed data to his own network to calculate gradients
#applies those gradients to the central_network
#bootstrap value is 0 if episode ended in terminal state; V(s_n+1) if episode was cut off in state s_n+1
#i.e. worker was in s_n and did action a_n to move to s_n+1, then episode was cut because exceed max length
def train(self,training_data,bootstrap_value,gamma,sess):
global GLOBAL_EPISODE_COUNTER, TO_TRAIN
#first replace the rewards with the discounted-rewards because this is what the network needs to calc losses
array_training_data = np.array(training_data)
step_rewards = [ritem for sublist in array_training_data[:,3] for ritem in sublist] #list of the step by step rewards
step_rewards = step_rewards + [bootstrap_value]
discR = worker_discount(step_rewards,gamma)[:-1] #cut of the last value because it was just used to give discounted reward estimate
discR_listed = [[item] for item in discR]
array_training_data[:,3] = discR_listed
stacked_states = np.vstack(array_training_data[:,0])
stacked_action = np.vstack(array_training_data[:,2])
stacked_reward = np.vstack(array_training_data[:,3])
stacked_decision = np.vstack(array_training_data[:,6])
if (TO_TRAIN=='Experts_n1') | (TO_TRAIN=='Experts_n2') | (TO_TRAIN=='Experts_n3'):
if self.actorenv==[0]: #if training network 1 on shape-sorting
feed_dict = {self.working_copy_network.inputs:stacked_states,
self.working_copy_network.n1_state_in[0]:self.n1_train_rnn_state[0],
self.working_copy_network.n1_state_in[1]:self.n1_train_rnn_state[1],
self.working_copy_network.n1_A:stacked_action,
self.working_copy_network.n1_R:stacked_reward}
self.n1_train_rnn_state, gl, _ = sess.run([self.working_copy_network.n1_state_out,
self.working_copy_network.n1_gradient_loss,
self.working_copy_network.n1_apply_gradients],
feed_dict=feed_dict)
elif self.actorenv==[1]: #if training network 2 on color-sorting
feed_dict = {self.working_copy_network.inputs:stacked_states,
self.working_copy_network.n2_state_in[0]:self.n2_train_rnn_state[0],
self.working_copy_network.n2_state_in[1]:self.n2_train_rnn_state[1],
self.working_copy_network.n2_A:stacked_action,
self.working_copy_network.n2_R:stacked_reward}
self.n2_train_rnn_state, gl, _ = sess.run([self.working_copy_network.n2_state_out,
self.working_copy_network.n2_gradient_loss,
self.working_copy_network.n2_apply_gradients],
feed_dict=feed_dict)
elif self.actorenv==[2]: #if training network 2 on color-sorting
feed_dict = {self.working_copy_network.inputs:stacked_states,
self.working_copy_network.n3_state_in[0]:self.n3_train_rnn_state[0],
self.working_copy_network.n3_state_in[1]:self.n3_train_rnn_state[1],
self.working_copy_network.n3_A:stacked_action,
self.working_copy_network.n3_R:stacked_reward}
self.n3_train_rnn_state, gl, _ = sess.run([self.working_copy_network.n3_state_out,
self.working_copy_network.n3_gradient_loss,
self.working_copy_network.n3_apply_gradients],
feed_dict=feed_dict)
elif TO_TRAIN=='DNET':
feed_dict = {self.working_copy_network.inputs:stacked_states,
self.working_copy_network.dnet_state_in[0]:self.dnet_train_rnn_state[0],
self.working_copy_network.dnet_state_in[1]:self.dnet_train_rnn_state[1],
self.working_copy_network.dnet_D:stacked_decision,
self.working_copy_network.dnet_R:stacked_reward}
self.dnet_train_rnn_state, gv, _ = sess.run([self.working_copy_network.dnet_state_out,
self.working_copy_network.gv,
self.working_copy_network.dnet_apply_gradients],
feed_dict=feed_dict)
sess.run(resetzero_network_vars('central_network','central_network'))
elif TO_TRAIN=='DNETAllExpert':
coded_decision = [np.argmax(item) for item in stacked_decision] #0,1,2 for n1, n2, n3
which_newexpert = [0 if item!=0 else 1 for item in coded_decision] #all the n3 decisions get 1, all other get 0
n1_decision_index = np.argwhere(np.array(which_newexpert,ndmin=2)==1)[:,1]
which_newexpert = [0 if item!=1 else 1 for item in coded_decision] #all the n3 decisions get 1, all other get 0
n2_decision_index = np.argwhere(np.array(which_newexpert,ndmin=2)==1)[:,1]
which_newexpert = [0 if item!=2 else 1 for item in coded_decision] #all the n3 decisions get 1, all other get 0
n3_decision_index = np.argwhere(np.array(which_newexpert,ndmin=2)==1)[:,1]
if TrainNewOnly==True:
if WHICH_DNET=='1e':
if len(n1_decision_index)!=0:
#Get states in which n3 was used
n1_used_states = stacked_states[n1_decision_index,:]
n1_used_actions = stacked_action[n1_decision_index,:]
n1_used_rewards = stacked_reward[n1_decision_index,:]
n1_feed_dict = {self.working_copy_network.inputs:n1_used_states,
self.working_copy_network.n1_state_in[0]:self.n1_train_rnn_state[0],
self.working_copy_network.n1_state_in[1]:self.n1_train_rnn_state[1],
self.working_copy_network.n1_A:n1_used_actions,
self.working_copy_network.n1_R:n1_used_rewards}
self.n1_train_rnn_state, n1_gl, _ = sess.run([self.working_copy_network.n1_state_out,
self.working_copy_network.n1_gradient_loss,
self.working_copy_network.n1_apply_gradients],
feed_dict=n1_feed_dict)
elif WHICH_DNET=='2e':
if len(n2_decision_index)!=0:
#Get states in which n3 was used
n2_used_states = stacked_states[n2_decision_index,:]
n2_used_actions = stacked_action[n2_decision_index,:]
n2_used_rewards = stacked_reward[n2_decision_index,:]
n2_feed_dict = {self.working_copy_network.inputs:n2_used_states,
self.working_copy_network.n2_state_in[0]:self.n2_train_rnn_state[0],
self.working_copy_network.n2_state_in[1]:self.n2_train_rnn_state[1],
self.working_copy_network.n2_A:n2_used_actions,
self.working_copy_network.n2_R:n2_used_rewards}
self.n2_train_rnn_state, n2_gl, _ = sess.run([self.working_copy_network.n2_state_out,
self.working_copy_network.n2_gradient_loss,
self.working_copy_network.n2_apply_gradients],
feed_dict=n2_feed_dict)
elif WHICH_DNET=='3e':
if len(n3_decision_index)!=0:
#Get states in which n3 was used
n3_used_states = stacked_states[n3_decision_index,:]
n3_used_actions = stacked_action[n3_decision_index,:]
n3_used_rewards = stacked_reward[n3_decision_index,:]
n3_feed_dict = {self.working_copy_network.inputs:n3_used_states,
self.working_copy_network.n3_state_in[0]:self.n3_train_rnn_state[0],
self.working_copy_network.n3_state_in[1]:self.n3_train_rnn_state[1],
self.working_copy_network.n3_A:n3_used_actions,
self.working_copy_network.n3_R:n3_used_rewards}
self.n3_train_rnn_state, n3_gl, _ = sess.run([self.working_copy_network.n3_state_out,
self.working_copy_network.n3_gradient_loss,
self.working_copy_network.n3_apply_gradients],
feed_dict=n3_feed_dict)
else:
#if n3 was called upon, train it on the episodes it was called for
if len(n1_decision_index)!=0:
#Get states in which n3 was used
n1_used_states = stacked_states[n1_decision_index,:]
n1_used_actions = stacked_action[n1_decision_index,:]
n1_used_rewards = stacked_reward[n1_decision_index,:]
n1_feed_dict = {self.working_copy_network.inputs:n1_used_states,
self.working_copy_network.n1_state_in[0]:self.n1_train_rnn_state[0],
self.working_copy_network.n1_state_in[1]:self.n1_train_rnn_state[1],
self.working_copy_network.n1_A:n1_used_actions,
self.working_copy_network.n1_R:n1_used_rewards}
self.n1_train_rnn_state, n1_gl, _ = sess.run([self.working_copy_network.n1_state_out,
self.working_copy_network.n1_gradient_loss,
self.working_copy_network.n1_apply_gradients],
feed_dict=n1_feed_dict)
if len(n2_decision_index)!=0:
#Get states in which n3 was used
n2_used_states = stacked_states[n2_decision_index,:]
n2_used_actions = stacked_action[n2_decision_index,:]
n2_used_rewards = stacked_reward[n2_decision_index,:]
n2_feed_dict = {self.working_copy_network.inputs:n2_used_states,
self.working_copy_network.n2_state_in[0]:self.n2_train_rnn_state[0],
self.working_copy_network.n2_state_in[1]:self.n2_train_rnn_state[1],
self.working_copy_network.n2_A:n2_used_actions,
self.working_copy_network.n2_R:n2_used_rewards}
self.n2_train_rnn_state, n2_gl, _ = sess.run([self.working_copy_network.n2_state_out,
self.working_copy_network.n2_gradient_loss,
self.working_copy_network.n2_apply_gradients],
feed_dict=n2_feed_dict)
if len(n3_decision_index)!=0:
#Get states in which n3 was used
n3_used_states = stacked_states[n3_decision_index,:]
n3_used_actions = stacked_action[n3_decision_index,:]
n3_used_rewards = stacked_reward[n3_decision_index,:]
n3_feed_dict = {self.working_copy_network.inputs:n3_used_states,
self.working_copy_network.n3_state_in[0]:self.n3_train_rnn_state[0],
self.working_copy_network.n3_state_in[1]:self.n3_train_rnn_state[1],
self.working_copy_network.n3_A:n3_used_actions,
self.working_copy_network.n3_R:n3_used_rewards}
self.n3_train_rnn_state, n3_gl, _ = sess.run([self.working_copy_network.n3_state_out,
self.working_copy_network.n3_gradient_loss,
self.working_copy_network.n3_apply_gradients],
feed_dict=n3_feed_dict)
#train dnet
dnet_feed_dict = {self.working_copy_network.inputs:stacked_states,
self.working_copy_network.dnet_state_in[0]:self.dnet_train_rnn_state[0],
self.working_copy_network.dnet_state_in[1]:self.dnet_train_rnn_state[1],
self.working_copy_network.dnet_D:stacked_decision,
self.working_copy_network.dnet_R:stacked_reward}
self.dnet_train_rnn_state, gv, dnet_gl, _ = sess.run([self.working_copy_network.dnet_state_out,
self.working_copy_network.gv,
self.working_copy_network.dnet_gradient_loss,
self.working_copy_network.dnet_apply_gradients],
feed_dict=dnet_feed_dict)
def get_experience(self,sess,coord,NUM_TRAIN_EPS=1000,on_ep=True):
print ("Starting " + self.name)
global GLOBAL_EPISODE_COUNTER, NUMBER_OF_WORKERS, TO_TRAIN, WHICH_DNET, TRAIN_EP_COUNT, last400ep, train_to_profst, avecorthresh, aveover
with sess.as_default(), sess.graph.as_default(): #with this session and session graph set to default
firstpriorenvtype = True
stop_training = False
while not coord.should_stop(): #tf.coordinator is passed to each worker thread that is started; it coordinates threads
#get copy of the uptodate central_network parameters
sess.run(resetzero_network_vars('central_network','central_network'))
sess.run(self.working_copy_network_params)
#now worker network is same as uptodate central_network
training_data = []
#begin episode
#if its the first time, get a random envtype to be the t-1 envtype; otherwise give it the last envtype so it picks a different one
if firstpriorenvtype==True:
prevenvtype = np.random.choice(np.array([0,1,2]),p=[1/3,1/3,1/3])
firstpriorenvtype = False
else:
prevenvtype = self.env.envtype
self.env = We.make_new_env(last_ep_type=prevenvtype,actorenv=self.actorenv,status='train',training_deck_indices=training_deck_indices) #for each ep make a new env obj to get new baseline probs of A and B
start_state = We.get_start_state_from_env(self.env)
s_cur = start_state
if (TO_TRAIN=='Experts_n1') | (TO_TRAIN=='Experts_n2') | (TO_TRAIN=='Experts_n3'):
if self.actorenv==[0]:
n1_ch_state_in = self.working_copy_network.n1_lstm_state_init #defines ch_state_in as zeros
self.n1_train_rnn_state = n1_ch_state_in #to do training need to create this self variable to pass the LSTM state in and out
ep_steps = 0
num_correct = 0
if on_ep==True:
ep_term = False
wholess = 0
thanwho = 3
else:
wholess = 0
thanwho = 10
while wholess < thanwho:
#keep track of [s_cur,a_cur,r_cur,s_new,ep_term]
#feed st to network to get policy and value output
# policy_rec, value_pred, ch_state_out = self.working_copy_network.get_policy_and_value(s_cur,ch_state_in,sess)
n1_policy_rec, n1_ch_state_out = sess.run([self.working_copy_network.n1_policy_layer_output,
self.working_copy_network.n1_state_out],
feed_dict={self.working_copy_network.inputs:s_cur,
self.working_copy_network.n1_state_in[0]:n1_ch_state_in[0],
self.working_copy_network.n1_state_in[1]:n1_ch_state_in[1]})
#choose action based on policy_rec
d_cur = np.zeros(3)
a_cur = worker_choose_action(n1_policy_rec) #a_cur is the 1-hot action vector
r_cur, s_new, ep_term, correct_ind, action_t = worker_act(self.env,s_cur,a_cur)
#now for this step we have [s_cur, ch_state_in, a_cur, r_cur, s_new, ep_term] to use for training
new_step_in_environment = [s_cur, n1_ch_state_in, a_cur, r_cur, s_new, ep_term, d_cur]
training_data.append(new_step_in_environment) #this is the data to calculate gradients with
s_cur = s_new
n1_ch_state_in = n1_ch_state_out
ep_steps += 1
num_correct += correct_ind
if (on_ep==True) & (ep_term==True):
wholess += 1
elif on_ep==False:
wholess += 1
#check if max episode length has been reached
if wholess == thanwho:
#use s_cur and ch_state_in to get v(s_cur)
#_, value_pred, _ = self.working_copy_network.get_policy_and_value(s_cur,ch_state_in,sess)
value_pred = sess.run([self.working_copy_network.n1_value_layer_output],
feed_dict={self.working_copy_network.inputs:s_cur,
self.working_copy_network.n1_state_in[0]:n1_ch_state_in[0],
self.working_copy_network.n1_state_in[1]:n1_ch_state_in[1]})
bootstrap_value = value_pred #this is scalar passed to train() below
if ep_term==True:
if self.name=='workern10':
if train_to_profst==True: #if training until proficient or stable
last400ep = np.append(last400ep,ep_steps)
if len(last400ep)>=10:
last10ep = last400ep[-10:] #get last 10 episodes
if len(np.where(last10ep<15)[0])==10: #if last 10 episodes were less than 15 steps, proficiency achieved: stop training
stop_training = True
if len(last400ep) > 400: #if >400 points can test for stability criteria
last400ep = last400ep[-400:] #pop off first one so it is len=400
if np.abs(np.mean(last400ep[-200:])-np.mean(last400ep[0:200]))<1: #if diff between mean of last 200 and previous 200 is <1 stability achieved: stop training
stop_training = True
if train_assesser_on==True:
self.assesser.append(ep_steps)
if len(self.assesser)>aveover:
self.assesser.pop(0)
if np.mean(self.assesser)<avecorthresh:
stop_training = True
ep_steps = 0
num_correct = 0
prevenvtype = self.env.envtype
self.env = We.make_new_env(last_ep_type=prevenvtype,actorenv=self.actorenv,status='train',set_start_state=s_cur,training_deck_indices=training_deck_indices)
#when episode==done
#train processes the training data then runs it through the worker's network to calculate gradients,
#calculates gradients, then takes these to the central_network and uses them to update the central_network
elif self.actorenv==[1]:
n2_ch_state_in = self.working_copy_network.n2_lstm_state_init #defines ch_state_in as zeros
self.n2_train_rnn_state = n2_ch_state_in #to do training need to create this self variable to pass the LSTM state in and out
ep_steps = 0
num_correct = 0
if on_ep==True:
ep_term = False
wholess = 0
thanwho = 3
else:
wholess = 0
thanwho = 10
while wholess < thanwho:
n2_policy_rec, n2_ch_state_out = sess.run([self.working_copy_network.n2_policy_layer_output,
self.working_copy_network.n2_state_out],
feed_dict={self.working_copy_network.inputs:s_cur,
self.working_copy_network.n2_state_in[0]:n2_ch_state_in[0],
self.working_copy_network.n2_state_in[1]:n2_ch_state_in[1]})
#choose action based on policy_rec
d_cur = np.zeros(3)
a_cur = worker_choose_action(n2_policy_rec) #a_cur is the 1-hot action vector
r_cur, s_new, ep_term, correct_ind, action_t = worker_act(self.env,s_cur,a_cur)
new_step_in_environment = [s_cur, n2_ch_state_in, a_cur, r_cur, s_new, ep_term, d_cur]
training_data.append(new_step_in_environment) #this is the data to calculate gradients with
s_cur = s_new
n2_ch_state_in = n2_ch_state_out
ep_steps += 1
num_correct += correct_ind
if (on_ep==True) & (ep_term==True):
wholess += 1 #if on_ep=True then wholess is indicator of when ep is over -> train
elif on_ep==False:
wholess += 1 #if on_ep=False then wholess is counting cardpulls
#check if max episode length has been reached
if wholess == thanwho:
value_pred = sess.run([self.working_copy_network.n2_value_layer_output],
feed_dict={self.working_copy_network.inputs:s_cur,
self.working_copy_network.n2_state_in[0]:n2_ch_state_in[0],
self.working_copy_network.n2_state_in[1]:n2_ch_state_in[1]})
bootstrap_value = value_pred #this is scalar passed to train() below
if ep_term==True:
if (self.name=='workern20'):
if train_to_profst==True: #if training until proficient or stable
last400ep = np.append(last400ep,ep_steps)
if len(last400ep)>=10:
last10ep = last400ep[-10:] #get last 10 episodes
if len(np.where(last10ep<15)[0])==10: #if last 10 episodes were less than 15 steps, proficiency achieved: stop training
stop_training = True
if len(last400ep) > 400: #if >400 points can test for stability criteria
last400ep = last400ep[-400:] #pop off first one so it is len=400
if np.abs(np.mean(last400ep[-200:])-np.mean(last400ep[0:200]))<1: #if diff between mean of last 200 and previous 200 is <1 stability achieved: stop training
stop_training = True
if train_assesser_on==True:
self.assesser.append(ep_steps)
if len(self.assesser)>aveover:
self.assesser.pop(0)
if np.mean(self.assesser)<avecorthresh:
stop_training = True
ep_steps = 0
num_correct = 0
prevenvtype = self.env.envtype
self.env = We.make_new_env(last_ep_type=prevenvtype,actorenv=self.actorenv,status='train',set_start_state=s_cur,training_deck_indices=training_deck_indices)
elif self.actorenv==[2]:
n3_ch_state_in = self.working_copy_network.n3_lstm_state_init #defines ch_state_in as zeros
self.n3_train_rnn_state = n3_ch_state_in #to do training need to create this self variable to pass the LSTM state in and out
ep_steps = 0
num_correct = 0
if on_ep==True:
ep_term = False
wholess = 0
thanwho = 3
else:
wholess = 0
thanwho = 10
while wholess < thanwho:
n3_policy_rec, n3_ch_state_out = sess.run([self.working_copy_network.n3_policy_layer_output,
self.working_copy_network.n3_state_out],
feed_dict={self.working_copy_network.inputs:s_cur,
self.working_copy_network.n3_state_in[0]:n3_ch_state_in[0],
self.working_copy_network.n3_state_in[1]:n3_ch_state_in[1]})
#choose action based on policy_rec
d_cur = np.zeros(3)
a_cur = worker_choose_action(n3_policy_rec) #a_cur is the 1-hot action vector
r_cur, s_new, ep_term, correct_ind, action_t = worker_act(self.env,s_cur,a_cur)
new_step_in_environment = [s_cur, n3_ch_state_in, a_cur, r_cur, s_new, ep_term, d_cur]
training_data.append(new_step_in_environment) #this is the data to calculate gradients with
s_cur = s_new
n3_ch_state_in = n3_ch_state_out
ep_steps += 1
num_correct += correct_ind
if (on_ep==True) & (ep_term==True):
wholess += 1 #if on_ep=True then wholess is indicator of when ep is over -> train
elif on_ep==False:
wholess += 1 #if on_ep=False then wholess is counting cardpulls
#check if max episode length has been reached
if wholess == thanwho:
value_pred = sess.run([self.working_copy_network.n3_value_layer_output],
feed_dict={self.working_copy_network.inputs:s_cur,
self.working_copy_network.n3_state_in[0]:n3_ch_state_in[0],
self.working_copy_network.n3_state_in[1]:n3_ch_state_in[1]})
bootstrap_value = value_pred #this is scalar passed to train() below
if ep_term==True:
if (self.name=='workern30'):
if train_to_profst==True: #if training until proficient or stable
last400ep = np.append(last400ep,ep_steps)
if len(last400ep)>=10:
last10ep = last400ep[-10:] #get last 10 episodes
if len(np.where(last10ep<15)[0])==10: #if last 10 episodes were less than 15 steps, proficiency achieved: stop training
stop_training = True
if len(last400ep) > 400: #if >400 points can test for stability criteria
last400ep = last400ep[-400:] #pop off first one so it is len=400
if np.abs(np.mean(last400ep[-200:])-np.mean(last400ep[0:200]))<1: #if diff between mean of last 200 and previous 200 is <1 stability achieved: stop training
stop_training = True
if train_assesser_on==True:
self.assesser.append(ep_steps)
if len(self.assesser)>aveover:
self.assesser.pop(0)
if np.mean(self.assesser)<avecorthresh:
stop_training = True
ep_steps = 0
num_correct = 0
prevenvtype = self.env.envtype
self.env = We.make_new_env(last_ep_type=prevenvtype,actorenv=self.actorenv,status='train',set_start_state=s_cur,training_deck_indices=training_deck_indices)
elif TO_TRAIN=='DNET': #if getting experience for the decision network training or the newexpert n3
dnet_ch_state_in = self.working_copy_network.dnet_lstm_state_init #defines ch_state_in as zeros
self.dnet_train_rnn_state = dnet_ch_state_in #to do training need to create this self variable to pass the LSTM state in and out
n1_ch_state_in = self.working_copy_network.n1_lstm_state_init #defines ch_state_in as zeros
n2_ch_state_in = self.working_copy_network.n2_lstm_state_init #defines ch_state_in as zeros
n3_ch_state_in = self.working_copy_network.n3_lstm_state_init
ep_steps = 0
num_correct = 0
if on_ep==True:
ep_term = False
wholess = 0
thanwho = 3
else:
wholess = 0 #cardpulls
thanwho = 10 #cards_to_train_on
while wholess < thanwho:
dnet_policy_rec, dnet_ch_state_out, lv = sess.run([self.working_copy_network.dnet_policy_layer_output,
self.working_copy_network.dnet_state_out,
self.working_copy_network.lv],
feed_dict={self.working_copy_network.inputs:s_cur,
self.working_copy_network.dnet_state_in[0]:dnet_ch_state_in[0],
self.working_copy_network.dnet_state_in[1]:dnet_ch_state_in[1]})
#get the expert networks (1 & 2) policy rec and LSTM states to continue passing
n1pr, n2pr, n3pr, n1_ch_state_out, n2_ch_state_out, n3_ch_state_out = sess.run([self.working_copy_network.n1_policy_layer_output,
self.working_copy_network.n2_policy_layer_output,
self.working_copy_network.n3_policy_layer_output,
self.working_copy_network.n1_state_out,
self.working_copy_network.n2_state_out,
self.working_copy_network.n3_state_out],
feed_dict={self.working_copy_network.inputs:s_cur,
self.working_copy_network.n1_state_in[0]:n1_ch_state_in[0],
self.working_copy_network.n1_state_in[1]:n1_ch_state_in[1],
self.working_copy_network.n2_state_in[0]:n2_ch_state_in[0],
self.working_copy_network.n2_state_in[1]:n2_ch_state_in[1],
self.working_copy_network.n3_state_in[0]:n3_ch_state_in[0],
self.working_copy_network.n3_state_in[1]:n3_ch_state_in[1]})
#put all the expert network policy recommendations into a list to choose from
pr_list = [n1pr,n2pr,n3pr]
if WHICH_DNET=='1e':
dnet_policy_rec[0][1] = 0 #if not using n3, don't let choose n3
dnet_policy_rec[0][2] = 0 #if not using n3, don't let choose n3
elif WHICH_DNET=='2e':
dnet_policy_rec[0][2] = 0 #if not using n3, don't let choose n3
#decide which expert network to use based on dnet's policy_rec
d_cur = worker_choose_action(dnet_policy_rec) #d_cur is the 1-hot action vector that is the length of the number of expert networks
a_cur = worker_decide(d_cur,pr_list) #choses which policy to use and then uses that policy to select action
r_cur, s_new, ep_term, correct_ind, action_t = worker_act(self.env,s_cur,a_cur)
#now for this step we have [s_cur, ch_state_in, a_cur, r_cur, s_new, ep_term] to use for training
new_step_in_environment = [s_cur, dnet_ch_state_in, a_cur, r_cur, s_new, ep_term, d_cur]
training_data.append(new_step_in_environment) #this is the data to calculate gradients with
s_cur = s_new
dnet_ch_state_in = dnet_ch_state_out
n1_ch_state_in = n1_ch_state_out
n2_ch_state_in = n2_ch_state_out
n3_ch_state_in = n3_ch_state_out
ep_steps += 1
num_correct += correct_ind
if (on_ep==True) & (ep_term==True):
wholess += 1 #if on_ep=True then wholess is indicator of when ep is over -> train
elif on_ep==False:
wholess += 1 #if on_ep=False then wholess is counting cardpulls
#check if max episode length has been reached
if wholess == thanwho:
value_pred = sess.run([self.working_copy_network.dnet_value_layer_output],
feed_dict={self.working_copy_network.inputs:s_cur,
self.working_copy_network.dnet_state_in[0]:dnet_ch_state_in[0],
self.working_copy_network.dnet_state_in[1]:dnet_ch_state_in[1]})
bootstrap_value = value_pred #this is scalar passed to train() below
if ep_term==True:
if self.name=='workerd0':
if train_to_profst==True: #if training until proficient or stable
last400ep = np.append(last400ep,ep_steps)
if len(last400ep)>=10:
last10ep = last400ep[-10:] #get last 10 episodes
if len(np.where(last10ep<15)[0])==10: #if last 10 episodes were less than 15 steps, proficiency achieved: stop training
stop_training = True
if len(last400ep) > 400: #if >400 points can test for stability criteria
last400ep = last400ep[-400:] #pop off first one so it is len=400
if np.abs(np.mean(last400ep[-200:])-np.mean(last400ep[0:200]))<1: #if diff between mean of last 200 and previous 200 is <1 stability achieved: stop training
stop_training = True
if train_assesser_on==True:
self.assesser.append(ep_steps)
if len(self.assesser)>aveover:
self.assesser.pop(0)
if np.mean(self.assesser)<avecorthresh:
stop_training = True
ep_steps = 0
num_correct = 0
prevenvtype = self.env.envtype
self.env = We.make_new_env(last_ep_type=prevenvtype,actorenv=self.actorenv,status='train',set_start_state=s_cur,training_deck_indices=training_deck_indices)
elif TO_TRAIN=='DNETAllExpert':
dnet_ch_state_in = self.working_copy_network.dnet_lstm_state_init #defines ch_state_in as zeros
n1_ch_state_in = self.working_copy_network.n1_lstm_state_init #defines ch_state_in as zeros
n2_ch_state_in = self.working_copy_network.n2_lstm_state_init #defines ch_state_in as zeros
n3_ch_state_in = self.working_copy_network.n3_lstm_state_init #defines ch_state_in as zeros
self.n1_train_rnn_state = n1_ch_state_in
self.n2_train_rnn_state = n2_ch_state_in
self.n3_train_rnn_state = n3_ch_state_in #to do training need to create this self variable to pass the LSTM state in and out
self.dnet_train_rnn_state = dnet_ch_state_in
ep_steps = 0
num_correct = 0
if on_ep==True:
ep_term = False
wholess = 0
thanwho = 3
else:
wholess = 0 #cardpulls
thanwho = 10 #cards_to_train_on
while wholess < thanwho:
dnet_policy_rec, dnet_ch_state_out = sess.run([self.working_copy_network.dnet_policy_layer_output,
self.working_copy_network.dnet_state_out],
feed_dict={self.working_copy_network.inputs:s_cur,
self.working_copy_network.dnet_state_in[0]:dnet_ch_state_in[0],
self.working_copy_network.dnet_state_in[1]:dnet_ch_state_in[1]})
#get the expert networks (1 & 2) policy rec and LSTM states to continue passing
n1pr, n2pr, n3pr, n1_ch_state_out, n2_ch_state_out, n3_ch_state_out = sess.run([self.working_copy_network.n1_policy_layer_output,
self.working_copy_network.n2_policy_layer_output,
self.working_copy_network.n3_policy_layer_output,
self.working_copy_network.n1_state_out,
self.working_copy_network.n2_state_out,
self.working_copy_network.n3_state_out],
feed_dict={self.working_copy_network.inputs:s_cur,
self.working_copy_network.n1_state_in[0]:n1_ch_state_in[0],
self.working_copy_network.n1_state_in[1]:n1_ch_state_in[1],
self.working_copy_network.n2_state_in[0]:n2_ch_state_in[0],
self.working_copy_network.n2_state_in[1]:n2_ch_state_in[1],
self.working_copy_network.n3_state_in[0]:n3_ch_state_in[0],
self.working_copy_network.n3_state_in[1]:n3_ch_state_in[1]})
#put all the expert network policy recommendations into a list to choose from
pr_list = [n1pr,n2pr,n3pr]
if WHICH_DNET=='1e':
dnet_policy_rec[0][1] = 0 #if not using n3, don't let choose n3
dnet_policy_rec[0][2] = 0 #if not using n3, don't let choose n3
elif WHICH_DNET=='2e':
dnet_policy_rec[0][2] = 0 #if not using n3, don't let choose n3
#decide which expert network to use based on dnet's policy_rec
d_cur = worker_choose_action(dnet_policy_rec) #d_cur is the 1-hot action vector that is the length of the number of expert networks
a_cur = worker_decide(d_cur,pr_list) #choses which policy to use and then uses that policy to select action
r_cur, s_new, ep_term, correct_ind, action_t = worker_act(self.env,s_cur,a_cur)
#now for this step we have [s_cur, ch_state_in, a_cur, r_cur, s_new, ep_term] to use for training
new_step_in_environment = [s_cur, n3_ch_state_in, a_cur, r_cur, s_new, ep_term, d_cur]
training_data.append(new_step_in_environment) #this is the data to calculate gradients with
s_cur = s_new
dnet_ch_state_in = dnet_ch_state_out
n1_ch_state_in = n1_ch_state_out
n2_ch_state_in = n2_ch_state_out
n3_ch_state_in = n3_ch_state_out
ep_steps += 1
num_correct += correct_ind #will add 1 if was correct, 0 if incorrect
if (on_ep==True) & (ep_term==True):
wholess += 1 #if on_ep=True then wholess is indicator of when ep is over -> train
elif on_ep==False:
wholess += 1 #if on_ep=False then wholess is counting cardpulls
#check if max episode length has been reached
if wholess == thanwho:
value_pred = sess.run([self.working_copy_network.dnet_value_layer_output],
feed_dict={self.working_copy_network.inputs:s_cur,
self.working_copy_network.dnet_state_in[0]:dnet_ch_state_in[0],
self.working_copy_network.dnet_state_in[1]:dnet_ch_state_in[1]})
bootstrap_value = value_pred #this is scalar passed to train() below
if ep_term==True:
if self.name=='workerdne0':
if train_to_profst==True: #if training until proficient or stable
last400ep = np.append(last400ep,ep_steps)
if len(last400ep)>=10:
last10ep = last400ep[-10:] #get last 10 episodes
if len(np.where(last10ep<15)[0])==10: #if last 10 episodes were less than 15 steps, proficiency achieved: stop training
stop_training = True
if len(last400ep) > 400: #if >400 points can test for stability criteria
last400ep = last400ep[-400:] #pop off first one so it is len=400
if np.abs(np.mean(last400ep[-200:])-np.mean(last400ep[0:200]))<1: #if diff between mean of last 200 and previous 200 is <1 stability achieved: stop training
stop_training = True
if train_assesser_on==True:
self.assesser.append(ep_steps)
if len(self.assesser)>aveover:
self.assesser.pop(0)
if np.mean(self.assesser)<avecorthresh:
stop_training = True
ep_steps = 0
num_correct = 0
prevenvtype = self.env.envtype
self.env = We.make_new_env(last_ep_type=prevenvtype,actorenv=self.actorenv,status='train',set_start_state=s_cur,training_deck_indices=training_deck_indices) #for each ep make a new env obj to get new baseline probs of A and B
if stop_training==True:
coord.request_stop()
self.train(training_data,bootstrap_value,GAMMA,sess)