Skip to content

Commit

Permalink
add load tn from code (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArrogantGao authored Jan 30, 2024
1 parent 0ab5dbb commit c6cd869
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/Core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,21 @@ function TensorNetworkModel(
TensorNetworkModel(collect(LT, vars), code, tensors, evidence, mars)
end

"""
$(TYPEDSIGNATURES)
"""
function TensorNetworkModel(
model::UAIModel{T}, code;
evidence = Dict{Int,Int}(),
mars = [[i] for i=1:model.nvars],
vars = [1:model.nvars...]
)::TensorNetworkModel where{T}
@debug "constructing tensor network model from code"
tensors = Array{T}[[ones(T, [model.cards[i] for i in mar]...) for mar in mars]..., [t.vals for t in model.factors]...]

return TensorNetworkModel(vars, code, tensors, evidence, mars)
end

"""
$(TYPEDSIGNATURES)
Expand Down
12 changes: 12 additions & 0 deletions test/map.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ using Test
using OMEinsum
using TensorInference

@testset "load from code" begin
model = problem_from_artifact("uai2014", "MAR", "Promedus", 14)

tn1 = TensorNetworkModel(read_model(model);
evidence=read_evidence(model),
optimizer = TreeSA(ntrials = 3, niters = 2, βs = 1:0.1:80))

tn2 = TensorNetworkModel(read_model(model), tn1.code, evidence=read_evidence(model))

@test tn1.code == tn2.code
end

@testset "gradient-based tensor network solvers" begin
model = problem_from_artifact("uai2014", "MAR", "Promedus", 14)

Expand Down

0 comments on commit c6cd869

Please sign in to comment.