Browse Source

18个股 19大盘

yufeng 4 years ago
parent
commit
58acabef61

+ 5 - 5
mix/mix_kmeans_predict_1.py

@@ -3,9 +3,9 @@ import numpy as np
3 3
 from keras.models import load_model
4 4
 import joblib
5 5
 
6
-model_path = '161_18d_mix_5D_ma5_s_seq.h5' # 160roc分类 161dmi分类
7
-data_dir = 'D:\\data\\quantization\\dmi\\'
8
-args = 'stock160_18d_test_A'
6
+model_path = '160_18d_mix_6D_ma5_s_seq.h5' # 160roc分类 161dmi分类
7
+data_dir = 'D:\\data\\quantization\\macd\\'
8
+args = 'stock160_18d_train2_B'
9 9
 
10 10
 
11 11
 def read_data(path):
@@ -17,7 +17,7 @@ def read_data(path):
17 17
                 lines.append(line)
18 18
 
19 19
     size = len(lines[0])
20
-    train_x=[s[:size - 2] for s in lines]
20
+    train_x=[s[:size - 2 - 18*3] for s in lines]
21 21
     train_y=[s[size-1] for s in lines]
22 22
     return np.array(train_x),np.array(train_y),lines
23 23
 
@@ -69,7 +69,7 @@ def predict(file_path='', idx=-1):
69 69
     # test_x_b = test_x_b.reshape(test_x.shape[0], 18, 10, 1)
70 70
     test_x_c = test_x[:,18*24:]
71 71
 
72
-    model=load_model(model_path.split('.')[0] + '_' + str(idx) + '.h5')
72
+    model=load_model(model_path.split(".")[0] + '.h5')
73 73
     score = model.evaluate([test_x_c, test_x_a, ], test_y)
74 74
     print('MIX', score)
75 75
 

+ 3 - 3
mix/mix_kmeans_train_1.py

@@ -16,8 +16,8 @@ from keras import regularizers
16 16
 from keras.models import Model
17 17
 
18 18
 epochs= 50
19
-model_path = '161_18d_mix_5D_ma5_s_seq.h5'
20
-data_dir = 'D:\\data\\quantization\\dmi\\'
19
+model_path = '164_18d_mix_6D_ma5_s_seq.h5'
20
+data_dir = 'D:\\data\\quantization\\dapan\\'
21 21
 args = 'stock160_18d_train_A_'
22 22
 
23 23
 
@@ -49,7 +49,7 @@ def read_data(path):
49 49
 
50 50
 
51 51
 def mul_train(name="10_18d"):
52
-    for x in range(8, 12):
52
+    for x in range(1, 5):
53 53
         score = train(data_dir + name + str(x) + ".log", x) # stock160_18d_trai_0
54 54
 
55 55
         with open(data_dir + name + '_mix.log', 'a') as f:

+ 5 - 5
mix/mix_predict.py

@@ -47,12 +47,12 @@ def predict(file_path='', model_path='15min_dnn_seq.h5', idx=-1):
47 47
 
48 48
     test_x_tmp = test_x[:,:18*24]
49 49
     test_x_a = test_x_tmp.reshape(test_x.shape[0], 18, 24, 1)
50
-    test_x_b = test_x_tmp.reshape(test_x.shape[0], 18, 24)
50
+    # test_x_b = test_x_tmp.reshape(test_x.shape[0], 18, 24)
51 51
     # test_x_b = test_x_b.reshape(test_x.shape[0], 18, 2, 1)
52 52
     test_x_c = test_x[:,18*24:]
53 53
 
54 54
     model=load_model(model_path)
55
-    score = model.evaluate([test_x_c, test_x_a, test_x_b], test_y)
55
+    score = model.evaluate([test_x_c, test_x_a], test_y)
56 56
     print('MIX', score)
57 57
 
58 58
     up_num = 0
@@ -62,7 +62,7 @@ def predict(file_path='', model_path='15min_dnn_seq.h5', idx=-1):
62 62
     down_error = 0
63 63
     down_right = 0
64 64
     i = 0
65
-    result=model.predict([test_x_c, test_x_a, test_x_b])
65
+    result=model.predict([test_x_c, test_x_a])
66 66
     win_dnn = []
67 67
     for r in result:
68 68
         fact = test_y[i]
@@ -102,8 +102,8 @@ def predict(file_path='', model_path='15min_dnn_seq.h5', idx=-1):
102 102
 
103 103
 
104 104
 if __name__ == '__main__':
105
-    predict(file_path='D:\\data\\quantization\\stock160_18d_test.log', model_path='163_18d_mix_5D_ma5_s_seq.h5')
106
-    # predict(file_path='D:\\data\\quantization\\stock160_18d_train1.log', model_path='162_18d_mix_5D_ma5_s_seq.h5')
105
+    predict(file_path='D:\\data\\quantization\\stock160_18d_test.log', model_path='160_18d_mix_6D_ma5_s_seq.h5')
106
+    # predict(file_path='D:\\data\\quantization\\stock160_18d_train1.log', model_path='160_18d_mix_6D_ma5_s_seq.h5')
107 107
     # predict(file_path='D:\\data\\quantization\\stock6_test.log', model_path='15m_dnn_seq.h5')
108 108
     # multi_predict(model='15_18d')
109 109
     # predict_today(20200229, model='11_18d')

+ 13 - 13
mix/mix_predict_1.py

@@ -26,32 +26,31 @@ def _score(fact, line):
26 26
     up_error = 0
27 27
 
28 28
     if fact[0] == 1:
29
-        up_right = up_right + 1.12
29
+        up_right = up_right + 1.1
30 30
     elif fact[1] == 1:
31
-        up_right = up_right + 1.06
31
+        up_right = up_right + 1.04
32 32
     elif fact[2] == 1:
33 33
         up_right = up_right + 1
34
-        up_error = up_error + 0.5
35 34
     elif fact[3] == 1:
36 35
         up_right = up_right + 0.94
37 36
         up_error = up_error + 1
38 37
     else:
39 38
         up_error = up_error + 1
40
-        up_right = up_right + 0.88
39
+        up_right = up_right + 0.90
41 40
     return up_right,up_error
42 41
 
43 42
 
44 43
 def predict(file_path='', model_path='15min_dnn_seq.h5', idx=-1):
45 44
     test_x,test_y,lines=read_data(file_path)
46 45
 
47
-    test_x_a = test_x[:,:18*24]
48
-    test_x_a = test_x_a.reshape(test_x.shape[0], 18, 24, 1)
49
-    # test_x_b = test_x[:, 18*18:18*18+2*18]
50
-    # test_x_b = test_x_b.reshape(test_x.shape[0], 18, 2, 1)
51
-    test_x_c = test_x[:,18*24:]
46
+    test_x_a = test_x[:,:18*21]
47
+    test_x_a = test_x_a.reshape(test_x.shape[0], 18, 21, 1)
48
+    # test_x_b = test_x[:, 9*26:9*26+9*26]
49
+    # test_x_b = test_x_b.reshape(test_x.shape[0], 9, 26, 1)
50
+    test_x_c = test_x[:,18*21:]
52 51
 
53 52
     model=load_model(model_path)
