-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.py
64 lines (44 loc) · 1.82 KB
/
Main.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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 5 15:09:40 2019
@author: ouhajime
"""
import sys
from SHAN import SHAN_Model
import keras
import os
from keras.preprocessing.sequence import pad_sequences
import Setting
import numpy as np
def main(argv):
mode = 'train'
if len(sys.argv) == 2:
mode = argv[1]
x_path = "./DATA/{}/glove_vec.npy".format(mode)
label_path = "./DATA/{}/{}_label.npy".format(mode,mode)
LSK_path = "./DATA/{}/LSK.npy".format(mode)
glove_target_vec_path = "./DATA/{}/glove_target_vec.npy".format(mode)
x = np.load(x_path)
label = np.load(label_path)
LSK = np.load(LSK_path)
target = np.load(glove_target_vec_path)
LSK = pad_sequences(LSK, maxlen=Setting.max_len, dtype='float', padding='post')
x = pad_sequences(x, maxlen=Setting.max_len, dtype='float', padding='post')
target = np.reshape(target,(-1,1,300))
LSK = np.reshape(LSK,(-1,Setting.max_len,1))
model = SHAN_Model()
model_weights_path = './Model/SHAN.h5'
if os.path.exists(model_weights_path):
model.load_weights (model_weights_path)
if mode == 'test':
print model.evaluate([x,target,LSK], label,batch_size=32)
elif mode == 'train':
checkpointer = keras.callbacks.ModelCheckpoint(filepath=model_weights_path, verbose=1, save_best_only=True,save_weights_only=True)
earlystop = keras.callbacks.EarlyStopping(monitor='val_loss', min_delta=0.00000001, patience=3, verbose=0, mode='auto', baseline=None, restore_best_weights=False)
model.fit([x,target,LSK], label,batch_size=128, epochs=31,validation_split=0.2,callbacks=[checkpointer,earlystop])
# model.save(model_weights_path)
else:
print('Input Mode Error !')
if __name__ == "__main__":
main(sys.argv)