Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 62 additions & 29 deletions src/fit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ function fit!(model::AbstractModel, Rs::Union{Vector{PState{T}},Vector{Vector{PS
for (j,R) in enumerate(Rs)
valset = partial_md(R,ps,st)[1]
for (i, (l1,l2)) in enumerate(LLset)
A_all[i][(2l1+1)*(2l2+1)*(j-1)+1:(2l1+1)*(2l2+1)*j,:] = flat(valset[i])
first_row = (2l1 + 1) * (2l2 + 1) * (j - 1) + 1
flat!(A_all[i], first_row, valset[i])
end
end

Expand All @@ -48,39 +49,60 @@ function fit!(model::AbstractModel, Rs::Union{Vector{PState{T}},Vector{Vector{PS
println("Fitting the $(Dict_Int2Orbs[l1])$(Dict_Int2Orbs[l2]) blocks ...")
println()

RMSE = 0

# get the design matrix A for the (l1,l2) block, and delete the corresponding part in A_all
A = popat!(A_all, 1)
num = size(A)[2] # number of basis

A = [A; λ*Γ[i]]

for kk = 1 : size(model.ps.dot[i].W,1)
ii, jj = k2ij(kk, n_orbs1[l1+1], n_orbs2[l2+1])
# println("Fitting the ($ii,$jj)-th $(Dict_Int2Orbs[l1])$(Dict_Int2Orbs[l2]) block ...")

Yij = [ get_Y(Ys[t], n_orbs1, n_orbs2, l1, l2, ii, jj) for t = 1:length(Ys) ]

# construct Y
Y = zeros(Float64, length(Ys)*length(Yij[1]))
for k in 1:length(Ys)
Y[(k-1)*length(Yij[1])+1:k*length(Yij[1])] = Yij[k]
output_count = size(model.ps.dot[i].W, 1)
block_size = (2l1 + 1) * (2l2 + 1)
data_rows = length(Ys) * block_size
if solver isa ACEfit.QR && iszero(solver.lambda)
Y = zeros(Float64, data_rows + num, output_count)
for kk in 1:output_count
ii, jj = k2ij(kk, n_orbs1[l1+1], n_orbs2[l2+1])
for sample in eachindex(Ys)
row_start = (sample - 1) * block_size + 1
rows = row_start:(row_start + block_size - 1)
Y[rows, kk] .= vec(get_Y(
Ys[sample], n_orbs1, n_orbs2,
l1, l2, ii, jj,
))
end
end
Y = [Y; zeros(num)]

# solve for C[kk]
C = ACEfit.solve(solver, A, Y)["C"];
@set! model.ps.dot.$(layer_set[i]).W[kk,:] = C
# list of potential solvers: ACEfit: QR, LSQR, RRQR, SKLEARN_BRR, SKLEARN_ARD, BLR, TruncatedSVD...

RMSE += norm((A*C-Y)[1:end-num])^2/(length(Y)-num)
# Solve all orbital pairs with one QR factorization and reuse the
# disposable augmented matrix as the factorization workspace.
A_data = copy(view(A, 1:data_rows, :))
C = qr!(A) \ Y
residual = A_data * C - view(Y, 1:data_rows, :)
model.ps.dot[i].W .= transpose(C)
RMSE = sqrt(sum(abs2, residual) / (data_rows * output_count))
else
# Preserve the established one-right-hand-side interface for
# solvers that do not support matrix-valued targets.
mean_squared_error = 0.0
for kk in 1:output_count
ii, jj = k2ij(kk, n_orbs1[l1+1], n_orbs2[l2+1])
Y = zeros(Float64, data_rows + num)
for sample in eachindex(Ys)
row_start = (sample - 1) * block_size + 1
rows = row_start:(row_start + block_size - 1)
Y[rows] .= vec(get_Y(
Ys[sample], n_orbs1, n_orbs2,
l1, l2, ii, jj,
))
end
C = ACEfit.solve(solver, A, Y)["C"]
@set! model.ps.dot.$(layer_set[i]).W[kk, :] = C
mean_squared_error +=
norm((A * C - Y)[1:data_rows])^2 / data_rows
end
RMSE = sqrt(mean_squared_error / output_count)
GC.gc()
end
# println("RMSE = $(sqrt(RMSE/length(LLset)))")
println("RMSE = $(sqrt(RMSE/size(model.ps.dot[i].W,1)))")
println("RMSE = $RMSE")
println()

GC.gc()
end

model = (TP <: On_Model) ? TP(model.model, model.ps, model.st, model.n_orbs, true) : TP(model.model, model.ps, model.st, model.n_orbs1, model.n_orbs2, true)
Expand Down Expand Up @@ -124,7 +146,8 @@ function fit!(model::AbstractModel, Rs::Union{Vector{PState{T}},Vector{Vector{PS
for (j,R) in enumerate(Rs)
valset = partial_md(R,ps,st)[1]
for (i, (l1,l2)) in enumerate(LLset)
A_all[i][(2l1+1)*(2l2+1)*(j-1)+1:(2l1+1)*(2l2+1)*j,:] = flat(valset[i])
first_row = (2l1 + 1) * (2l2 + 1) * (j - 1) + 1
flat!(A_all[i], first_row, valset[i])
end
end
println("Finish constructing A")
Expand Down Expand Up @@ -215,7 +238,12 @@ function fit!(model::Density_Model, Rs, Ys; solver = ACEfit.SKLEARN_BRR(), λ =
if length(Rs[key]) == 0 || length(Ys[key]) == 0
continue
end
model.Models[key] = fit!(model.Models[key], identity.(pop!(Rs,key)), assemble_Y( identity.(pop!(Ys,key)), get_norbs(model.Models[key])...); solver = solver, λ = λ, reg = reg, GC_switcher = GC_switcher)
states = identity.(pop!(Rs, key))
targets = identity.(pop!(Ys, key))
model.Models[key] = fit!(
model.Models[key], states, targets;
solver=solver, λ=λ, reg=reg,
)
end
else
for key in keys(model.Models)
Expand All @@ -224,7 +252,12 @@ function fit!(model::Density_Model, Rs, Ys; solver = ACEfit.SKLEARN_BRR(), λ =
if length(Rs[key]) == 0 || length(Ys[key]) == 0
continue
end
model.Models[key] = fit!(model.Models[key], identity.(pop!(Rs,key)), assemble_Y( identity.(pop!(Ys,key)), get_norbs(model.Models[key])...); solver = solver, λ = λ, reg = reg, GC_switcher = GC_switcher)
states = identity.(pop!(Rs, key))
targets = identity.(pop!(Ys, key))
model.Models[key] = fit!(
model.Models[key], states, targets;
solver=solver, λ=λ, reg=reg,
)
end
end
if !isfitted(model)
Expand Down Expand Up @@ -358,4 +391,4 @@ end
# return DM, train_set
# end

end # module
end # module
25 changes: 24 additions & 1 deletion src/utils/transformations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ using RepLieGroups.O3: ClebschGordan, wigner_D_indices, adjoint, Rot3DCoeffs, di
import RepLieGroups.O3: _mrange
import EquivariantModels: coco_dot

export eigen_retraction, svd_retraction, flat, k2ij
export eigen_retraction, svd_retraction, flat, flat!, k2ij

# The transformation matrix from complex SHs to real SHs
function ctran(L)
Expand Down Expand Up @@ -39,6 +39,29 @@ function flat(a)
return tmp
end

function flat!(destination::Matrix, first_row::Int, values)
size(destination, 2) == length(values) || throw(DimensionMismatch(
"the destination and values must have the same number of columns",
))
isempty(values) && return destination

value_length = length(first(values))
first_row >= 1 || throw(BoundsError(destination, first_row))
first_row + value_length - 1 <= size(destination, 1) ||
throw(BoundsError(destination, first_row:(first_row + value_length - 1)))

destination_rows = size(destination, 1)
@inbounds for column in eachindex(values)
value = values[column]
length(value) == value_length || throw(DimensionMismatch(
"all values must have the same flattened length",
))
destination_offset = first_row + (column - 1) * destination_rows
copyto!(destination, destination_offset, value, 1, value_length)
end
return destination
end

function k2ij(k, n, m)
i = div(k-1, m) + 1
j = k - (i-1)*m
Expand Down
39 changes: 39 additions & 0 deletions test/test_transformations.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
module TestCouplingTransformations

using ACEdensitymatrix
using ACEfit
using LinearAlgebra
using Random
using SparseArrays
using Test

Expand Down Expand Up @@ -30,6 +32,43 @@ const ADM = ACEdensitymatrix
end
end
end

@testset "allocation-free feature assembly" begin
values = [
[1.0 2.0; 3.0 4.0],
[-1.0 0.5; 0.25 2.0],
]
reference = ADM.flat(values)
destination = fill(NaN, size(reference, 1) + 2, size(reference, 2))

ADM.flat!(destination, 2, values)

@test destination[2:(end - 1), :] == reference
@test all(isnan, destination[[1, end], :])
@test_throws DimensionMismatch ADM.flat!(
zeros(size(reference, 1), size(reference, 2) + 1), 1, values,
)
end

@testset "batched QR matches repeated solves" begin
rng = MersenneTwister(1234)
data_matrix = randn(rng, 18, 6)
regularization = 1e-4 * Matrix{Float64}(I, 6, 6)
design_matrix = vcat(data_matrix, regularization)
data_targets = randn(rng, 18, 5)
targets = vcat(data_targets, zeros(6, 5))

batched = qr!(copy(design_matrix)) \ targets
repeated = hcat([
ACEfit.solve(
ACEfit.QR(), design_matrix, targets[:, column],
)["C"]
for column in axes(targets, 2)
]...)

@test batched ≈ repeated rtol=1e-12 atol=1e-12
@test data_matrix * batched ≈ data_matrix * repeated
end
end

end
Loading