54
-    score = model.evaluate([test_x_c, test_x_a], test_y)
53
+    score = model.evaluate([test_x_c, test_x_a, ], test_y)
55 54
     print('MIX', score)
56 55
 
57 56
     up_num = 0
@@ -61,7 +60,7 @@ def predict(file_path='', model_path='15min_dnn_seq.h5', idx=-1):
61 60
     down_error = 0
62 61
     down_right = 0
63 62
     i = 0
64
-    result=model.predict([test_x_c, test_x_a,])
63
+    result = model.predict([test_x_c, test_x_a, ])
65 64
     win_dnn = []
66 65
     for r in result:
67 66
         fact = test_y[i]
@@ -83,6 +82,7 @@ def predict(file_path='', model_path='15min_dnn_seq.h5', idx=-1):
83 82
                     down_error = down_error + 1
84 83
                     down_right = down_right + 1.06
85 84
                 elif fact[2] == 1:
85
+                    down_error = down_error + 0.5
86 86
                     down_right = down_right + 1
87 87
                 elif fact[3] == 1:
88 88
                     down_right = down_right + 0.94
@@ -100,8 +100,8 @@ def predict(file_path='', model_path='15min_dnn_seq.h5', idx=-1):
100 100
 
101 101
 
102 102
 if __name__ == '__main__':
103
-    # predict(file_path='D:\\data\\quantization\\stock160_18d_test.log', model_path='162_18d_mix_5D_ma5_s_seq.h5')
104
-    predict(file_path='D:\\data\\quantization\\stock160_18d_train1.log', model_path='162_18d_mix_5D_ma5_s_seq.h5')
103
+    predict(file_path='D:\\data\\quantization\\stock175_18d_test.log', model_path='175_18d_mix_6D_ma5_s_seq_2.h5')
104
+    # predict(file_path='D:\\data\\quantization\\stock175_18d_train1.log', model_path='175_18d_mix_6D_ma5_s_seq_2.h5')
105 105
     # predict(file_path='D:\\data\\quantization\\stock6_test.log', model_path='15m_dnn_seq.h5')
106 106
     # multi_predict(model='15_18d')
107 107
     # predict_today(20200229, model='11_18d')

+ 98 - 0
mix/mix_predict_180.py

@@ -0,0 +1,98 @@
1
+# -*- encoding:utf-8 -*-
2
+import numpy as np
3
+from keras.models import load_model
4
+import joblib
5
+
6
+
7
+def read_data(path):
8
+    lines = []
9
+    with open(path) as f:
10
+        for line in f.readlines()[:]:
11
+            line = eval(line.strip())
12
+            if line[-2][0].startswith('0') or line[-2][0].startswith('3'):
13
+                lines.append(line)
14
+
15
+    size = len(lines[0])
16
+    train_x=[s[:size - 2] for s in lines]
17
+    train_y=[s[size-1] for s in lines]
18
+    return np.array(train_x),np.array(train_y),lines
19
+
20
+
21
+def _score(fact, line):
22
+    with open('dnn_predict_dmi_18d.txt', 'a') as f:
23
+        f.write(str([line[-2], line[-1]]) + "\n")
24
+
25
+    up_right = 0
26
+    up_error = 0
27
+
28
+    if fact[0] == 1:
29
+        up_right = up_right + 1.1
30
+    elif fact[1] == 1:
31
+        up_right = up_right + 0.99
32
+        up_error = up_error + 0.2
33
+    else:
34
+        up_error = up_error + 1
35
+        up_right = up_right + 0.90
36
+    return up_right,up_error
37
+
38
+
39
+def predict(file_path='', model_path='15min_dnn_seq.h5', idx=-1):
40
+    test_x,test_y,lines=read_data(file_path)
41
+
42
+    test_x_a = test_x[:,:18*18]
43
+    test_x_a = test_x_a.reshape(test_x.shape[0], 18, 18, 1)
44
+    # test_x_b = test_x[:, 9*26:9*26+9*26]
45
+    # test_x_b = test_x_b.reshape(test_x.shape[0], 9, 26, 1)
46
+    test_x_c = test_x[:,18*18:]
47
+
48
+    model=load_model(model_path)
49
+    score = model.evaluate([test_x_c, test_x_a,], test_y)
50
+    print('MIX', score)
51
+
52
+    up_num = 0
53
+    up_error = 0
54
+    up_right = 0
55
+    down_num = 0
56
+    down_error = 0
57
+    down_right = 0
58
+    i = 0
59
+    result = model.predict([test_x_c, test_x_a,])
60
+    win_dnn = []
61
+    for r in result:
62
+        fact = test_y[i]
63
+
64
+        if idx in [-2]:
65
+            if r[0] > 0.5 or r[1] > 0.5:
66
+                pass
67
+        else:
68
+            if r[0] > 0.8:
69
+                tmp_right,tmp_error = _score(fact, lines[i])
70
+                up_right = tmp_right + up_right
71
+                up_error = tmp_error + up_error
72
+                up_num = up_num + 1
73
+            elif r[2] > 0.6:
74
+                if fact[0] == 1:
75
+                    down_error = down_error + 1
76
+                    down_right = down_right + 1.1
77
+                elif fact[1] == 1:
78
+                    down_error = down_error + 0.2
79
+                    down_right = down_right + 0.99
80
+                else:
81
+                    down_right = down_right + 0.88
82
+                down_num = down_num + 1
83
+
84
+        i = i + 1
85
+    if up_num == 0:
86
+        up_num = 1
87
+    if down_num == 0:
88
+        down_num = 1
89
+    print('MIX', up_right, up_num, up_right/up_num, up_error/up_num, down_right/down_num, down_error/down_num)
90
+    return win_dnn,up_right/up_num,down_right/down_num
91
+
92
+
93
+if __name__ == '__main__':
94
+    # predict(file_path='D:\\data\\quantization\\stock181_18d_test.log', model_path='181_18d_mix_6D_ma5_s_seq.h5')
95
+    predict(file_path='D:\\data\\quantization\\stock186A_18d_train1.log', model_path='186A_18d_mix_6D_ma5_s_seq.h5')
96
+    # predict(file_path='D:\\data\\quantization\\stock6_test.log', model_path='15m_dnn_seq.h5')
97
+    # multi_predict(model='15_18d')
98
+    # predict_today(20200229, model='11_18d')

+ 103 - 0
mix/mix_predict_190.py

