Skip to content

Commit c6cd869

Browse files
authored
add load tn from code (#90)
1 parent 0ab5dbb commit c6cd869

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/Core.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,21 @@ function TensorNetworkModel(
175175
TensorNetworkModel(collect(LT, vars), code, tensors, evidence, mars)
176176
end
177177

178+
"""
179+
$(TYPEDSIGNATURES)
180+
"""
181+
function TensorNetworkModel(
182+
model::UAIModel{T}, code;
183+
evidence = Dict{Int,Int}(),
184+
mars = [[i] for i=1:model.nvars],
185+
vars = [1:model.nvars...]
186+
)::TensorNetworkModel where{T}
187+
@debug "constructing tensor network model from code"
188+
tensors = Array{T}[[ones(T, [model.cards[i] for i in mar]...) for mar in mars]..., [t.vals for t in model.factors]...]
189+
190+
return TensorNetworkModel(vars, code, tensors, evidence, mars)
191+
end
192+
178193
"""
179194
$(TYPEDSIGNATURES)
180195

test/map.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@ using Test
22
using OMEinsum
33
using TensorInference
44

5+
@testset "load from code" begin
6+
model = problem_from_artifact("uai2014", "MAR", "Promedus", 14)
7+
8+
tn1 = TensorNetworkModel(read_model(model);
9+
evidence=read_evidence(model),
10+
optimizer = TreeSA(ntrials = 3, niters = 2, βs = 1:0.1:80))
11+
12+
tn2 = TensorNetworkModel(read_model(model), tn1.code, evidence=read_evidence(model))
13+
14+
@test tn1.code == tn2.code
15+
end
16+
517
@testset "gradient-based tensor network solvers" begin
618
model = problem_from_artifact("uai2014", "MAR", "Promedus", 14)
719

0 commit comments

Comments
 (0)