kailaix / ADCME.jl

Automatic Differentiation Library for Computational and Mathematical Engineering
https://kailaix.github.io/ADCME.jl/latest/
MIT License
286 stars 57 forks source link

topological sort failed when modifying examples #61

Closed bzapf closed 3 years ago

bzapf commented 3 years ago

Hi, when trying to adjust your example for "Function Inverse Problem: Sparse Data" to the Poisson equation, I get messages like

2020-10-26 12:59:51.582303: E tensorflow/core/grappler/optimizers/dependency_optimizer.cc:697] Iteration = 0, topological sort failed with message: The graph couldn't be sorted in topological order.

I tried to recover f = pi^2 sin(pi*x) from u with -d^2/dx^2 u= f, which works reasonably well despite the error message.

So I'm wondering where I am using ADCME wrong, because these messages do not show up when I run the example.

Thanks for hints, Bastian

using LinearAlgebra
using ADCME
using PyPlot

n = 101 
h = 1/(n-1)
x = LinRange(0,1,n)|>collect

u = sin.(π*x);
f_gt = @. π^2*u;

function residual_and_jac(θ, uu)

    f_nn = squeeze(fc(reshape(x[1:end-2],:,1), [20,20,1], θ)) + 1.

    u_full = vector(2:n-1, uu, n)

    laplacian_u = -(u_full[3:end]+u_full[1:end-2]-2u_full[2:end-1])/h^2

    res =  laplacian_u - f_nn

    J = gradients(res, uu)

    res, J
end

θ = Variable(fc_init([1,20,20,1]))
ADCME.options.newton_raphson.rtol = 1e-4 # relative tolerance
ADCME.options.newton_raphson.tol = 1e-4 # absolute tolerance
ADCME.options.newton_raphson.verbose = false # print details in newton_raphson

u_est = newton_raphson_with_grad(residual_and_jac, constant(zeros(n-2)),θ)
residual = u_est[1:5:end] - u[2:end-1][1:5:end]
loss = sum(residual^2)

f = squeeze(fc(reshape(x,:,1), [20,20,1], θ)) + 1.0
sess = Session(); init(sess)
BFGS!(sess, loss)

figure(figsize=(10,4))
subplot(121)
plot(x, f_gt, label="Reference")
plot(x, run(sess, f), "o", markersize=5., label="Estimated")
legend(); xlabel("\$u\$"); ylabel("\$f(x)\$"); grid("on")
subplot(122)
plot(x, (@. sin(π*x)), label="Reference")
plot(x[2:end-1], run(sess, u_est), "--", label="Estimated")
legend(); xlabel("\$x\$"); ylabel("\$u\$"); grid("on")
kailaix commented 3 years ago

Hi Bastian, you can safely ignore the error because it's related to computational graph optimization (tensorflow/core/grappler/optimizers/dependency_optimizer.cc). You are using a Newton-Raphson solver, whose computational graph is more complex than typical ones, and the tensorflow graph optimizer cannot handle this yet.

bzapf commented 3 years ago

Great, thanks for the fast reply!