-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPressurePredictor.jl
150 lines (142 loc) · 5.13 KB
/
PressurePredictor.jl
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
cd(@__DIR__)
using Pkg
Pkg.activate(".")
using Flux
using Plots
using Flux: @epochs, @show
using BSON: @save
using Random
using Statistics
#function for parsing data
function get_data(filename)
f1 = open(filename)
tempdata = readlines(f1)
tempdata[1] = tempdata[1][4:end] #cutting of beggining bc some weird chars
close(f1)
data = []
regvec = zeros(length(tempdata),
length(split(tempdata[1], ",")))
#parsing datavector to float array... not regularized
for i in 1:length(tempdata)
regvec[i,:] = parse.(Float64,split(tempdata[i], ","))
end
return regvec
end
#normalizing data
function norm_data(data, scalingmatrix=nothing)
#getting regularization values and normalizing data
regvec = data
normvec = zeros(length(regvec[:,1]),
length(regvec[1,:]))
#checking if optional scalingmatrix was passed, if not will calc mean and std
if scalingmatrix == nothing
scalingmatrix = zeros(length(regvec[1,:]),2)
for i in 1:length(regvec[1,:])
scalingmatrix[i,1] = Statistics.mean(regvec[:,i]) #first index is mean
scalingmatrix[i,2] = Statistics.std(regvec[:,i]) #second index is std
end
for i in 1:length(regvec[1,:])
#normalizing regvec
normvec[:,i] = (regvec[:,i] .- scalingmatrix[i,1]) ./ scalingmatrix[i,2]
end
else
#for case of normalizing test data by training std and mean
for i in 1:length(regvec[1,:])
#normalizing regvec
normvec[:,i] = (regvec[:,i] .- scalingmatrix[i,1]) ./ scalingmatrix[i,2]
end
end
return normvec, scalingmatrix
end
#function to convert vector of vector to matrix
function vecvec_to_matrix(vecvec)
dim1 = length(vecvec)
dim2 = length(vecvec[1])
my_array = zeros(dim1, dim2)
for i in 1:dim1
for j in 1:dim2
my_array[i,j] = vecvec[i][j]
end
end
return my_array
end
#getting training data
fname = "Dataset/FINAL_DATA.txt"
data_tmp = get_data(fname)
#seperating test and train data
train_data_tmp = []; test_data_tmp = [];
for i in 1:length(data_tmp[:,1])
if data_tmp[i,2] in [0.3,0.38,0.46,0.53,0.61,0.65] #training case
push!(train_data_tmp, data_tmp[i,:])
else #testing case
push!(test_data_tmp, data_tmp[i,:])
end
end
train_data = vecvec_to_matrix(train_data_tmp)
test_data = vecvec_to_matrix(test_data_tmp)
#normalizing data
train_norm, scalingmatrix = norm_data(train_data)
test_norm, scalingmatrix = norm_data(test_data,scalingmatrix)
#converting data to format which works with network
data = []
for i in 1:length(train_norm[:,1])
#adjust x and y for fault detection here
push!(data,(train_norm[i,[1,2,3]],train_norm[i,4]))
end
#network initialization function (allows for easier iterations)
function networkitr(data,Q,wd,iterations)
#model... must adjust if you want a different structure
itrmodel = Chain( Dense(3,Q), #σ(w1*x1 + w2*x2 + ...)
Dense(Q,Q,gelu),
Dense(Q,Q,gelu),
Dense(Q,Q,gelu),
Dense(Q,Q,gelu),
Dense(Q,Q,gelu),
Dense(Q,Q,gelu),
Dense(Q,1)) # 1 output connect to dim = Q H
opt = ADAM()
para = Flux.params(itrmodel) # variable to represent all of our weights and biases
l1(x) = sum(x .^ 2)
loss(x, y) = Flux.mse(itrmodel(x), y) + wd * sum(l1, para) # cost function
loss_wout_reg(x,y) = Flux.mse(itrmodel(x), y)
#=
for 500 iterations of our entire data set (@epochs 500) model is "trained"
Flux.train!( cost function, weights and biases, training data, optimizer setting)
=#
lowestmse = 1.0
for i in 1:iterations
trainset = Random.shuffle(data)[1:64]
Flux.train!(loss, para, trainset, opt)
Err = 0.0
for datapoint in data
Err += loss_wout_reg(datapoint[1],datapoint[2])
end
Err = Err/length(data)
#calculating lowestmse
if lowestmse > Err
global iterationbest = itrmodel #setting best model to lowestmse
lowestmse = Err
end
#print iteration data
print("\rError $lowestmse Itr $i/$iterations ")
end
return iterationbest, lowestmse
end
#iterating training diff networks
lowestmse_overall = 1.0
for q in [8,16,32]
for wd in [0,0.00001,0.000001,0.0000001]
#training netowrk iteration
print("\nTesting q=$q,wd=$wd\n")
global iteration_best, lowestmse_itr = networkitr(data,q,wd,2000)
if lowestmse_itr < lowestmse_overall
print("\nlowestmse = $lowestmse_itr < best overall = $lowestmse_overall\n")
print("Replacing Network with best network: ")
global best_string = "$q Nodes, $wd regularization parameter\n"
print(best_string)
global overall_best = iteration_best
global lowestmse_overall = lowestmse_itr
end
end
end
print("best networkw was: ", best_string, "with Error $lowestmse_overall")