-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathone_dense_layer.py
97 lines (81 loc) · 3.21 KB
/
one_dense_layer.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
import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from DataPESQ import DataPESQ
import itertools as it
from cachetools import cached, TTLCache
import os
cache = TTLCache(maxsize=3000, ttl=9000)
test = False
load = False
path = 'data\PESQ_DB.xlsx'
init_momentum = 0.9
arhitecture = [30,30]
epochs = 100
def own_evaluate(model, test_data, test_labels, string):
test_labels_array = list(map(lambda x: [x], test_labels))
x = list(map(lambda x: x[0], test_data))
y = list(map(lambda x: x[1], test_data))
# print(test_labels_array)
true_results = np.concatenate((test_data, test_labels_array), axis=1)
prediction = model.predict(test_data)
prediction_vector = np.concatenate((prediction),axis=None)
model_results = np.concatenate((test_data, prediction), axis=1)
diff = test_labels - prediction_vector
# diff = list(map(lambda x: np.array(x), diff))
# diff_results = np.concatenate((test_data, diff), axis=1)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x, y, diff)
plt.show()
plt.savefig(string + '\\figure.png',bbox_inches='tight')
return
@cached(cache)
def split_data(path):
dataObj = DataPESQ(path)
data = dataObj.get_data()
np.random.shuffle(data)
sample_train = round(len(data) * 0.8)
sample_test = len(data) - sample_train
print(len(data), sample_train, sample_test)
training, test = data[sample_test:], data[:sample_test]
print(np.shape(training))
print(np.shape(test))
return training, test
def explore_models(list, trainig_data, training_labels, epochs):
model = keras.Sequential()
for x in list:
model.add(keras.layers.Dense(x,activation='relu'))
model.add(keras.layers.Dense(1))
model.compile(optimizer=tf.train.RMSPropOptimizer(0.01),
loss='mse',
metrics=['mse'])
model.fit(trainig_data, training_labels, epochs=epochs)
return model
def __main__():
print("Pre-processing data ...")
trainig, test = split_data(path)
trainig_labels = list(it.chain.from_iterable(list(map(lambda x: x[:1],trainig))))
trainig_data = list(map(lambda x: x[1:],trainig))
test_labels = list(it.chain.from_iterable(list(map(lambda x: x[:1],test))))
test_data = list(map(lambda x: x[1:],test))
trainig_data = np.array(trainig_data)
trainig_labels = np.array(trainig_labels)
test_data = np.array(test_data)
test_labels = np.array(test_labels)
if test:
model = explore_models(arhitecture, trainig_data, trainig_labels, epochs)
for i in [10,20]:
for j in [10,20]:
arhitecture = [i, j]
string = 'models\model' + '-' + str(i) + '-' + str(j)
os.makedirs(string)
model = explore_models(arhitecture, trainig_data, trainig_labels, epochs)
test_loss, test_acc = model.evaluate(test_data, test_labels)
print(test_acc)
print("Evaluating Model ... Done")
own_evaluate(model, test_data, test_labels, string)
model.save(string + '')
__main__()