-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpruning.jl
178 lines (161 loc) · 4.73 KB
/
pruning.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
include("header.jl")
include("dataset.jl")
include("network.jl")
include("callback.jl")
# pyplot()
plot_loss(l_loss_train, l_loss_val; yscale = :log10)
function loss_neuralode_res(p)
l_loss_exp = zeros(n_exp)
for i_exp in 1:n_exp
exp_data = l_exp_data[i_exp]
pred = Array(pred_n_ode(p, i_exp, exp_data))
masslist = sum(clamp.(@view(pred[1:end-1, :]), 0, Inf), dims = 1)'
l_loss_exp[i_exp] = mae(masslist, @view(exp_data[1:length(masslist), 3]))
end
return l_loss_exp
end
println("results after pruning")
maxiters = 1e5
p_cutoff = 0.185
loss_epoch = zeros(Float64, n_exp);
for i_exp = 1:n_exp
loss_epoch[i_exp] = loss_neuralode(p, i_exp)
end
loss_train = mean(loss_epoch[l_train])
loss_val = mean(loss_epoch[l_val])
@printf(
"Loss train: %.2f val: %.2f p_cutoff: %.2e",
log10(loss_train),
log10(loss_val),
p_cutoff
)
display_p(p)
for i_exp in randperm(n_exp)
cbi(p, i_exp)
end
l_loss_exp = loss_neuralode_res(p)
l_exp_data = []
l_exp_info = zeros(Float64, length(l_exp), 3)
for (i_exp, value) in enumerate(l_exp)
filename = string("exp_data/expdata_no", string(value), ".txt")
exp_data = Float64.(load_exp(filename))
push!(l_exp_data, exp_data)
l_exp_info[i_exp, 1] = exp_data[1, 2] # initial temperature, K
end
l_exp_info[:, 2] = readdlm("exp_data/beta.txt")[l_exp]
l_exp_info[:, 3] = log.(readdlm("exp_data/ocen.txt")[l_exp] .+ llb)
l_loss_exp = loss_neuralode_res(p)
conc_end = zeros(n_exp, ns)
# Plot TGA data
list_plt = []
for i_exp = 1:14
T0, beta, ocen = l_exp_info[i_exp, :]
exp_data = l_exp_data[i_exp]
sol = pred_n_ode(p, i_exp, exp_data)
Tlist = similar(sol.t)
T0, beta, ocen = l_exp_info[i_exp, :]
for (i, t) in enumerate(sol.t)
Tlist[i] = getsampletemp(t, T0, beta)
end
conc_end[i_exp, :] .= sol[:, end]
if beta < 100
plt = plot(
Tlist,
exp_data[:, 3],
seriestype = :scatter,
label = "Exp-$i_exp",
framestyle = :box,
)
plot!(
plt,
Tlist,
sum(sol[1:end-1, :], dims = 1)',
lw = 2,
legend = false,
)
xlabel!(plt, "Temperature [K]")
else
plt = plot(
exp_data[:, 1] / 60.0,
exp_data[:, 3],
seriestype = :scatter,
label = "Exp-$i_exp",
framestyle = :box,
)
plot!(
plt,
sol.t / 60,
sum(sol[1:end-1, :], dims = 1)',
lw = 2,
legend = false,
)
xlabel!(plt, "Time [min]")
end
exp_cond = @sprintf("T0:%.0f K", T0)
if beta < 100.0
exp_cond *= @sprintf("\nbeta:%.0f K/min", beta)
end
if exp(ocen) * 100.0 > 0.001
exp_cond *= @sprintf("\n[O2]:%.2f", exp(ocen))
else
exp_cond *= @sprintf("\ninert")
end
ann_loc = [0.2, 0.3]
if i_exp in [6, 7, 14]
ann_loc = [0.7, 0.4]
end
annotate!(plt, xlims(plt)[1] + (xlims(plt)[end] - xlims(plt)[1]) * ann_loc[1],
ylims(plt)[1] + (ylims(plt)[end] - ylims(plt)[1]) * ann_loc[2],
text(exp_cond, 11))
ylabel!(plt, "Mass [-] (No. $i_exp)")
ylims!(plt, (0.0, 1.0))
plot!(
plt,
xtickfontsize = 11,
ytickfontsize = 11,
xguidefontsize = 12,
yguidefontsize = 12,
)
push!(list_plt, plt)
end
plt_all = plot(list_plt..., layout = (7, 2))
plot!(plt_all, size = (800, 1200))
png(plt_all, string(fig_path, "/TGA_mass_summary"))
# https://github.com/DENG-MIT/Biomass.jl/blob/main/backup/crnn_cellulose_ocen_test.jl
varnames = ["Cellu", "S2", "S3", "Vola"]
for i_exp in 1:n_exp
T0, beta, ocen = l_exp_info[i_exp, :]
exp_data = l_exp_data[i_exp]
sol = pred_n_ode(p, i_exp, exp_data)
Tlist = similar(sol.t)
T0, beta, ocen = l_exp_info[i_exp, :]
for (i, t) in enumerate(sol.t)
Tlist[i] = getsampletemp(t, T0, beta)
end
value = l_exp[i_exp]
list_plt = []
scale_factor = 1 ./ maximum(sol[:, :], dims=2)
scale_factor .= 1.0
plt = plot(Tlist, clamp.(sol[1, :], 0, Inf), label = varnames[1])
for i in 2:ns
if scale_factor[i] > 1.1
_label = @sprintf("%s x %.2e", varnames[i], scale_factor[i])
else
_label = varnames[i]
end
plot!(plt, Tlist, clamp.(sol[i, :], 0, Inf) * scale_factor[i], label = _label)
end
xlabel!(plt, "Temperature [K]");
ylabel!(plt, "Mass (-)");
plot!(plt, size=(350, 350), legend=:topleft, framestyle=:box)
plot!(
plt,
xtickfontsize = 11,
ytickfontsize = 11,
xguidefontsize = 12,
yguidefontsize = 12,
)
png(plt, string(fig_path, "/pred_S_exp_$value"))
end
include("dataset.jl")
gr()