@@ -0,0 +1,103 @@
1
+# -*- encoding:utf-8 -*-
2
+import numpy as np
3
+from keras.models import load_model
4
+import joblib
5
+
6
+
7
+def read_data(path):
8
+    lines = []
9
+    with open(path) as f:
10
+        for line in f.readlines()[:]:
11
+            line = eval(line.strip())
12
+            if line[-2][0].startswith('0') or line[-2][0].startswith('3'):
13
+                lines.append(line)
14
+
15
+    size = len(lines[0])
16
+    train_x=[s[:size - 2] for s in lines]
17
+    train_y=[s[size-1] for s in lines]
18
+    return np.array(train_x),np.array(train_y),lines
19
+
20
+
21
+def _score(fact, line):
22
+    # with open('dnn_predict_dmi_18d.txt', 'a') as f:
23
+    #     f.write(str([line[-2], line[-1]]) + "\n")
24
+
25
+    up_right = 0
26
+    up_error = 0
27
+
28
+    if fact[0] == 1:
29
+        up_right = up_right + 1.1
30
+    elif fact[1] == 1:
31
+        up_right = up_right + 0.99
32
+    elif fact[2] == 1:
33
+        up_right = up_right + 0.96
34
+        up_error = up_error + 0.5
35
+    else:
36
+        up_error = up_error + 1
37
+        up_right = up_right + 0.90
38
+    return up_right,up_error
39
+
40
+
41
+def predict(file_path='', model_path='15min_dnn_seq.h5', idx=-1):
42
+    test_x,test_y,lines=read_data(file_path)
43
+
44
+    test_x_a = test_x[:,:18*18]
45
+    test_x_a = test_x_a.reshape(test_x.shape[0], 18, 18, 1)
46
+    # test_x_b = test_x[:, 9*26:9*26+9*26]
47
+    # test_x_b = test_x_b.reshape(test_x.shape[0], 9, 26, 1)
48
+    test_x_c = test_x[:,18*18:]
49
+
50
+    model=load_model(model_path)
51
+    score = model.evaluate([test_x_c, test_x_a,], test_y)
52
+    print('MIX', score)
53
+
54
+    up_num = 0
55
+    up_error = 0
56
+    up_right = 0
57
+    down_num = 0
58
+    down_error = 0
59
+    down_right = 0
60
+    i = 0
61
+    result = model.predict([test_x_c, test_x_a,])
62
+    win_dnn = []
63
+
64
+    for r in result:
65
+        fact = test_y[i]
66
+
67
+        if idx in [-2]:
68
+            if r[0] > 0.5 or r[1] > 0.5:
69
+                pass
70
+        else:
71
+            if r[0] > 0.5:
72
+                tmp_right,tmp_error = _score(fact, lines[i])
73
+                up_right = tmp_right + up_right
74
+                up_error = tmp_error + up_error
75
+                up_num = up_num + 1
76
+            elif r[2] > 0.5:
77
+                if fact[0] == 1:
78
+                    down_error = down_error + 1
79
+                    down_right = down_right + 1.06
80
+                elif fact[1] == 1:
81
+                    down_error = down_error + 0.3
82
+                    down_right = down_right + 1
83
+                elif fact[2] == 1:
84
+                    down_right = down_right + 0.96
85
+                else:
86
+                    down_right = down_right + 0.9
87
+                down_num = down_num + 1
88
+
89
+        i = i + 1
90
+    if up_num == 0:
91
+        up_num = 1
92
+    if down_num == 0:
93
+        down_num = 1
94
+    print('MIX', up_right, up_num, up_right/up_num, up_error/up_num, down_right/down_num, down_error/down_num)
95
+    return win_dnn,up_right/up_num,down_right/down_num
96
+
97
+
98
+if __name__ == '__main__':
99
+    # predict(file_path='D:\\data\\quantization\\stock181_18d_test.log', model_path='181_18d_mix_6D_ma5_s_seq.h5')
100
+    predict(file_path='D:\\data\\quantization\\stock196_18d_train1.log', model_path='196_18d_mix_6D_ma5_s_seq.h5')
101
+    # predict(file_path='D:\\data\\quantization\\stock6_test.log', model_path='15m_dnn_seq.h5')
102
+    # multi_predict(model='15_18d')
103
+    # predict_today(20200229, model='11_18d')

+ 10 - 12
mix/mix_predict_by_day.py

@@ -41,18 +41,20 @@ def predict(file_path='', model_path='15min_dnn_seq'):
41 41
 
42 42
         for line in lines:
43 43
             train_x = np.array([line[:size - 1]])
44
-            train_x_a = train_x[:,:18*24]
45
-            train_x_a = train_x_a.reshape(train_x.shape[0], 18, 24, 1)
44
+            train_x_a = train_x[:,:18*18]
45
+            train_x_a = train_x_a.reshape(train_x.shape[0], 18, 18, 1)
46 46
             # train_x_b = train_x[:, 18*18:18*18+2*18]
47 47
             # train_x_b = train_x_b.reshape(train_x.shape[0], 18, 2, 1)
48
-            train_x_c = train_x[:,18*24:]
48
+            train_x_c = train_x[:,18*18:]
49 49
 
50 50
             result = model.predict([train_x_c, train_x_a])
51 51
 
52
-            if result[0][3] + result[0][4] > 0.5:
52
+            if result[0][2]> 0.5:
53 53
                 down_num = down_num + 1
54
-            elif result[0][1] + result[0][0] > 0.5:
55
-                up_num = up_num + 0.6
54
+            elif result[0][0] > 0.5:
55
+                up_num = up_num + 1
56
+            elif result[0][1] > 0.5:
57
+                up_num = up_num + 0.2
56 58
             # else:
57 59
             #     up_num = up_num + 0.4 # 乐观调大 悲观调小
58 60
             #     down_num = down_num + 0.6
@@ -64,13 +66,9 @@ def predict(file_path='', model_path='15min_dnn_seq'):
64 66
                 x1 = x1 + 1
65 67
             if maxx - result[0][2] == 0:
66 68
                 x2 = x2 + 1
67
-            if maxx - result[0][3] == 0:
68
-                x3 = x3 + 1
69
-            if maxx - result[0][4] == 0:
70
-                x4 = x4 + 1
71 69
 
72 70
         # print(key, int(up_num), int(down_num), (down_num*1.2 + 2)/(up_num*1.2 + 2), )
73
-        print(key, x0, x1, x2,x3,x4)
71
+        print(key, x0, x1, x2, (down_num*1.2 + 2)/(up_num*1.2 + 2))
74 72
 
75 73
 
76 74
 if __name__ == '__main__':
@@ -78,5 +76,5 @@ if __name__ == '__main__':
78 76
     # predict(file_path='D:\\data\\quantization\\stock9_18_20200220.log', model_path='18d_dnn_seq.h5')
79 77
     # predict(file_path='D:\\data\\quantization\\stock9_18_2.log', model_path='18d_dnn_seq.h5')
80 78
     # predict(file_path='D:\\data\\quantization\\stock16_18d_20200310.log', model_path='16_18d_mix_seq')
81
-    predict(file_path='D:\\data\\quantization\\stock160_18d_10D_20200313.log', model_path='160_18d_mix_10D_ma5_s_seq')
79
+    predict(file_path='D:\\data\\quantization\\stock186_18d_20191211.log', model_path='186_18d_mix_6D_ma5_s_seq')
82 80
     # predict(file_path='D:\\data\\quantization\\stock9_18_4.log', model_path='18d_dnn_seq.h5')

+ 85 - 0
mix/mix_predict_by_day_190.py

