File size: 1,749 Bytes
2437f34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# ====================
# parameter update part
# ====================

from dual_network import DN_INPUT_SHAPE
from tensorflow.keras.callbacks import LearningRateScheduler, LambdaCallback
from tensorflow.keras.models import load_model
from tensorflow.keras import backend as K
from pathlib import Path
import numpy as np
import pickle

NUM_EPOCH = 100
BATCH_SIZE = 128

def load_data():
    history_path = sorted(Path('./data').glob('*.history'))[-1]
    with history_path.open(mode='rb') as f:
        return pickle.load(f)
    
# Training the dual network
def train_network():
    # Loading training data
    history = load_data()
    s, p, v = zip(*history)

    # Reshaping the input data for training
    a, b, c = DN_INPUT_SHAPE
    s = np.array(s)
    s = s.reshape(len(s), c, a, b).transpose(0, 2, 3, 1)
    p = np.array(p)
    v = np.array(v)

    # Loading the best player's model
    model = load_model('./model/best.keras')

    # Compiling the model
    model.compile(loss=['categorical_crossentropy', 'mse'], optimizer='adam')

    # Learning rate
    def step_decay(epoch):
        x = 0.001
        if epoch >= 50: x = 0.0005
        if epoch >= 80: x = 0.00025
        return x
    lr_decay = LearningRateScheduler(step_decay)

    # Output
    print_callback = LambdaCallback(
        on_epoch_begin=lambda epoch, logs: print('\rTrain {}/{}'.format(epoch + 1, NUM_EPOCH), end='')
    )

    # Executing training
    model.fit(
        s, [p, v], batch_size=BATCH_SIZE , epochs=NUM_EPOCH, verbose=0, callbacks=[lr_decay, print_callback]
    )

    # Saving the latest player's model
    model.save('./model/latest.keras')

    # Clearing the model
    K.clear_session()
    del model

if __name__ == '__main__':
    train_network()