Replies: 7 comments 8 replies
-
A quick working around solution is to disable the core mechanism when testing CRNN. |
Beta Was this translation helpful? Give feedback.
0 replies
-
Useful tips |
Beta Was this translation helpful? Give feedback.
0 replies
-
example reaction pathflux analysis |
Beta Was this translation helpful? Give feedback.
1 reply
-
Good hyper-parameters include("header.jl")
is_restart = true;
n_epoch = 100000;
ntotal = 20;
batch_size = 4;
n_plot = 5;
grad_max = 10.0^(1);
maxiters = 1000;
n_exp = 2;
nr = 3;
opt = ADAMW(0.01, (0.9, 0.999), 1.e-6);
atol = 1.e-8;
rtol = 1.e-3;
lb = atol;
ub = 1.0;
include("dataset.jl")
include("network.jl")
include("callback.jl")
# opt = ADAMW(5.e-3, (0.9, 0.999), 1.f-6)
epochs = ProgressBar(iter:n_epoch);
loss_epoch = zeros(Float64, n_exp);
grad_norm = zeros(Float64, n_exp);
for epoch in epochs
global p
for i_exp in randperm(n_exp)
sample = rand(batch_size:ntotal)
# _, ind = loss_n_ode(p, i_exp, sample; get_ind = true)
grad = ForwardDiff.gradient(x -> loss_n_ode(x, i_exp, sample), p)
grad_norm[i_exp] = norm(grad, 2)
if grad_norm[i_exp] > grad_max
grad = grad ./ grad_norm[i_exp] .* grad_max
end
update!(opt, p, grad)
end
for i_exp = 1:n_exp
loss_epoch[i_exp] = loss_n_ode(p, i_exp)
end
_loss = mean(loss_epoch)
_gnorm = mean(grad_norm)
set_description(
epochs,
string(
@sprintf("Loss %.4e grad %.2e lr %.2e", _loss, _gnorm, opt[1].eta)
),
)
cb(p, _loss, _gnorm)
end |
Beta Was this translation helpful? Give feedback.
2 replies
-
Working version @inbounds function dudt!(du, u, p, t)
Y = @view(u[1:ns])
T = u[end]
mean_MW = 1.0 / dot(Y, 1 ./ gas.MW)
ρ_mass = P / R / T * mean_MW
X = Y2X(gas, Y, mean_MW)
C = Y2C(gas, Y, ρ_mass)
cp_mole, cp_mass = get_cp(gas, T, X, mean_MW)
h_mole = get_H(gas, T, Y, X)
S0 = get_S(gas, T, P, X)
# _p = reshape(p, nr, 3)
# kp = @. @views(exp(_p[:, 1] + _p[:, 2] * log(T) - _p[:, 3] * 4184.0 / R / T))
kp = exp.(p)
qdot = wdot_func(gas.reaction, T, C, S0, h_mole; get_qdot = true) .* kp
qdot[[2,3,4,6,7]] .*= 0.0
wdot = gas.reaction.vk * qdot
Ydot = wdot / ρ_mass .* gas.MW
Tdot = -dot(h_mole, wdot) / ρ_mass / cp_mass
du .= vcat(Ydot, Tdot)
return du
end
@inbounds function dudt!(du, u, p, t)
w_in, w_b, w_out = p2vec(p)
Y = clamp.(@view(u[1:ns]), -1.e-3, 1.0)
T = clamp(u[end], 600.0, 3500.0) * ones(eltype(p), 1)[1]
mean_MW = 1.0 / dot(Y, 1 ./ gas.MW)
ρ_mass = P / R / T * mean_MW
X = Y2X(gas, Y, mean_MW)
C = Y2C(gas, Y, ρ_mass)
cp_mole, cp_mass = get_cp(gas, T, X, mean_MW)
h_mole = get_H(gas, T, Y, X)
S0 = get_S(gas, T, P, X)
qdot = wdot_func(gas.reaction, T, C, S0, h_mole; get_qdot=true)
qdot[2:7] .*= 0.0
wdot = gas.reaction.vk * qdot
crnn_in = vcat(log.(clamp.(@view(C[ind_crnn]), lb, 1.0)), -1.0 / T, log(T))
wdot[ind_crnn] .+= w_out * exp.(w_in' * crnn_in + w_b)
Ydot = wdot / ρ_mass .* gas.MW
Tdot = -dot(h_mole, wdot) / ρ_mass / cp_mass
du .= vcat(Ydot, Tdot)
return du
end
np = crnn_nr * (nse + 3) + 1;
p = randn(Float64, np) .* 0.1 .- 0.05;
p[1:crnn_nr] .+= 1.0; #lnA
p[crnn_nr * 2 + 1:crnn_nr * 3] .+= 0.0; # Ea
p[end] = 0.1; # slope
@inbounds function p2vec(p)
slope = p[end] .* 10.0
w_b = @view(p[1:crnn_nr]) .* slope
w_in_b = @view(p[crnn_nr + 1:crnn_nr * 2])
w_in_Ea = @view(p[crnn_nr * 2 + 1:crnn_nr * 3]) .* slope
w_out = E_null' *
reshape(@view(p[crnn_nr * 3 + 1:crnn_nr * (nse + 3)]),
nse, crnn_nr)
# w_out = reshape(@view(p[crnn_nr * 3 + 1:crnn_nr * (crnn_ns + 3)]),
# crnn_ns, crnn_nr)
# w_in = reshape(@view(p[crnn_nr * (crnn_ns + 3)+1:crnn_nr * (crnn_ns*2 + 3)]),
# crnn_ns, crnn_nr)
# w_out = @. -w_in * (10 ^ w_out)
w_in = vcat(clamp.(-w_out, 0.0, 2.5), w_in_Ea', w_in_b')
return w_in, w_b, w_out
end |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Note down my questions about modeling HyChem
Beta Was this translation helpful? Give feedback.
All reactions