-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain_infer.py
More file actions
158 lines (140 loc) · 6.37 KB
/
Main_infer.py
File metadata and controls
158 lines (140 loc) · 6.37 KB
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
import tensorflow as tf
import numpy as np
import math
import os
import matplotlib.pyplot as plt
from matplotlib import gridspec
import tensorflow.python.framework.dtypes
from numpy import vstack
#from sklearn.datasets import fetch_california_housing
from sklearn.gaussian_process import GaussianProcessRegressor
from scipy.sparse import csr_matrix
from scipy.sparse import coo_matrix
from feature_reduction import *
from autoencoder import *
import pandas as pd
import autokeras as ak
import timeit
import time
import pdb
from configuration import args
from prepare_data import generate_datasets, normalize
from Visualization_Bayesian import *
from bayes_opt.logger import JSONLogger
from bayes_opt.event import Events
import timeit
import time
os.environ["CUDA_VISIBLE_DEVICES"]="0"
def csr2dense(X_train_sparse):
x_data = np.array(X_train_sparse)
X_batch = []
for i in range(len(x_data)):
x = np.array(x_data[i].todense())
X_batch.append(x)
X_batch = np.array(X_batch)
return X_batch
def coo_to_tensor(x_data):
X_tf_ind_array = []
X_tf_val_array = []
for i in range(len(x_data)):
X_tf_ind = tf.SparseTensor(indices=np.column_stack((x_data[i].row, x_data[i].col)), values=x_data[i].col, dense_shape=x_data[i].shape)
X_tf_val = tf.SparseTensor(indices=np.column_stack((x_data[i].row, x_data[i].col)), values=x_data[i].data, dense_shape=x_data[i].shape)
X_tf_ind_array.append(X_tf_ind)
X_tf_val_array.append(X_tf_val)
return X_tf_ind_array, X_tf_val_array
def embedding_lookup_sparse(X_tf_ind_array, X_tf_val_array, V, W):
embedded_matrix = []
timeconsume = 0.00
for i in range(len(X_tf_ind_array)):
part_1=time.clock()
result = tf.nn.embedding_lookup_sparse(V, X_tf_ind_array[i], X_tf_val_array[i], combiner='sum')
result += W
part_2=time.clock()
timeconsume += part_2 - part_1
embedded_matrix.append(tf.reshape(result, [-1]))
embedded_matrix = np.array(embedded_matrix)
# print("\tFeature Reduction Time:" + str([round(float(timeconsume), 5)]))
# print("\tAvg. Feature Reduction Time:" + str([round(float(timeconsume)/len(X_tf_ind_array), 5)]))
return embedded_matrix, float(timeconsume)/len(X_tf_ind_array)
if __name__ == '__main__':
# GPU settings
gpus = tf.config.list_physical_devices("GPU")
if gpus:
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
# import pdb; pdb.set_trace()
# get the inference dataset
read_start = time.clock()
X_train_sparse, Y_train_data= generate_datasets()
read_end = time.clock()
read_timeconsume = read_end - read_start
read_time = float(read_timeconsume)/len(X_train_sparse)
# print("\tData Read Time:" + str([round(float(read_timeconsume), 5)]))
# print("\tAvg. Data Read Time:" + str([round(float(read_timeconsume)/len(X_train_sparse), 5)]))
autoencoder_path = "./"+ args.benchmark +"/autoencoder"
encoder = tf.keras.models.load_model(autoencoder_path)
tf.keras.utils.plot_model(
encoder, to_file=args.save_bayesian_path + 'autoencoder.png', show_shapes=False, show_layer_names=True,
rankdir='TB', expand_nested=False, dpi=96
)
# import pdb; pdb.set_trace()
# get the embedding kernel matrix and do feature reduction on sparse matrix
W = encoder.layers[0].get_weights()[1] #biases
V = encoder.layers[0].get_weights()[0] #wights
if (args.benchmark=='AMG'):
X_tf_ind_array, X_tf_val_array = coo_to_tensor(X_train_sparse)
X_encoding, fea_time = embedding_lookup_sparse(X_tf_ind_array, X_tf_val_array, V, W)
elif (args.benchmark=='CG'):
X_train_sparse = csr2dense(X_train_sparse)
read_start = time.clock()
X_encoding = np.array(np.dot(X_train_sparse,V)+W)
read_end = time.clock()
fea_timeconsume = read_end - read_start
fea_time = float(fea_timeconsume)/len(X_encoding)
elif (args.benchmark=='MG' or args.benchmark=='Lagos_fine' or args.benchmark=='Lagos_coarse'):
read_start = time.clock()
X_encoding = np.array(np.dot(X_train_sparse,V)+W)
read_end = time.clock()
fea_timeconsume = read_end - read_start
fea_time = float(fea_timeconsume)/len(X_encoding)
split_index = int(math.floor(len(X_encoding)* args.TRAIN_SET_RATIO / 100.0))
assert (split_index >= 0 and split_index <= len(X_encoding))
x_train = X_encoding[:split_index]
x_test = X_encoding[split_index:]
y_train = np.array(Y_train_data[:split_index])
if (args.benchmark=='CG'):
y_test = np.array(Y_train_data[split_index:args.sample_size])
else:
y_test = np.array(Y_train_data[split_index:])
#ml_loss, initial_history, final_history = Model_search(x_train, y_train, x_test, y_test)
# Autokeras searched model
model_path = "./"+ args.benchmark +"/best_model"
model = tf.keras.models.load_model(model_path)
tf.keras.utils.plot_model(
model, to_file=args.save_bayesian_path + 'model.png', show_shapes=False, show_layer_names=True,
rankdir='TB', expand_nested=False, dpi=96
)
model.summary()
#retrain the model
model.fit(x_train, y_train, epochs = 500, validation_data = (x_test, y_test), verbose=1)
# tf.profiler.experimental.start('logdir')
start = time.clock()
y_predict = model.predict(x_test)
# tf.profiler.experimental.stop()
end = time.clock()
timeconsume = end - start
# print("\tModel Inference Time:" + str([round(float(timeconsume), 5)]))
# print("\tAvg. Model Inference Time:" + str([round(float(timeconsume)/len(x_test), 5)]))
infer_time = float(timeconsume)/len(x_test)
loss = np.mean(np.abs(y_predict-y_test))
print(loss)
time_record = np.hstack([read_time, fea_time, infer_time])
# np.savetxt(args.save_bayesian_path + "x_test.txt", np.array(x_test), fmt='%.16f',
# delimiter=',')
np.savetxt(args.save_bayesian_path + "y_test.txt", np.array(y_test), fmt='%.16f',
delimiter=',')
np.savetxt(args.save_bayesian_path + "y_prediction.txt", np.array(y_predict), fmt='%.16f',
delimiter=',')
np.savetxt(args.save_bayesian_path + "time_record.txt", np.array(time_record), fmt='%.16f',
delimiter=',')
# print(y_predict)