@@ -0,0 +1,85 @@
1
+# -*- encoding:utf-8 -*-
2
+import numpy as np
3
+from keras.models import load_model
4
+import joblib
5
+
6
+
7
+def read_data(path):
8
+    day_lines = {}
9
+    with open(path) as f:
10
+        for line in f.readlines()[:]:
11
+            line = eval(line.strip())
12
+            date = str(line[-1][-1])
13
+            if date in day_lines:
14
+                day_lines[date].append(line)
15
+            else:
16
+                day_lines[date] = [line]
17
+    # print(len(day_lines['20191230']))
18
+    return day_lines
19
+
20
+
21
+def predict(file_path='', model_path='15min_dnn_seq'):
22
+    day_lines = read_data(file_path)
23
+    print('数据读取完毕')
24
+
25
+    model=load_model(model_path + '.h5')
26
+    print('模型加载完毕')
27
+
28
+    items = sorted(day_lines.keys())
29
+    for key in items:
30
+        # print(day)
31
+        lines = day_lines[key]
32
+
33
+        up_num = 0
34
+        down_num = 0
35
+        size = len(lines[0])
36
+        x0 = 0
37
+        x1 = 0
38
+        x2 = 0
39
+        x3 = 0
40
+        x4 = 0
41
+
42
+        for line in lines:
43
+            train_x = np.array([line[:size - 1]])
44
+            train_x_a = train_x[:,:18*18]
45
+            train_x_a = train_x_a.reshape(train_x.shape[0], 18, 18, 1)
46
+            # train_x_b = train_x[:, 18*18:18*18+2*18]
47
+            # train_x_b = train_x_b.reshape(train_x.shape[0], 18, 2, 1)
48
+            train_x_c = train_x[:,18*18:]
49
+
50
+            result = model.predict([train_x_c, train_x_a])
51
+
52
+            ratio = 1
53
+            if train_x_c[0][-1] == 1:
54
+                ratio = 1.2
55
+
56
+            if result[0][0]> 0.5:
57
+                up_num = up_num + ratio
58
+            elif result[0][1] > 0.5:
59
+                up_num = up_num + 0.3
60
+            elif result[0][2] > 0.5:
61
+                down_num = down_num + 0.3
62
+            else:
63
+                down_num = down_num + ratio
64
+
65
+            maxx = max(result[0])
66
+            if maxx - result[0][0] == 0:
67
+                x0 = x0 + 1
68
+            if maxx - result[0][1] == 0:
69
+                x1 = x1 + 1
70
+            if maxx - result[0][2] == 0:
71
+                x2 = x2 + 1
72
+            if maxx - result[0][3] == 0:
73
+                x3 = x3 + 1
74
+
75
+        # print(key, int(up_num), int(down_num), (down_num*1.2 + 2)/(up_num*1.2 + 2), )
76
+        print(key, x0, x1, x2,x3, (down_num*1.2 + 2)/(up_num*1.2 + 2))
77
+
78
+
79
+if __name__ == '__main__':
80
+    # predict(file_path='D:\\data\\quantization\\stock6_5_test.log', model_path='5d_dnn_seq.h5')
81
+    # predict(file_path='D:\\data\\quantization\\stock9_18_20200220.log', model_path='18d_dnn_seq.h5')
82
+    # predict(file_path='D:\\data\\quantization\\stock9_18_2.log', model_path='18d_dnn_seq.h5')
83
+    # predict(file_path='D:\\data\\quantization\\stock16_18d_20200310.log', model_path='16_18d_mix_seq')
84
+    predict(file_path='D:\\data\\quantization\\stock196_18d_20200325.log', model_path='196_18d_mix_6D_ma5_s_seq')
85
+    # predict(file_path='D:\\data\\quantization\\stock9_18_4.log', model_path='18d_dnn_seq.h5')

+ 23 - 13
mix/mix_predict_everyday.py

@@ -5,10 +5,21 @@ import joblib
5 5
 
6 6
 
7 7
 holder_stock_list = [
8
-                        '000063.SZ', '002093.SZ'
9
-                        '300253.SZ', '300807.SZ',
8
+                        # 医疗
9
+                        '000150.SZ', '300300.SZ', '603990.SH', '300759.SZ', '300347.SZ', '300003.SZ', '300253.SZ',
10
+                        # 5G
11
+                        '300698.SZ', '600498.SH', '300310.SZ', '600353.SH', '603912.SH', '603220.SH', '300602.SZ', '600260.SH',
12
+                        # 车联网
13
+                        '002369.SZ', '002920.SZ', '300020.SZ', '002373.SZ', '002869.SZ',
14
+                        # 工业互联网
15
+                        '002184.SZ', '002364.SZ','300310.SZ', '300670.SZ', '300166.SZ', '002169.SZ', '002380.SZ',
16
+                        # 特高压
17
+                        '300341.SZ', '300670.SZ', '300018.SZ', '600268.SH', '002879.SZ',
18
+                        # 基础建设
19
+                        '300041.SZ', '603568.SH', '000967.SZ', '603018.SH',
20
+                        # B
21
+                        '002555.SZ', '002174.SZ'
10 22
 
11
-                         # b账户
12 23
 
13 24
     ]
14 25
 
@@ -82,17 +93,16 @@ def predict_today(file, day, model='10_18d', log=True):
82 93
 
83 94
     for line in lines:
84 95
         train_x = np.array([line[:size - 1]])
85
-        train_x_a = train_x[:,:18*24]
86
-        train_x_a = train_x_a.reshape(train_x.shape[0], 18, 24, 1)
87
-        # train_x_b = train_x[:, 18*18:18*18+2*18]
88
-        # train_x_b = train_x_b.reshape(train_x.shape[0], 18, 2, 1)
89
-        train_x_c = train_x[:,18*24:]
96
+        train_x_tmp = train_x[:,:18*18]
97
+        train_x_a = train_x_tmp.reshape(train_x.shape[0], 18, 18, 1)
98
+        # train_x_b = train_x_tmp.reshape(train_x.shape[0], 18, 24)
99
+        train_x_c = train_x[:,18*18:]
90 100
 
91
-        result = model.predict([train_x_c, train_x_a])
101
+        result = model.predict([train_x_c, train_x_a, ])
92 102
         # print(result, line[-1])
93 103
         stock = code_table.find_one({'ts_code':line[-1][0]})
94 104
 
95
-        if result[0][0] + result[0][1] > 0.5:
105
+        if result[0][0] > 0.5:
96 106
             if line[-1][0].startswith('688'):
97 107
                 continue
98 108
             # 去掉ST
@@ -141,11 +151,11 @@ def predict_today(file, day, model='10_18d', log=True):
141 151
                 with open('D:\\data\\quantization\\predict\\' + str(day) + '_mix.txt', mode='a', encoding="utf-8") as f:
142 152
                     f.write(str(line[-1]) + ' ' + stock['name'] + ' ' + stock['sw_industry'] + ' ' + str(concept_detail_list) + ' buy' + '\n')
143 153
 
144
-        elif result[0][2] > 0.5:
154
+        elif result[0][1] > 0.5:
145 155
             if stock['ts_code'] in holder_stock_list:
146 156
                 print(stock['ts_code'], stock['name'], '震荡评级')
147 157
 
148
-        elif result[0][3] + result[0][4] > 0.5:
158
+        elif result[0][2] > 0.5:
149 159
             if stock['ts_code'] in holder_stock_list:
150 160
                 print(stock['ts_code'], stock['name'], '赶紧卖出')
151 161
         else:
@@ -202,6 +212,6 @@ if __name__ == '__main__':
202 212
     # predict(file_path='D:\\data\\quantization\\stock6_5_test.log', model_path='5d_dnn_seq.h5')
