Skip to content
Open
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
24 changes: 18 additions & 6 deletions experiments/dmrg.jl
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
using TensorTrains, TensorTrains.MatrixProductStates
using Random
using Plots

rng = Xoshiro(1)
d_original = 3
L = 5
q = rand_mps(ComplexF64, d_original, L, 4)
F = Float64
q = rand_mps(rng, F, d_original, L, 4)
normalize!(q)
nsamples = 5*10^3
X = [sample(q)[1] for _ in 1:nsamples]
X = [sample(rng, q)[1] for _ in 1:nsamples]
nll = -loglikelihood(q, X)
println("Negative Log-Likelihood according to generating distribution q = $nll\n")
mq = marginals(q)
pmq = twovar_marginals(q)

p = rand_mps(ComplexF64, 2, length(q), 4)
p = rand_mps(rng, F, 2, length(q), 4)

function CB()
nlls = zeros(0)
diff_marg = zeros(0)
diff_pair_marg = zeros(0)
dots = zeros(0)
function cb(sweep, k, p, nll)
p_cp = deepcopy(p)
Expand All @@ -24,26 +29,33 @@ function CB()
mbd = maximum(bond_dims(p.ψ))
mp = marginals(p)
d_m = maximum(maximum.(abs, mp-mq))
pmp = twovar_marginals(p)
d_pm = maximum(maximum.(abs, pmp-pmq))
println("# Sweep $sweep, site k=$k")
println("Negative LogLikelihood = $nll.\ndmax=$mbd")
println("|<p|q>|=$d")
println("Max diff marginals = $d_m\n")
println("Max diff marginals = $d_m")
println("Max diff pair marginals = $d_pm\n")
push!(nlls, -loglikelihood(p, X))
push!(diff_marg, d_m)
push!(diff_pair_marg, d_pm)
push!(dots, d)
end
end

callback = CB()
nsweeps = 40
normalize_gradient = true
two_site_dmrg!(p, X, nsweeps;
η=1e-4, ndesc=10, svd_trunc=TruncBond(d_original+2), callback)
normalize_gradient,
η=1e-4, ndesc=10, svd_trunc=TruncBond(d_original), callback)

pl1 = plot(callback.nlls, xlabel="it", ylabel="NLL", label="")
hline!(pl1, [nll], ls=:dash, c=:gray, label="NLL according to generative model")
# vline!(pl1, (N-1):(N-1):length(callback.nlls), label="Ends of sweeps", ls=:dash, c=:gray)
pl2 = plot(callback.diff_marg, xlabel="it", ylabel="Max error on marginals", label="")
# vline!(pl2, (N-1):(N-1):length(callback.nlls), label="Ends of sweeps", ls=:dash, c=:gray)
pl2b = plot(callback.diff_pair_marg, xlabel="it", ylabel="Max error on pair marginals", label="")
pl3 = plot(callback.dots, xlabel="it", ylabel="<p|q>", label="")
# vline!(pl3, (N-1):(N-1):length(callback.nlls), label="Ends of sweeps", ls=:dash, c=:gray)
plot(pl1, pl2, pl3, layout=(3,1), size=(500,800), margin=5Plots.mm)
plot(pl1, pl2, pl2b, pl3, layout=(4,1), size=(500,900), margin=5Plots.mm)
4 changes: 4 additions & 0 deletions src/dmrg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ function _two_site_dmrg_sweep_generic!(
η = 1e-3, # learning rate for gradient descent
ndesc = 100, # number of gradient descent steps
optimizer = Optim.Adam(; alpha=η),
normalize_gradient = false, # normalize gradient to go quicker on flat regions
callback = (sweep, k, p, loss_val) -> nothing,
prodA_left = [precompute_left_environments(p, x) for x in data[1]],
prodA_right = [precompute_right_environments(p, x) for x in data[1]],
Expand All @@ -87,6 +88,9 @@ function _two_site_dmrg_sweep_generic!(
)
if G !== nothing
G .= reshape(grad, length(G))
if normalize_gradient
G ./= norm(G)
end
end
if F !== nothing
return val
Expand Down
Loading