Julia 使用Flux的神经代理问题

Julia 使用Flux的神经代理问题,julia,Julia,我正在尝试使用Flux.jl构建一个神经代理,目标是开发一个在估计插值方面与kriging函数相当的函数。 代码如下所示: using Plots using Surrogates using Flux f(x) = sin(x) x = range(0, 10.0, length = 9) x= Float32.(x) y = f.(x); scatter(x,y) model1 = Chain( Dense(1, 5, σ), Dense(5,2,σ), Dense(2, 1)

我正在尝试使用Flux.jl构建一个神经代理,目标是开发一个在估计插值方面与
kriging
函数相当的函数。 代码如下所示:

using Plots
using Surrogates
using Flux

f(x) = sin(x)
x = range(0, 10.0, length = 9)
x= Float32.(x)
y = f.(x);
scatter(x,y)
model1 = Chain(
  Dense(1, 5, σ),
  Dense(5,2,σ),
  Dense(2, 1)
)
neural = NeuralSurrogate(x, y, model = model1, n_echos = 5);

#surrogate_optimize(schaffer, SRBF(), lower_bound, upper_bound, neura, SobolSample(), maxiters=20, num_new_samples=10)
x_test = range(0, 10.0, length = 101)
x_test= Float32.(x_test)

res = [neural(i) for i in x_test]

plot!(x_test, result)

但是,我得到一个方法错误,如下所示:

MethodError: no method matching NeuralSurrogate(::Array{Float32,1}, ::Array{Float32,1}; model=Chain(Dense(1, 5, σ), Dense(5, 2, σ), Dense(2, 1)), n_echos=5)

我可以知道这个错误的原因吗?如何解决这个问题?

NeuralSurrogate构造函数需要一个上下限。Surrogates.jl文档中的示例说明了这一点,但如果您不确定,可以始终使用
方法
功能

julia> methods(NeuralSurrogate)
2 methods for type constructor:

    (::Type{NeuralSurrogate})(x, y, lb, ub; model, loss, opt, n_echos) in Surrogates at /srv/julia/pkg/packages/Surrogates/3CmhS/src/NeuralSurrogate.jl:24
    (::Type{NeuralSurrogate})(x::X, y::Y, model::M, loss::L, opt::O, ps::P, n_echos::N, lb::A, ub::U) where {X, Y, M, L, O, P, N, A, U} in Surrogates at /srv/julia/pkg/packages/Surrogates/3CmhS/src/NeuralSurrogate.jl:4
一个有效的例子:

using Plots
using Surrogates
using Flux

lb = 0.0
ub = 10.0
f(x) = sin(x)
x = range(lb, ub, length = 9)
x= Float32.(x)
y = f.(x);
scatter(x,y)
model1 = Chain(
  Dense(1, 5, σ),
  Dense(5,2,σ),
  Dense(2, 1)
)
neural = NeuralSurrogate(x, y, lb, ub, model = model1, n_echos = 5);

x_test = range(lb, ub, length = 101)
x_test= Float32.(x_test)

res = [neural(i) for i in x_test]

plot!(x_test, res)

这里的目标是复制使用
神经
网络的功能。你做过这个吗?我对输出有一些问题,似乎
结果
没有映射整个函数
f(x)
。我猜这是因为我从
通量函数->neural()
中得到的输出值。任何想法,我可以如何使它映射函数
f(x)
通过插值?当我运行大量的历元(如10000)时,我会得到我期望的平滑曲线,在训练数据中的点之间插值。5个时代不足以实现融合。在训练数据的范围之外,所有的赌注都是不可能的。你对结果不满意吗?谢谢,是的,你是对的,在增加了纪元的数量后,它正试图更好地适应。我还添加了一个额外的
优化函数
(取自文档),现在预测的结果非常棒!非常好的建议,非常感谢!