203 213
     # predict(file_path='D:\\data\\quantization\\stock6_test.log', model_path='15m_dnn_seq.h5')
204 214
     # multi_predict()
205
-    predict_today("D:\\data\\quantization\\stock160_18d_20200316.log", 20200316, model='160_18d_mix_5D_ma5_s_seq.h5', log=True)
215
+    predict_today("D:\\data\\quantization\\stock186_18d_20200325.log", 20200325, model='186_18d_mix_6D_ma5_s_seq.h5', log=True)
206 216
     # join_two_day(20200305, 20200305)
207 217
     # check_everyday(20200311, 20200312)

+ 50 - 40
mix/mix_train_1.py

@@ -14,29 +14,37 @@ from keras.utils import np_utils
14 14
 from keras.layers import Conv2D, Activation, MaxPool2D, Flatten, Dense,Dropout,Input,MaxPooling2D,BatchNormalization,concatenate
15 15
 from keras import regularizers
16 16
 from keras.models import Model
17
+from keras.callbacks import EarlyStopping
17 18
 
18
-epochs= 440
19
-size = 580000
20
-file_path = 'D:\\data\\quantization\\stock160_18d_train.log'
21
-model_path = '160_18d_mix_5D_ma5_s_seq.h5'
19
+early_stopping = EarlyStopping(monitor='accuracy', patience=5, verbose=2)
22 20
 
21
+epochs= 108
22
+size = 450000 #18W 60W
23
+file_path = 'D:\\data\\quantization\\stock175_18d_train2.log'
24
+model_path = '175_18d_mix_6D_ma5_s_seq_2.h5'
23 25
 
24
-def read_data(path):
26
+
27
+def read_data(path, path1="D:\\data\\quantization\\stock175_18d_train1.log"):
25 28
     lines = []
26 29
     with open(path) as f:
27
-        i = 0
28 30
         for x in range(size): #610000
29 31
             line = eval(f.readline().strip())
30 32
             lines.append(line)
31 33
 
34
+    with open(path1) as f:
35
+        for x in range(50000):
36
+            line = eval(f.readline().strip())
37
+            lines.append(line)
38
+
32 39
     random.shuffle(lines)
33 40
     print('读取数据完毕')
34 41
 
35
-    d=int(0.7*len(lines))
42
+    d=int(0.85*len(lines))
43
+    length = len(lines[0])
36 44
 
37
-    train_x=[s[:-2] for s in lines[0:d]]
45
+    train_x=[s[:length - 2] for s in lines[0:d]]
38 46
     train_y=[s[-1] for s in lines[0:d]]
39
-    test_x=[s[:-2] for s in lines[d:]]
47
+    test_x=[s[:length - 2] for s in lines[d:]]
40 48
     test_y=[s[-1] for s in lines[d:]]
41 49
 
42 50
     print('转换数据完毕')
@@ -51,18 +59,18 @@ def read_data(path):
51 59
 
52 60
 train_x,train_y,test_x,test_y=read_data(file_path)
53 61
 
54
-train_x_a = train_x[:,:18*24]
55
-train_x_a = train_x_a.reshape(train_x.shape[0], 18, 24, 1)
56
-# train_x_b = train_x[:, 18*18:18*18+2*18]
57
-# train_x_b = train_x_b.reshape(train_x.shape[0], 18, 2, 1)
58
-train_x_c = train_x[:,18*24:]
62
+train_x_a = train_x[:,:18*19]
63
+train_x_a = train_x_a.reshape(train_x.shape[0], 18, 19, 1)
64
+# train_x_b = train_x[:, 9*26:18*26]
65
+# train_x_b = train_x_b.reshape(train_x.shape[0], 9, 26, 1)
66
+train_x_c = train_x[:,18*19:]
59 67
 
60 68
 
61 69
 def create_mlp(dim, regress=False):
62 70
     # define our MLP network
63 71
     model = Sequential()
64
-    model.add(Dense(64, input_dim=dim, activation="relu"))
65
-    model.add(Dense(64, activation="relu"))
72
+    model.add(Dense(96, input_dim=dim, activation="relu"))
73
+    model.add(Dense(96, activation="relu"))
66 74
 
67 75
     # check to see if the regression node should be added
68 76
     if regress:
@@ -72,7 +80,7 @@ def create_mlp(dim, regress=False):
72 80
     return model
73 81
 
74 82
 
75
-def create_cnn(width, height, depth, filters=(4, 6), kernel_size=(5, 6), regress=False, output=24):
83
+def create_cnn(width, height, depth, size=48, kernel_size=(5, 6), regress=False, output=24):
76 84
     # initialize the input shape and channel dimension, assuming
77 85
     # TensorFlow/channels-last ordering
78 86
     inputShape = (width, height, 1)
