Radial basis function (RBF2D) from ADCME.jl returns MethodError

I am trying this tutorial for raidal basis function using ADCME.jl package.
When I execute the code, I get a method error:

using ADCME

# use centers on a uniform grid 
n = 5
h = 1/n
xc = Float64[] 
yc = Float64[]
for i = 1:n+1
    for j = 1:n+1
        push!(xc, (i-1)*h)
        push!(yc, (j-1)*h)
    end
end

# by default, c is initialized to Variable(ones(...))
# eps is initialized to ones(...) and no linear terms are used
rbf = RBF2D(xc, yc)

x = rand(100) 
y = rand(100)
f = @. 1+y^2/(1+x^2)

fv = rbf(x, y)
loss = sum((f-fv)^2)

sess = Session(); init(sess)
BFGS!(sess, loss)

Error observed for fv = rbf(x, y) line:

MethodError: no method matching load_op_and_grad(::Missing, ::String)
Closest candidates are:
  load_op_and_grad(!Matched::Union{String, PyCall.PyObject}, ::String; multiple, verbose) at C:\Users\user\.julia\packages\ADCME\7qIYe\src\extra.jl:160
(::RBF2D)(::Array{Float64,1}, ::Array{Float64,1}) at rbf.jl:64
top-level scope at test-functions.jl:23

Please suggest on the possible cause of this error and how may i resolve this issue?

Thanks in advance!!

Have you tried Surrogates.jl? That should be rather straighforward.

1 Like

@ChrisRackauckas , Thanks for reminding, highly appreciate it!!

I tried a tutorial using surrogates.jl, however my code is throwing an error, saying that in sample() sample not defined.

using Surrogates
using Plots
default()
g(x) = log(x)*x^2 + x^3
n_samples = 30
lower_bound = 5
upper_bound = 25
x = sample(n_samples, lower_bound, upper_bound, SobolSample())
y = g.(x)
scatter(x, y, label="Sampled Points", xlims=(lower_bound, upper_bound), legend=:top)
plot!(f, label="True function", scatter(x, y, label="Sampled Points", xlims=(lower_bound, upper_bound), legend=:top)

radial_surrogate = RadialBasis(x, y, lower_bound, upper_bound)
val = radial_surrogate(5.4)

Error

UndefVarError: sample not defined
top-level scope at test-functions.jl:39

May i know what is the cause of this error?

Thanks !!!

Are you on Julia v1.5 with Surrogates v1.6?

1 Like

Yes, I am using Julia 1.5.3. and Surrogates v1.6!

Odd… Surrogates.sample?

1 Like

what’s this Sample.jl?

Apologies, that is my mistake. I will fix that now.

Btw Surrogates.sample works!!!

@ChrisRackauckas Thanks !

I will run the test and post update on my implementation.

Is something throwing a warning saying that it’s shadowing sample when you do using?

It does not throw any warnings just an error saying that it is not define. The error result is presented in highlight:

UndefVarError: sample not defined
top-level scope at test-functions.jl:39

@ChrisRackauckas I figured out the issue, why was i getting the error for sample, it is due to the ADCME.jl package.
The ADCME.sample is creating a conflict with Surrogates.sample identifier. This I resolved it by removing the precompilation of the package.

I ran the tutorial for Surrogates and below is the example code:

using Surrogates
using Plots
default()
g(x) = log(x)*x^2 + x^3
n_samples = 30
lower_bound = 5
upper_bound = 25
x = sample(n_samples, lower_bound, upper_bound, SobolSample())
y = g.(x)

radial_surrogate = RadialBasis(x, y, lower_bound, upper_bound)

plot(x, y, seriestype=:scatter, label="Sampled points", xlims=(lower_bound, upper_bound), legend=:top)
plot!(g, label="True function",  xlims=(lower_bound, upper_bound), legend=:top)
plot!(radial_surrogate, label="Surrogate function",  xlims=(lower_bound, upper_bound), legend=:top)

I would like to know, how may I implement various functions for rbf like multiquadratic, gaussian etc?

What do you mean?

Apologies, for the pixelated snippet,

Reference for above: scipy.interpolate.Rbf — SciPy v1.6.1 Reference Guide

There are various functions which are used for operating rbf as listed in the documentation. however, this documentation is for scipy python. Do we have something like this which we can implement in Surrogates? I couldn’t find something similar for julia.

Yes, look at the tutorial and it shows how to interpolate the RBF: Basics · Surrogates.jl

1 Like

Thanks, I went through everything in documentation again and it is now much clearer now.
I highly appreciate your input and time.

I build the surrogate model for kriging function, here is my code:

Surrogate

using Surrogates
using Plots, DataFrames
default()

f(x) = sin(x)
n_samples = 10
lower_bound = 0.0
upper_bound = 9

x = sample(n_samples, lower_bound, upper_bound, SobolSample())
y = f.(x)

scatter(x, y, label="Sampled points")

kriging_surrogate = Surrogates.Kriging(x, y, lower_bound, upper_bound, p=1.9);

@show surrogate_optimize(f, SRBF(), lower_bound, upper_bound, kriging_surrogate, SobolSample())

result = []
arr = 0:1:10 

for i in arr
    interpolated = kriging_surrogate(i)
    push!(result, interpolated)
end

plot!(result, label="kriging", legend=:bottomright) #, title="Percentage Error", xlabel="Values", ylabel="Percentage")

Plot for the code:
image

I am quite confused why the julia model is not accurate as compared to the python?

Python Example

import numpy as np
from scipy.interpolate import Rbf, InterpolatedUnivariateSpline

import matplotlib
#matplotlib.use('Agg')
import matplotlib.pyplot as plt

# setup data
x = np.linspace(0, 10, 9)
y = np.sin(x)
xi = np.linspace(0, 10, 101)

# use RBF method
rbf = Rbf(x, y)
fi = rbf(xi)

plt.plot(x, y, 'bo')
plt.plot(xi, fi, 'g')
plt.plot(xi, np.sin(xi), 'r')
plt.title('Interpolation using RBF (scipy) - multiquadrics')
plt.tight_layout()
plt.show()

Plot for the python: image

May i know why is there offset in the datapoints?

Thanks in advance!

that looks good. Can you open an issue?

Thanks for the suggestion,
I have open an issue : Incorrect predicted values for kriging function · Issue #250 · SciML/Surrogates.jl (github.com)

Looking forward to positive response. :slight_smile: