Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
odunbar committed Oct 9, 2024
1 parent f340845 commit 161f7e3
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 83 deletions.
12 changes: 10 additions & 2 deletions examples/Learn_hyperparameters/1d_to_1d_regression_direct.jl
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ function estimate_mean_and_coeffnorm_covariance(
means[:, i] += m' / repeats
coeffl2norm[1, i] += sqrt(sum(c .^ 2)) / repeats
complexity[1, i] += cplx / repeats

end
end

Expand Down Expand Up @@ -224,7 +224,15 @@ for (idx, type) in enumerate(feature_types)
N_iter = 20
initial_params = construct_initial_ensemble(rng, priors, N_ens)
data = vcat(y[(n_train + 1):end], 0.0, 0.0)
ekiobj = [EKP.EnsembleKalmanProcess(initial_params, data, Γ, Inversion(), scheduler=DataMisfitController(terminate_at=1000))]
ekiobj = [
EKP.EnsembleKalmanProcess(
initial_params,
data,
Γ,
Inversion(),
scheduler = DataMisfitController(terminate_at = 1000),
),
]


err = zeros(N_iter)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ function estimate_mean_and_coeffnorm_covariance(
means[:, i] += m' / repeats
coeffl2norm[1, i] += sqrt(sum(c .^ 2)) / repeats
complexity[1, i] += cplx / repeats

end
end

Expand Down Expand Up @@ -218,7 +218,15 @@ for (idx, type) in enumerate(feature_types)
N_iter = 20
initial_params = construct_initial_ensemble(rng, priors, N_ens)
data = vcat(y, 0.0, 0.0)
ekiobj = [EKP.EnsembleKalmanProcess(initial_params, data, Γ, Inversion(), scheduler=EKP.DataMisfitController(terminate_at=100))]
ekiobj = [
EKP.EnsembleKalmanProcess(
initial_params,
data,
Γ,
Inversion(),
scheduler = EKP.DataMisfitController(terminate_at = 100),
),
]


err = zeros(N_iter)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,15 @@ for (idx, type) in enumerate(feature_types)
N_iter = 20
initial_params = construct_initial_ensemble(rng, priors, N_ens)
data = vcat(y[(n_train + 1):end], 0.0)
ekiobj = [EKP.EnsembleKalmanProcess(initial_params, data, Γ, Inversion(), scheduler = EKP.DataMisfitController(terminate_at=100))]
ekiobj = [
EKP.EnsembleKalmanProcess(
initial_params,
data,
Γ,
Inversion(),
scheduler = EKP.DataMisfitController(terminate_at = 100),
),
]


err = zeros(N_iter)
Expand Down
11 changes: 2 additions & 9 deletions examples/Learn_hyperparameters/nd_to_1d_regression_direct.jl
Original file line number Diff line number Diff line change
Expand Up @@ -272,15 +272,8 @@ for i in 1:N_iter

#get parameters:
lvec = transform_unconstrained_to_constrained(priors, get_u_final(ekiobj[1]))
g_ens = calculate_ensemble_mean_and_coeffnorm(
rng,
lvec,
noise_sd,
n_features,
batch_sizes,
io_pairs,
repeats = repeats,
)
g_ens =
calculate_ensemble_mean_and_coeffnorm(rng, lvec, noise_sd, n_features, batch_sizes, io_pairs, repeats = repeats)

if i % update_cov_step == 0 # one update to the

Expand Down
107 changes: 53 additions & 54 deletions examples/Learn_hyperparameters/nd_to_1d_regression_direct_crossval.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function calculate_mean_and_coeffs(
noise_sd::Real,
n_features::Int,
train_idx,
test_idx,
test_idx,
batch_sizes::Dict,
io_pairs::PairedDataContainer,
)
Expand Down Expand Up @@ -116,7 +116,7 @@ function estimate_mean_and_coeffnorm_covariance(
noise_sd::Real,
n_features::Int,
train_idx,
test_idx,
test_idx,
batch_sizes::Dict,
io_pairs::PairedDataContainer,
n_samples::Int;
Expand All @@ -128,7 +128,8 @@ function estimate_mean_and_coeffnorm_covariance(
complexity = zeros(1, n_samples)
for i in 1:n_samples
for j in 1:repeats
m, c, cplxty = calculate_mean_and_coeffs(rng, l, noise_sd, n_features, train_idx, test_idx, batch_sizes, io_pairs)
m, c, cplxty =
calculate_mean_and_coeffs(rng, l, noise_sd, n_features, train_idx, test_idx, batch_sizes, io_pairs)
means[:, i] += m[1, :] / repeats
coeffl2norm[1, i] += sqrt(sum(c .^ 2)) / repeats
complexity[1, i] += cplxty / repeats
Expand All @@ -145,7 +146,7 @@ function calculate_ensemble_mean_and_coeffnorm(
noise_sd::Real,
n_features::Int,
train_idx,
test_idx,
test_idx,
batch_sizes::Dict,
io_pairs::PairedDataContainer;
repeats::Int = 1,
Expand All @@ -163,7 +164,8 @@ function calculate_ensemble_mean_and_coeffnorm(
for i in collect(1:N_ens)
for j in collect(1:repeats)
l = lmat[:, i]
m, c, cplxty = calculate_mean_and_coeffs(rng, l, noise_sd, n_features, train_idx, test_idx, batch_sizes, io_pairs)
m, c, cplxty =
calculate_mean_and_coeffs(rng, l, noise_sd, n_features, train_idx, test_idx, batch_sizes, io_pairs)
means[:, i] += m[1, :] / repeats
coeffl2norm[1, i] += sqrt(sum(c .^ 2)) / repeats
complexity[1, i] += cplxty / repeats
Expand Down Expand Up @@ -207,14 +209,18 @@ n_train = Int(floor(0.8 * n_data))
n_test = n_data - n_train
n_cross_val = 3
@info "number of cross-validation sets: $(n_cross_val)"
if n_test*n_cross_val > n_data
throw(ArgumentError("train/test split produces cross validation test sets of size $(n_test), out of $(n_data). set \"n_cross_val\" < $(Int(floor(n_data/n_test))). Received $n_cross_val"))
if n_test * n_cross_val > n_data
throw(
ArgumentError(
"train/test split produces cross validation test sets of size $(n_test), out of $(n_data). set \"n_cross_val\" < $(Int(floor(n_data/n_test))). Received $n_cross_val",
),
)
end
train_idx = []
test_idx = []
idx_shuffle = randperm(rng, n_data)
for i = 1:n_cross_val
tmp = idx_shuffle[(i-1)*n_test+1:i*n_test]
for i in 1:n_cross_val
tmp = idx_shuffle[((i - 1) * n_test + 1):(i * n_test)]
push!(test_idx, tmp)
push!(train_idx, setdiff(collect(1:n_data), tmp))
end
Expand All @@ -229,7 +235,7 @@ if CALC_TRUTH
n_samples = Int(floor(((1 + n_test) + 1) * sample_multiplier))

observation_vec = []
for cv_idx = 1:n_cross_val
for cv_idx in 1:n_cross_val
println("Estimating output covariance with ", n_samples, " samples")
internal_Γ = estimate_mean_and_coeffnorm_covariance(
rng,
Expand All @@ -249,14 +255,7 @@ if CALC_TRUTH

data = vcat(get_outputs(io_pairs)[test_idx[cv_idx]], 0.0, 0.0)

push!(observation_vec, EKP.Observation(
Dict(
"names" => "$(cv_idx)",
"samples" => data[:],
"covariances" => Γ,
),
),
)
push!(observation_vec, EKP.Observation(Dict("names" => "$(cv_idx)", "samples" => data[:], "covariances" => Γ)))
end
observation = combine_observations(observation_vec)

Expand Down Expand Up @@ -292,8 +291,8 @@ for i in 1:N_iter
#get parameters:
lvec = transform_unconstrained_to_constrained(priors, get_u_final(ekiobj[1]))

g_ens = zeros(n_cross_val*(n_test+2),N_ens)
for cv_idx = 1:n_cross_val
g_ens = zeros(n_cross_val * (n_test + 2), N_ens)
for cv_idx in 1:n_cross_val
g_ens_tmp = calculate_ensemble_mean_and_coeffnorm(
rng,
lvec,
Expand All @@ -304,43 +303,43 @@ for i in 1:N_iter
batch_sizes,
io_pairs,
)
g_ens[(cv_idx-1)*(n_test+2) + 1: cv_idx*(n_test+2), :] = g_ens_tmp
g_ens[((cv_idx - 1) * (n_test + 2) + 1):(cv_idx * (n_test + 2)), :] = g_ens_tmp
end

#=
if i % update_cov_step == 0 # one update to the

constrained_u = transform_unconstrained_to_constrained(priors, get_u_final(ekiobj[1]))
println("Estimating output covariance with ", n_samples, " samples")
internal_Γ_new = estimate_mean_and_coeffnorm_covariance(
rng,
mean(constrained_u, dims = 2)[:, 1], # take mean values
noise_sd,
n_features,
batch_sizes,
io_pairs,
n_samples,
)
Γ_new = internal_Γ_new
Γ_new[1:n_test, 1:n_test] += regularizer * I
Γ_new[(n_test + 1):end, (n_test + 1):end] += I
println(
"Estimated covariance. Tr(cov) = ",
tr(Γ_new[1:n_test, 1:n_test]),
" + ",
tr(Γ_new[(n_test + 1):end, (n_test + 1):end]),
)
#=
if i % update_cov_step == 0 # one update to the
constrained_u = transform_unconstrained_to_constrained(priors, get_u_final(ekiobj[1]))
println("Estimating output covariance with ", n_samples, " samples")
internal_Γ_new = estimate_mean_and_coeffnorm_covariance(
rng,
mean(constrained_u, dims = 2)[:, 1], # take mean values
noise_sd,
n_features,
batch_sizes,
io_pairs,
n_samples,
)
Γ_new = internal_Γ_new
Γ_new[1:n_test, 1:n_test] += regularizer * I
Γ_new[(n_test + 1):end, (n_test + 1):end] += I
println(
"Estimated covariance. Tr(cov) = ",
tr(Γ_new[1:n_test, 1:n_test]),
" + ",
tr(Γ_new[(n_test + 1):end, (n_test + 1):end]),
)
ekiobj[1] = EKP.EnsembleKalmanProcess(
get_u_final(ekiobj[1]),
data,
Γ_new,
Inversion(),
localization_method = loc_method,
)
ekiobj[1] = EKP.EnsembleKalmanProcess(
get_u_final(ekiobj[1]),
data,
Γ_new,
Inversion(),
localization_method = loc_method,
)
end
=#
end
=#
terminated = EKP.update_ensemble!(ekiobj[1], g_ens)
if !isnothing(terminated)
break
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,15 +265,8 @@ for i in 1:N_iter

#get parameters:
lvec = transform_unconstrained_to_constrained(priors, get_u_final(ekiobj[1]))
g_ens = calculate_ensemble_mean_and_coeffnorm(
rng,
lvec,
noise_sd,
n_features,
batch_sizes,
io_pairs,
repeats = repeats,
)
g_ens =
calculate_ensemble_mean_and_coeffnorm(rng, lvec, noise_sd, n_features, batch_sizes, io_pairs, repeats = repeats)

if i % update_cov_step == 0 # one update to the

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,10 @@ function calculate_mean_cov_and_coeffs(
io_train_cost = PairedDataContainer(itrain, otrain)
itest = get_inputs(io_pairs)[:, (n_train + 1):end]
otest = get_outputs(io_pairs)[:, (n_train + 1):end]

else
n_train = Int(size(get_inputs(io_pairs),2))

n_train = Int(size(get_inputs(io_pairs), 2))
n_test = n_train
# split data into train/test randomly
itrain = get_inputs(io_pairs)[:, 1:n_train]
Expand Down Expand Up @@ -226,7 +226,7 @@ function estimate_mean_and_coeffnorm_covariance(
n_train = Int(floor(0.8 * size(get_inputs(io_pairs), 2))) # 80:20 train test
n_test = size(get_inputs(io_pairs), 2) - n_train
else
n_train = Int( size(get_inputs(io_pairs), 2))
n_train = Int(size(get_inputs(io_pairs), 2))
n_test = n_train
end
output_dim = size(get_outputs(io_pairs), 1)
Expand Down Expand Up @@ -310,7 +310,7 @@ function calculate_ensemble_mean_and_coeffnorm(
n_train = Int(floor(0.8 * size(get_inputs(io_pairs), 2))) # 80:20 train test
n_test = size(get_inputs(io_pairs), 2) - n_train
else
n_train = Int( size(get_inputs(io_pairs), 2))
n_train = Int(size(get_inputs(io_pairs), 2))
n_test = n_train
end

Expand Down
2 changes: 1 addition & 1 deletion test/Methods/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ tol = 1e3 * eps()
) #don't invert, just make PD
reg_new = get_regularization(rfm_warn2)
@test isposdef(reg_new)
@test minimum(eigvals(reg_new)) > (1e12-1) * eps()
@test minimum(eigvals(reg_new)) > (1e12 - 1) * eps()

rfm = RandomFeatureMethod(
sff,
Expand Down

0 comments on commit 161f7e3

Please sign in to comment.