@@ -80,18 +88,15 @@ def create_cnn(width, height, depth, filters=(4, 6), kernel_size=(5, 6), regress
80 88
 
81 89
     # define the model input
82 90
     inputs = Input(shape=inputShape)
83
-
84
-    x = inputs
85
-
91
+    # x = inputs
86 92
     # CONV => RELU => BN => POOL
87
-    x = Conv2D(32, kernel_size, strides=2, padding="same")(x)
93
+    x = Conv2D(size, kernel_size, strides=2, padding="same")(inputs)
88 94
     x = Activation("relu")(x)
89 95
     x = BatchNormalization(axis=chanDim)(x)
90
-    # x = MaxPooling2D(pool_size=(2, 2))(x)
91
-    # if width > 2:
92
-    #     x = Conv2D(32, (10, 6), padding="same")(x)
93
-    #     x = Activation("relu")(x)
94
-    #     x = BatchNormalization(axis=chanDim)(x)
96
+
97
+    # y = Conv2D(24, (2, 8), strides=2, padding="same")(inputs)
98
+    # y = Activation("relu")(y)
99
+    # y = BatchNormalization(axis=chanDim)(y)
95 100
 
96 101
     # flatten the volume, then FC => RELU => BN => DROPOUT
97 102
     x = Flatten()(x)
@@ -118,8 +123,11 @@ def create_cnn(width, height, depth, filters=(4, 6), kernel_size=(5, 6), regress
118 123
 
119 124
 # create the MLP and CNN models
120 125
 mlp = create_mlp(train_x_c.shape[1], regress=False)
121
-cnn_0 = create_cnn(18, 24, 1, kernel_size=(6, 6), regress=False, output=256)
122
-# cnn_1 = create_cnn(18, 2, 1, kernel_size=(6,2), regress=False, output=36)
126
+# cnn_0 = create_cnn(18, 21, 1, kernel_size=(3, 3), size=64, regress=False, output=128)       # 31 97 46
127
+# cnn_0 = create_cnn(18, 21, 1, kernel_size=(6, 6), size=64, regress=False, output=128)         # 29 98 47
128
+# cnn_0 = create_cnn(18, 21, 1, kernel_size=(9, 9), size=64, regress=False, output=128)         # 28 97 53
129
+cnn_0 = create_cnn(18, 19, 1, kernel_size=(3, 19), size=96, regress=False, output=128)        #A 28 98 40 #B 38 97 51 #A 24 98 41  #b 33 97 48 #B
130
+# cnn_1 = create_cnn(9, 26, 1, kernel_size=(2, 14), size=36, regress=False, output=64)
123 131
 
124 132
 # create the input to our final set of layers as the *output* of both
125 133
 # the MLP and CNN
@@ -127,10 +135,10 @@ combinedInput = concatenate([mlp.output, cnn_0.output])
127 135
 
128 136
 # our final FC layer head will have two dense layers, the final one
129 137
 # being our regression head
130
-x = Dense(512, activation="relu", kernel_regularizer=regularizers.l1(0.003))(combinedInput)
138
+x = Dense(1024, activation="relu", kernel_regularizer=regularizers.l1(0.003))(combinedInput)
131 139
 x = Dropout(0.2)(x)
132
-x = Dense(512, activation="relu")(x)
133
-x = Dense(512, activation="relu")(x)
140
+x = Dense(1024, activation="relu")(x)
141
+x = Dense(1024, activation="relu")(x)
134 142
 # 在建设一层
135 143
 x = Dense(5, activation="softmax")(x)
136 144
 
@@ -156,13 +164,17 @@ model.fit(
156 164
     # validation_data=([testAttrX, testImagesX], testY),
157 165
     # epochs=int(3*train_x_a.shape[0]/1300),
158 166
     epochs=epochs,
159
-    batch_size=2048, shuffle=True)
167
+    batch_size=2048, shuffle=True,
168
+    callbacks=[early_stopping]
169
+)
160 170
 
161
-test_x_a = test_x[:,:18*24]
162
-test_x_a = test_x_a.reshape(test_x.shape[0], 18, 24, 1)
163
-# test_x_b = test_x[:, 18*18:18*18+2*18]
164
-# test_x_b = test_x_b.reshape(test_x.shape[0], 18, 2, 1)
165
-test_x_c = test_x[:,18*24:]
171
+model.save(model_path)
172
+
173
+test_x_a = test_x[:,:18*19]
174
+test_x_a = test_x_a.reshape(test_x.shape[0], 18, 19, 1)
175
+# test_x_b = test_x[:, 9*26:9*26+9*26]
176
+# test_x_b = test_x_b.reshape(test_x.shape[0], 9, 26, 1)
177
+test_x_c = test_x[:,18*19:]
166 178
 
167 179
 # make predictions on the testing data
168 180
 print("[INFO] predicting house prices...")
@@ -170,6 +182,4 @@ score  = model.evaluate([test_x_c, test_x_a], test_y)
170 182
 
171 183
 print(score)
172 184
 print('Test score:', score[0])
173
-print('Test accuracy:', score[1])
174
-
175
-model.save(model_path)
185
+print('Test accuracy:', score[1])

+ 202 - 0
mix/mix_train_180.py

@@ -0,0 +1,202 @@
1
+import keras
2
+# -*- encoding:utf-8 -*-
3
+import numpy as np
4
+from keras.models import Sequential
5
+# 优化方法选用Adam(其实可选项有很多,如SGD)
6
+from keras.optimizers import Adam
7
+import random
8
+from keras.models import load_model
9
+from imblearn.over_sampling import RandomOverSampler
10
+from keras.utils import np_utils
11
+# 用于模型初始化,Conv2D模型初始化、Activation激活函数,MaxPooling2D是池化层
12
+# Flatten作用是将多位输入进行一维化
13
+# Dense是全连接层
14
+from keras.layers import Conv2D, Activation, MaxPool2D, Flatten, Dense,Dropout,Input,MaxPooling2D,BatchNormalization,concatenate
15
+from keras import regularizers
16
+from keras.models import Model
17
+from keras.callbacks import EarlyStopping
18
+
19
+early_stopping = EarlyStopping(monitor='accuracy', patience=5, verbose=2)
20
+
21
+epochs= 138
22
+size = 400000 #18W 60W
23
+file_path = 'D:\\data\\quantization\\stock186B_18d_train2.log'
24
+model_path = '186B_18d_mix_6D_ma5_s_seq.h5'
25
+file_path1='D:\\data\\quantization\\stock186B_18d_test.log'
26
+'''
27
+1 MACD                      
28
+4 MACD不在cnn范围内         50,97,43
29
+2 DMI                       
30
+5 DMI不在cnn范围内          49,97,42
31
+3 ROC
32
+6 ROC不在cnn范围内          50,98,40
33
+8 RSI                       49,98,42
34
+'''
35
+'''
36
+a 加上大盘涨停和个股统计 涨停/跌停/创业板涨停跌停   39,98,39
37
+b 加上大盘涨停和个股统计 t1,t2,t3....    
38
+c MACD + ROC
39
+d DMI + ROC
40
+e RSI + ROC           
41
+'''
42
+
43
+def read_data(path, path1=file_path1):
44
+    lines = []
45
+    with open(path) as f:
46
+        for x in range(size): #610000
47
+            line = eval(f.readline().strip())
48
+            lines.append(line)
49
+
50
+    with open(path1) as f:
51
+        for x in range(50000):
52
+            line = eval(f.readline().strip())
53
+            lines.append(line)
54
+
55
+    random.shuffle(lines)
56
+    print('读取数据完毕')
57
+
58
+    d=int(0.85*len(lines))
59
+    length = len(lines[0])
60
+
61
+    train_x=[s[:length - 2] for s in lines[0:d]]
62
+    train_y=[s[-1] for s in lines[0:d]]
63
+    test_x=[s[:length - 2] for s in lines[d:]]
64
+    test_y=[s[-1] for s in lines[d:]]
65
+
66
+    print('转换数据完毕')
67
+
68
+    ros = RandomOverSampler(random_state=0)
69
+    X_resampled, y_resampled = ros.fit_sample(np.array(train_x), np.array(train_y))
70
+
71
+    print('数据重采样完毕')
72
+
73
+    return X_resampled,y_resampled,np.array(test_x),np.array(test_y)
74
+
75
+
76
+train_x,train_y,test_x,test_y=read_data(file_path)
77
+
78
+train_x_a = train_x[:,:18*18]
79
+train_x_a = train_x_a.reshape(train_x.shape[0], 18, 18, 1)
80
+# train_x_b = train_x[:, 9*26:18*26]
81
+# train_x_b = train_x_b.reshape(train_x.shape[0], 9, 26, 1)
82
+train_x_c = train_x[:,18*18:]
83
+
84
+
85
+def create_mlp(dim, regress=False):
86
+    # define our MLP network
87
+    model = Sequential()
88
+    model.add(Dense(96, input_dim=dim, activation="relu"))
89
+    model.add(Dense(96, activation="relu"))
90
+
91
+    # check to see if the regression node should be added
92
+    if regress:
93
+        model.add(Dense(1, activation="linear"))
94
+
95
+    # return our model
96
+    return model
97
+
98
+
99
+def create_cnn(width, height, depth, size=48, kernel_size=(5, 6), regress=False, output=24):
100
+    # initialize the input shape and channel dimension, assuming
101
+    # TensorFlow/channels-last ordering
102
+    inputShape = (width, height, 1)
103
+    chanDim = -1
104
+
105
+    # define the model input
106
+    inputs = Input(shape=inputShape)
107
+    # x = inputs
108
+    # CONV => RELU => BN => POOL
109
+    x = Conv2D(size, kernel_size, strides=2, padding="same")(inputs)
110
+    x = Activation("relu")(x)
111
+    x = BatchNormalization(axis=chanDim)(x)
112
+
113
+    # y = Conv2D(24, (2, 8), strides=2, padding="same")(inputs)
114
+    # y = Activation("relu")(y)
115
+    # y = BatchNormalization(axis=chanDim)(y)
116
+
117
+    # flatten the volume, then FC => RELU => BN => DROPOUT
118
+    x = Flatten()(x)
119
+    x = Dense(output)(x)
120
+    x = Activation("relu")(x)
121
+    x = BatchNormalization(axis=chanDim)(x)
122
+    x = Dropout(0.2)(x)
123
+
124
+    # apply another FC layer, this one to match the number of nodes
125
+    # coming out of the MLP
126
+    x = Dense(output)(x)
127
+    x = Activation("relu")(x)
128
+
129
+    # check to see if the regression node should be added
130
+    if regress:
131
+        x = Dense(1, activation="linear")(x)
132
+
133
+    # construct the CNN
134
+    model = Model(inputs, x)
135
+
136
+    # return the CNN
137
+    return model
138
+
139
+
140
+# create the MLP and CNN models
141
+mlp = create_mlp(train_x_c.shape[1], regress=False)
142
+# cnn_0 = create_cnn(18, 21, 1, kernel_size=(3, 3), size=64, regress=False, output=128)       # 31 97 46
143
+# cnn_0 = create_cnn(18, 21, 1, kernel_size=(6, 6), size=64, regress=False, output=128)         # 29 98 47
144
+# cnn_0 = create_cnn(18, 21, 1, kernel_size=(9, 9), size=64, regress=False, output=128)         # 28 97 53
145
+cnn_0 = create_cnn(18, 18, 1, kernel_size=(3, 18), size=96, regress=False, output=96)       #A23 99 33 A' 26 99 36 #B 34 98 43
146
+# cnn_1 = create_cnn(18, 21, 1, kernel_size=(18, 11), size=96, regress=False, output=96)
147
+# cnn_1 = create_cnn(9, 26, 1, kernel_size=(2, 14), size=36, regress=False, output=64)
148
+
149
+# create the input to our final set of layers as the *output* of both
150
+# the MLP and CNN
151
+combinedInput = concatenate([mlp.output, cnn_0.output])
152
+
153
+# our final FC layer head will have two dense layers, the final one
154
+# being our regression head
155
+x = Dense(1024, activation="relu", kernel_regularizer=regularizers.l1(0.003))(combinedInput)
156
+x = Dropout(0.2)(x)
157
+x = Dense(1024, activation="relu")(x)
158
+x = Dense(1024, activation="relu")(x)
159
+# 在建设一层
160
+x = Dense(3, activation="softmax")(x)
161
+
162
+# our final model will accept categorical/numerical data on the MLP
163
+# input and images on the CNN input, outputting a single value (the
164
+# predicted price of the house)
165
+model = Model(inputs=[mlp.input, cnn_0.input,], outputs=x)
166
+
167
+
168
+print("Starting training ")
169
+# h = model.fit(train_x, train_y, batch_size=4096*2, epochs=500, shuffle=True)
170
+
171
+# compile the model using mean absolute percentage error as our loss,
172
+# implying that we seek to minimize the absolute percentage difference
173
+# between our price *predictions* and the *actual prices*
174
+opt = Adam(lr=1e-3, decay=1e-3 / 200)
175
+model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=['accuracy'])
176
+
177
+# train the model
178
+print("[INFO] training model...")
179
+model.fit(
180
+    [train_x_c, train_x_a,], train_y,
181
+    # validation_data=([testAttrX, testImagesX], testY),
182
+    # epochs=int(3*train_x_a.shape[0]/1300),
183
+    epochs=epochs,
184
+    batch_size=2048, shuffle=True,
185
+    callbacks=[early_stopping]
186
+)
187
+
188
+model.save(model_path)
189
+
190
+test_x_a = test_x[:,:18*18]
191
+test_x_a = test_x_a.reshape(test_x.shape[0], 18, 18, 1)
192
+# test_x_b = test_x[:, 9*26:9*26+9*26]
193
+# test_x_b = test_x_b.reshape(test_x.shape[0], 9, 26, 1)
194
+test_x_c = test_x[:,18*18:]
195
+
196
+# make predictions on the testing data
197
+print("[INFO] predicting house prices...")
198
+score  = model.evaluate([test_x_c, test_x_a], test_y)
199
+
200
+print(score)
201
+print('Test score:', score[0])
202
+print('Test accuracy:', score[1])

