/
NNet.py
164 lines (130 loc) · 5.53 KB
/
NNet.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
import numpy as np
from keras.models import Sequential
from keras.models import Model
from keras.models import load_model
from keras.layers import Input, Dense, concatenate, Dropout
import matplotlib.pyplot as plt
import tools.eval
from argparse import ArgumentParser
import os
from autoencoder import encoder
DATA_DIR = 'C:/ml_data'
OUTPUT_DIR = './output/'
MODEL_DIR = './model/NNet'
def K_fold(X, train_test_margin=int(47500*4/5)):
X_in = X[:train_test_margin, :]
X_out = X[train_test_margin:, :]
return X_in, X_out
def build():
model = []
for feature in range(3):
input_MSD = Input(shape = (1024, ))
input_VAC = Input(shape = (1024, ))
input_pred = Input(shape = (3, ))
input_that = Input(shape = (1, ))
merge = concatenate([input_MSD, input_VAC])
out = Dropout(0.3)(merge)
out = Dense(16,
kernel_initializer='random_normal',
bias_initializer='random_normal',
activation='tanh')(out)
out = Dropout(0.3)(out)
merge = concatenate([out, input_pred])
out = Dense(3,
kernel_initializer='zeros',
bias_initializer='zeros',
activation='tanh')(merge)
merge = concatenate([out, input_that])
out = Dense(1,
kernel_initializer='ones',
bias_initializer='zeros')(merge)
input_list = [input_MSD, input_VAC, input_pred, input_that]
model.append(Model(inputs=input_list, outputs=out))
model[feature].compile(loss='mse', optimizer='adam')
return model
# (build, ) train model and print E_in
def train(X, y_predicted, y_train, model=None, iter=250, label='in'):
np.save(f'{DATA_DIR}/y_{label}.npy', y_train)
N, dim = X.shape
X_MSD = X[:, 0:1024]
X_VAC = X[:, 5000:6024]
'''
X_MSD = X[:, 0:5000]
X_VAC = X[:, 5000:10000]
X_MSD = encoder(X_MSD, layer_list = (2048, 1024), name = 'X_MSD_5000_1024', use_old=True)
X_VAC = encoder(X_VAC, layer_list = (2048, 1024), name = 'X_VAC_5000_1024', use_old=True)
'''
if model == None:
model = build()
elif model == 'LOAD':
model = [ load_model('model/NNet/model_%d.h5' % feature) for feature in range(3) ]
submissions = []
for feature in range(3):
y_predicted_that = y_predicted[:, feature]
X_train = [X_MSD, X_VAC, y_predicted, y_predicted_that]
print("------Training feature %d------" % feature)
total_steps = iter
for step in range(total_steps):
if step%50 == 0:
print('(feature %d) step = %d of %d' % (feature, step, total_steps))
model[feature].save('model/NNet/model_%d.h5' % feature)
cost = model[feature].train_on_batch(X_train, y_train[:, feature])
if step % 10 == 0:
print('train cost: ', cost)
# predict feature i and add to list
y_pred = model[feature].predict(X_train)
y_pred.shape = (y_pred.shape[0], )
submissions.append(y_pred)
submissions = np.array(submissions)
submissions = submissions.T
# save
np.savetxt(f'{OUTPUT_DIR}/submission_in.csv', submissions, delimiter=',')
# E_in
print("E_%s: \n%s" % (label, tools.eval.CalcError(y_name=f'y_{label}.npy', p_name=f'submission_{label}.csv')))
return model
# predict output and print E_out
def predict(X, y_predicted, y_test=None, model='LOAD', label='out'):
if label == 'test':
print('Generating TEST submission!')
np.save(f'{DATA_DIR}/y_{label}.npy', y_test)
if model == 'LOAD':
model = [ load_model('model/NNet/model_%d.h5' % feature) for feature in range(3) ]
X_MSD = X[:, 0:1024]
X_VAC = X[:, 5000:6024]
'''
X_MSD = X[:, 0:5000]
X_VAC = X[:, 5000:10000]
X_MSD = encoder(X_MSD, layer_list = (2048, 1024), name = 'X_MSD_5000_1024', use_old=True)
X_VAC = encoder(X_VAC, layer_list = (2048, 1024), name = 'X_VAC_5000_1024', use_old=True)
'''
submissions = []
for feature in range(3):
y_predicted_that = y_predicted[:, feature]
X_train = [X_MSD, X_VAC, y_predicted, y_predicted_that]
y_pred = model[feature].predict(X_train)
y_pred.shape = (y_pred.shape[0], )
submissions.append(y_pred)
submissions = np.array(submissions)
submissions = submissions.T
np.savetxt(f'{OUTPUT_DIR}/submission_{label}.csv', submissions, delimiter=',')
if not label == 'test':
print("E_%s: \n%s" % (label, tools.eval.CalcError(y_name=f'y_{label}.npy', p_name=f'submission_{label}.csv')))
return submissions
if __name__ == '__main__':
X_train = np.load(f'{DATA_DIR}/X_train.npy', mmap_mode='r')
X_in, X_out = K_fold(X_train)
X_test = np.load(f'{DATA_DIR}/X_test.npy', mmap_mode='r')
y_predicted = np.load(f'{DATA_DIR}/y_train_predict.npy', mmap_mode='r')
y_out_predicted = np.load(f'{DATA_DIR}/y_out_predict.npy', mmap_mode='r')
y_test_predicted = np.load(f'{DATA_DIR}/y_test_predict.npy', mmap_mode='r')
y_train = np.load(f'{DATA_DIR}/y_train.npy', mmap_mode='r')
y_in, y_out = K_fold(y_train)
model = train(X_in, y_predicted, y_in, iter=150)
y_pred = predict(X_out, y_out_predicted, y_out, model)
'''
y1 = np.loadtxt(f'{DATA_DIR}/test_submission1.csv', delimiter=',')
y_pred1 = (y_pred + y_test_predicted + y1)/3
np.savetxt(f'{OUTPUT_DIR}/submission_one.csv', y_pred1, delimiter=',')
#print("E_out1: \n%s" % (tools.eval.CalcError(y_name=f'y_out.npy', p_name=f'submission_one.csv')))
'''
exit()