Skip to content

Commit

Permalink
kwargs neural
Browse files Browse the repository at this point in the history
  • Loading branch information
ludoro committed May 19, 2020
1 parent 003b0c0 commit 7325b0e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 29 deletions.
3 changes: 1 addition & 2 deletions src/NeuralSurrogate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ mutable struct NeuralSurrogate{X,Y,M,L,O,P,N,A,U} <: AbstractSurrogate
- opt: optimization function
"""
function NeuralSurrogate(x,y,lb,ub,model,loss,opt,n_echos)
function NeuralSurrogate(x,y,lb,ub; model = Chain(Dense(length(x[1]),1), first), loss = (x,y) -> Flux.mse(model(x), y),opt = Descent(0.01),n_echos::Int = 1)
X = vec.(collect.(x))
data = zip(X, y)
ps = Flux.params(model)
@epochs n_echos Flux.train!(loss, ps, data, opt)
return NeuralSurrogate(x,y,model,loss,opt,ps,n_echos,lb,ub)

end

function (my_neural::NeuralSurrogate)(val)
Expand Down
24 changes: 12 additions & 12 deletions test/AD_compatibility.jl
Original file line number Diff line number Diff line change
Expand Up @@ -227,11 +227,11 @@ g = x -> my_loba'(x)
g(0.0)

#NN
model = Chain(Dense(1,1), first)
loss(x, y) = Flux.mse(model(x), y)
opt = Descent(0.01)
my_model = Chain(Dense(1,1), first)
my_loss(x, y) = Flux.mse(my_model(x), y)
my_opt = Descent(0.01)
n_echos = 1
my_neural = NeuralSurrogate(x,y,lb,ub,model,loss,opt,n_echos)
my_neural = NeuralSurrogate(x,y,lb,ub,model=my_model,loss=my_loss,opt=my_opt,n_echos=1)
g = x->my_neural'(x)
g(3.4)

Expand Down Expand Up @@ -283,11 +283,11 @@ g = x -> Zygote.gradient(my_second,x)
g((2.0,5.0))

#NN
model = Chain(Dense(2,1), first)
loss(x, y) = Flux.mse(model(x), y)
opt = Descent(0.01)
my_model = Chain(Dense(2,1), first)
my_loss(x, y) = Flux.mse(my_model(x), y)
my_opt = Descent(0.01)
n_echos = 1
my_neural = NeuralSurrogate(x,y,lb,ub,model,loss,opt,n_echos)
my_neural = NeuralSurrogate(x,y,lb,ub,model=my_model,loss=my_loss,opt=my_opt,n_echos=1)
g = x -> Zygote.gradient(my_neural, x)
g((2.0,5.0))

Expand All @@ -301,11 +301,11 @@ f = x -> [x[1]^2, x[2]]
y = f.(x)

#NN
model = Chain(Dense(2,2))
loss(x, y) = Flux.mse(model(x), y)
opt = Descent(0.01)
my_model = Chain(Dense(2,2))
my_loss(x, y) = Flux.mse(my_model(x), y)
my_opt = Descent(0.01)
n_echos = 1
my_neural = NeuralSurrogate(x,y,lb,ub,model,loss,opt,n_echos)
my_neural = NeuralSurrogate(x,y,lb,ub,model=my_model,loss=my_loss,opt=my_opt,n_echos=1)
Zygote.gradient(x -> sum(my_neural(x)), (2.0, 5.0))

my_rad = RadialBasis(x,y,lb,ub,rad = linearRadial)
Expand Down
32 changes: 18 additions & 14 deletions test/neuralSurrogate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ b = 10.0
obj_1D = x -> 2*x+3
x = sample(10,0.0,10.,SobolSample())
y = obj_1D.(x);
model = Chain(Dense(1,1), first)
loss(x, y) = Flux.mse(model(x), y)
opt = Descent(0.01)
my_model = Chain(Dense(1,1), first)
my_loss(x, y) = Flux.mse(my_model(x), y)
my_opt = Descent(0.01)
n_echos = 1
my_neural = NeuralSurrogate(x,y,a,b,model,loss,opt,n_echos)
my_neural = NeuralSurrogate(x,y,a,b,model = my_model,loss = my_loss,opt = my_opt,n_echos=1)
my_neural_kwargs = NeuralSurrogate(x,y,a,b)
add_point!(my_neural,8.5,20.0)
add_point!(my_neural,[3.2,3.5],[7.4,8.0])
val = my_neural(5.0)
Expand All @@ -24,11 +25,12 @@ ub = [5.0,5.0]
x = sample(5,lb,ub, SobolSample())
obj_ND_neural(x) = x[1]*x[2];
y = obj_ND_neural.(x)
model = Chain(Dense(2,1), first)
loss(x, y) = Flux.mse(model(x), y)
opt = Descent(0.01)
my_model = Chain(Dense(2,1), first)
my_loss(x, y) = Flux.mse(my_model(x), y)
my_opt = Descent(0.01)
n_echos = 1
my_neural = NeuralSurrogate(x,y,lb,ub,model,loss,opt,n_echos)
my_neural = NeuralSurrogate(x,y,lb,ub,model=my_model,loss=my_loss,opt=my_opt,n_echos=1)
my_neural_kwargs = NeuralSurrogate(x,y,lb,ub)
my_neural((3.5, 1.49))
my_neural([3.4,1.4])
add_point!(my_neural,(3.5,1.4),4.9)
Expand All @@ -41,19 +43,21 @@ ub = 10.0
x = sample(5, lb, ub, SobolSample())
push!(x, 2.0)
y = f.(x)
model = Chain(Dense(1,2))
loss(x, y) = Flux.mse(model(x), y)
surrogate = NeuralSurrogate(x,y,lb,ub,model,loss,opt,n_echos)
my_model = Chain(Dense(1,2))
my_loss(x, y) = Flux.mse(my_model(x), y)
surrogate = NeuralSurrogate(x,y,lb,ub,model = my_model,loss = my_loss ,opt=my_opt,n_echos=1)
surr_kwargs = NeuralSurrogate(x,y,lb,ub)

f = x -> [x[1], x[2]^2]
lb = [1.0, 2.0]
ub = [10.0, 8.5]
x = sample(20, lb, ub, SobolSample())
push!(x, (1.0, 2.0))
y = f.(x)
model = Chain(Dense(2,2))
loss(x, y) = Flux.mse(model(x), y)
surrogate = NeuralSurrogate(x,y,lb,ub,model,loss,opt,n_echos)
my_model = Chain(Dense(2,2))
my_loss(x, y) = Flux.mse(my_model(x), y)
surrogate = NeuralSurrogate(x,y,lb,ub,model=my_model,loss=my_loss,opt=my_opt,n_echos=1)
surrogate_kwargs = NeuralSurrogate(x,y,lb,ub)
surrogate((1.0, 2.0))
x_new = (2.0, 2.0)
y_new = f(x_new)
Expand Down
2 changes: 1 addition & 1 deletion test/optimization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ model = Chain(Dense(2,1), first)
loss(x, y) = Flux.mse(model(x), y)
opt = Descent(0.01)
n_echos = 1
my_neural_ND_neural = NeuralSurrogate(x,y,lb,ub,model,loss,opt,n_echos)
my_neural_ND_neural = NeuralSurrogate(x,y,lb,ub)
surrogate_optimize(objective_function_ND,SRBF(),lb,ub,my_neural_ND_neural,SobolSample(),maxiters=15)

#Random Forest
Expand Down

0 comments on commit 7325b0e

Please sign in to comment.