+ 191 - 0
mix/mix_train_190.py

@@ -0,0 +1,191 @@
1
+import keras
2
+# -*- encoding:utf-8 -*-
3
+import numpy as np
4
+from keras.models import Sequential
5
+# 优化方法选用Adam(其实可选项有很多,如SGD)
6
+from keras.optimizers import Adam
7
+import random
8
+from keras.models import load_model
9
+from imblearn.over_sampling import RandomOverSampler
10
+from keras.utils import np_utils
11
+# 用于模型初始化,Conv2D模型初始化、Activation激活函数,MaxPooling2D是池化层
12
+# Flatten作用是将多位输入进行一维化
13
+# Dense是全连接层
14
+from keras.layers import Conv2D, Activation, MaxPool2D, Flatten, Dense,Dropout,Input,MaxPooling2D,BatchNormalization,concatenate
15
+from keras import regularizers
16
+from keras.models import Model
17
+from keras.callbacks import EarlyStopping
18
+
19
+early_stopping = EarlyStopping(monitor='accuracy', patience=5, verbose=2)
20
+
21
+epochs= 88
22
+size = 400000 #18W 60W
23
+file_path = 'D:\\data\\quantization\\stock196_18d_train2.log'
24
+model_path = '196_18d_mix_6D_ma5_s_seq.h5'
25
+file_path1='D:\\data\\quantization\\stock196_18d_test.log'
26
+
27
+'''
28
+结果均用使用ma
29
+4 ROC         
30
+'''
31
+
32
+def read_data(path, path1=file_path1):
33
+    lines = []
34
+    with open(path) as f:
35
+        for x in range(size): #610000
36
+            line = eval(f.readline().strip())
37
+            lines.append(line)
38
+
39
+    # with open(path1) as f:
40
+    #     for x in range(50000):
41
+    #         line = eval(f.readline().strip())
42
+    #         lines.append(line)
43
+
44
+    random.shuffle(lines)
45
+    print('读取数据完毕')
46
+
47
+    d=int(0.85*len(lines))
48
+    length = len(lines[0])
49
+
50
+    train_x=[s[:length - 2] for s in lines[0:d]]
51
+    train_y=[s[-1] for s in lines[0:d]]
52
+    test_x=[s[:length - 2] for s in lines[d:]]
53
+    test_y=[s[-1] for s in lines[d:]]
54
+
55
+    print('转换数据完毕')
56
+
57
+    ros = RandomOverSampler(random_state=0)
58
+    X_resampled, y_resampled = ros.fit_sample(np.array(train_x), np.array(train_y))
59
+
60
+    print('数据重采样完毕')
61
+
62
+    return X_resampled,y_resampled,np.array(test_x),np.array(test_y)
63
+
64
+
65
+train_x,train_y,test_x,test_y=read_data(file_path)
66
+
67
+train_x_a = train_x[:,:18*18]
68
+train_x_a = train_x_a.reshape(train_x.shape[0], 18, 18, 1)
69
+# train_x_b = train_x[:, 9*26:18*26]
70
+# train_x_b = train_x_b.reshape(train_x.shape[0], 9, 26, 1)
71
+train_x_c = train_x[:,18*18:]
72
+
73
+
74
+def create_mlp(dim, regress=False):
75
+    # define our MLP network
76
+    model = Sequential()
77
+    model.add(Dense(96, input_dim=dim, activation="relu"))
78
+    model.add(Dense(96, activation="relu"))
79
+
80
+    # check to see if the regression node should be added
81
+    if regress:
82
+        model.add(Dense(1, activation="linear"))
83
+
84
+    # return our model
85
+    return model
86
+
87
+
88
+def create_cnn(width, height, depth, size=48, kernel_size=(5, 6), regress=False, output=24):
89
+    # initialize the input shape and channel dimension, assuming
90
+    # TensorFlow/channels-last ordering
91
+    inputShape = (width, height, 1)
92
+    chanDim = -1
93
+
94
+    # define the model input
95
+    inputs = Input(shape=inputShape)
96
+    # x = inputs
97
+    # CONV => RELU => BN => POOL
98
+    x = Conv2D(size, kernel_size, strides=2, padding="same")(inputs)
99
+    x = Activation("relu")(x)
100
+    x = BatchNormalization(axis=chanDim)(x)
101
+
102
+    # y = Conv2D(24, (2, 8), strides=2, padding="same")(inputs)
103
+    # y = Activation("relu")(y)
104
+    # y = BatchNormalization(axis=chanDim)(y)
105
+
106
+    # flatten the volume, then FC => RELU => BN => DROPOUT
107
+    x = Flatten()(x)
108
+    x = Dense(output)(x)
109
+    x = Activation("relu")(x)
110
+    x = BatchNormalization(axis=chanDim)(x)
111
+    x = Dropout(0.2)(x)
112
+
113
+    # apply another FC layer, this one to match the number of nodes
114
+    # coming out of the MLP
115
+    x = Dense(output)(x)
116
+    x = Activation("relu")(x)
117
+
118
+    # check to see if the regression node should be added
119
+    if regress:
120
+        x = Dense(1, activation="linear")(x)
121
+
122
+    # construct the CNN
123
+    model = Model(inputs, x)
124
+
125
+    # return the CNN
126
+    return model
127
+
128
+
129
+# create the MLP and CNN models
130
+mlp = create_mlp(train_x_c.shape[1], regress=False)
131
+# cnn_0 = create_cnn(18, 21, 1, kernel_size=(3, 3), size=64, regress=False, output=128)       # 31 97 46
132
+# cnn_0 = create_cnn(18, 21, 1, kernel_size=(6, 6), size=64, regress=False, output=128)         # 29 98 47
133
+# cnn_0 = create_cnn(18, 21, 1, kernel_size=(9, 9), size=64, regress=False, output=128)         # 28 97 53
134
+cnn_0 = create_cnn(18, 18, 1, kernel_size=(3, 18), size=96, regress=False, output=96)       #A23 99 33 A' 26 99 36 #B 34 98 43
135
+# cnn_1 = create_cnn(18, 21, 1, kernel_size=(18, 11), size=96, regress=False, output=96)
136
+# cnn_1 = create_cnn(9, 26, 1, kernel_size=(2, 14), size=36, regress=False, output=64)
137
+
138
+# create the input to our final set of layers as the *output* of both
139
+# the MLP and CNN
140
+combinedInput = concatenate([mlp.output, cnn_0.output])
141
+
142
+# our final FC layer head will have two dense layers, the final one
143
+# being our regression head
144
+x = Dense(1024, activation="relu", kernel_regularizer=regularizers.l1(0.003))(combinedInput)
145
+x = Dropout(0.2)(x)
146
+x = Dense(1024, activation="relu")(x)
147
+x = Dense(1024, activation="relu")(x)
148
+# 在建设一层
149
+x = Dense(4, activation="softmax")(x)
150
+
151
+# our final model will accept categorical/numerical data on the MLP
152
+# input and images on the CNN input, outputting a single value (the
153
+# predicted price of the house)
154
+model = Model(inputs=[mlp.input, cnn_0.input,], outputs=x)
155
+
156
+
157
+print("Starting training ")
158
+# h = model.fit(train_x, train_y, batch_size=4096*2, epochs=500, shuffle=True)
159
+
160
+# compile the model using mean absolute percentage error as our loss,
161
+# implying that we seek to minimize the absolute percentage difference
162
+# between our price *predictions* and the *actual prices*
163
+opt = Adam(lr=1e-3, decay=1e-3 / 200)
164
+model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=['accuracy'])
165
+
166
+# train the model
167
+print("[INFO] training model...")
168
+model.fit(
169
+    [train_x_c, train_x_a,], train_y,
170
+    # validation_data=([testAttrX, testImagesX], testY),
171
+    # epochs=int(3*train_x_a.shape[0]/1300),
172
+    epochs=epochs,
173
+    batch_size=2048, shuffle=True,
174
+    callbacks=[early_stopping]
175
+)
176
+
177
+model.save(model_path)
178
+
179
+test_x_a = test_x[:,:18*18]
180
+test_x_a = test_x_a.reshape(test_x.shape[0], 18, 18, 1)
181
+# test_x_b = test_x[:, 9*26:9*26+9*26]
182
+# test_x_b = test_x_b.reshape(test_x.shape[0], 9, 26, 1)
183
+test_x_c = test_x[:,18*18:]
184
+
185
+# make predictions on the testing data
186
+print("[INFO] predicting house prices...")
187
+score  = model.evaluate([test_x_c, test_x_a], test_y)
188
+
189
+print(score)
190
+print('Test score:', score[0])
191
+print('Test accuracy:', score[1])

+ 2 - 3
mix/mix_train_2.py

@@ -16,10 +16,10 @@ from keras.layers import LSTM
16 16
 from keras import regularizers
17 17
 from keras.models import Model
18 18
 
19
-epochs= 440
19
+epochs= 100
20 20
 size = 440000
21 21
 file_path = 'D:\\data\\quantization\\stock160_18d_train.log'
22
-model_path = '163_18d_mix_5D_ma5_s_seq.h5'
22
+model_path = '170_18d_mix_5D_ma5_s_seq.h5'
23 23
 
24 24
 
25 25
 def read_data(path):
@@ -184,7 +184,6 @@ model.fit(
184 184
 test_x_tmp = test_x[:,:18*24]
185 185
 test_x_a = test_x_tmp.reshape(test_x.shape[0], 18, 24, 1)
186 186
 test_x_b = test_x_tmp.reshape(test_x.shape[0], 18, 24)
187
-# test_x_b = test_x_b.reshape(test_x.shape[0], 18, 2, 1)
188 187
 test_x_c = test_x[:,18*24:]
189 188
 
190 189
 # make predictions on the testing data