Skip to content

Commit 5bc3cd2

Browse files
Merge pull request #341 from SciML/RA/subpackage
Split into subpackages
2 parents ebd6ee8 + cb2bf4a commit 5bc3cd2

File tree

23 files changed

+549
-278
lines changed

23 files changed

+549
-278
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
docs/build/
33
Manifest.toml
44
.vscode/settings.json
5+
*.swp

Project.toml

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,38 @@ authors = ["SciML"]
44
version = "5.0.1"
55

66
[deps]
7-
AbstractGPs = "99985d1d-32ba-4be9-9821-2ec096f28918"
87
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
98
ExtendableSparse = "95c220a8-a1cf-11e9-0c77-dbfce5f500b3"
109
GLM = "38e38edf-8417-5370-95a0-9cbb8c7f171a"
1110
IterativeSolvers = "42fd0dbc-a981-5370-80f2-aaf504508153"
1211
LatinHypercubeSampling = "a5e1c1ea-c99a-51d3-a14d-a9a37257b02d"
1312
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
14-
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
1513
Sobol = "ed01d8cd-4d21-5b2a-85b4-cc3bdc58bad4"
1614
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
17-
XGBoost = "009559a3-9522-5dbb-924b-0b6ed2b22bb9"
1815
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"
1916

2017
[compat]
21-
AbstractGPs = "0.5"
2218
Distributions = "0.21, 0.22, 0.23, 0.24, 0.25"
2319
ExtendableSparse = "0.4, 0.5, 0.6"
2420
Flux = "0.12, 0.13"
2521
GLM = "1.3"
2622
IterativeSolvers = "0.9"
2723
LatinHypercubeSampling = "1.2"
2824
PolyChaos = "0.2"
29-
Requires = "0.5, 1.0"
3025
Sobol = "1.3"
31-
XGBoost = "0.4, 1.1"
3226
Zygote = "0.4, 0.5, 0.6"
3327
julia = "1.6"
3428

3529
[extras]
3630
Cubature = "667455a9-e2ce-5579-9412-b964f529a492"
3731
Flux = "587475ba-b771-5e3f-ad9e-33799f191a9c"
3832
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
33+
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
3934
PolyChaos = "8d666b04-775d-5f6e-b778-5ac7c70f65a3"
4035
QuadGK = "1fd47b50-473d-5c70-9696-f719f8f3bcdc"
4136
SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"
4237
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
4338
Tracker = "9f7883ad-71c0-57eb-9f7f-b5c9e6d3789c"
4439

4540
[targets]
46-
test = ["Cubature", "SafeTestsets", "Flux", "ForwardDiff", "PolyChaos", "QuadGK", "Test", "Tracker"]
41+
test = ["Cubature", "SafeTestsets", "Flux", "ForwardDiff", "PolyChaos", "QuadGK", "Test", "Tracker", "Pkg"]
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name = "SurrogatesAbstractGPs"
2+
uuid = "78aa1720-c2af-471b-b307-964fd38f9b5f"
3+
authors = ["Ranjan Anantharaman <[email protected]>"]
4+
version = "0.1.0"
5+
6+
[deps]
7+
AbstractGPs = "99985d1d-32ba-4be9-9821-2ec096f28918"
8+
Surrogates = "6fc51010-71bc-11e9-0e15-a3fcc6593c49"
9+
10+
[compat]
11+
AbstractGPs = "0.5"
12+
julia = "1.6"
13+
14+
[extras]
15+
SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"
16+
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
17+
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"
18+
19+
[targets]
20+
test = ["SafeTestsets", "Test", "Zygote"]

src/AbstractGP.jl renamed to lib/SurrogatesAbstractGPs/src/SurrogatesAbstractGPs.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
module SurrogatesAbstractGPs
2+
3+
import Surrogates: add_point!, AbstractSurrogate, std_error_at_point
4+
export AbstractGPSurrogate, var_at_point, logpdf_surrogate
5+
16
using AbstractGPs
27

38
mutable struct AbstractGPSurrogate{X, Y, GP, GP_P, S} <: AbstractSurrogate
@@ -43,3 +48,5 @@ end
4348
function logpdf_surrogate(g::AbstractGPSurrogate)
4449
return logpdf(g.gp_posterior(g.x), g.y)
4550
end
51+
52+
end # module

test/AbstractGP.jl renamed to lib/SurrogatesAbstractGPs/test/runtests.jl

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
using SafeTestsets, Test
2+
using Surrogates: sample, SobolSample
3+
4+
@safetestset "AbstractGPSurrogate" begin
5+
16
using Surrogates
7+
using SurrogatesAbstractGPs
28
using AbstractGPs
3-
9+
using Zygote
410

511
@testset "1D -> 1D" begin
612
lb = 0.0
@@ -117,3 +123,34 @@ end
117123
agpND = AbstractGPSurrogate(x,y, gp=GP(SqExponentialKernel()), Σy=0.05)
118124
logpdf_surrogate(agpND)
119125
end
126+
127+
lb = 0.0
128+
ub = 3.0
129+
n = 10
130+
x = sample(n,lb,ub,SobolSample())
131+
f = x -> x^2
132+
y = f.(x)
133+
134+
#AbstractGP 1D
135+
@testset "AbstractGP 1D" begin
136+
agp1D = AbstractGPSurrogate(x,y, gp=GP(SqExponentialKernel()), Σy=0.05)
137+
g = x -> agp1D'(x)
138+
g([2.0])
139+
end
140+
141+
lb = [0.0,0.0]
142+
ub = [10.0,10.0]
143+
n = 5
144+
x = sample(n,lb,ub,SobolSample())
145+
f = x -> x[1]*x[2]
146+
y = f.(x)
147+
148+
# AbstractGP ND
149+
@testset "AbstractGPSurrogate ND" begin
150+
my_agp = AbstractGPSurrogate(x,y, gp=GP(SqExponentialKernel()), Σy=0.05)
151+
g = x ->Zygote.gradient(my_agp, x)
152+
#g([(2.0,5.0)])
153+
g((2.0,5.0))
154+
end
155+
156+
end

lib/SurrogatesFlux/Project.toml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name = "SurrogatesFlux"
2+
uuid = "4f55584b-dac4-4b1c-802a-b7c47b72cb4c"
3+
authors = ["Ranjan Anantharaman <[email protected]>"]
4+
version = "0.1.0"
5+
6+
[deps]
7+
Flux = "587475ba-b771-5e3f-ad9e-33799f191a9c"
8+
Surrogates = "6fc51010-71bc-11e9-0e15-a3fcc6593c49"
9+
10+
[extras]
11+
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
12+
SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"
13+
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
14+
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"
15+
16+
[targets]
17+
test = ["LinearAlgebra", "SafeTestsets", "Test", "Zygote"]

src/NeuralSurrogate.jl renamed to lib/SurrogatesFlux/src/SurrogatesFlux.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
module SurrogatesFlux
2+
3+
import Surrogates: add_point!, AbstractSurrogate
4+
export NeuralSurrogate
5+
16
using Flux
27
using Flux: @epochs
38

@@ -53,3 +58,5 @@ function add_point!(my_n::NeuralSurrogate, x_new, y_new)
5358
@epochs my_n.n_echos Flux.train!(my_n.loss, my_n.ps, data, my_n.opt)
5459
nothing
5560
end
61+
62+
end # module
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
using SafeTestsets
2+
3+
@safetestset "SurrogatesFlux" begin
4+
5+
using Surrogates
6+
using Surrogates: SobolSample
7+
using Flux
8+
using Flux: @epochs
9+
using SurrogatesFlux
10+
using LinearAlgebra
11+
using Zygote
12+
13+
#1D
14+
a = 0.0
15+
b = 10.0
16+
obj_1D = x -> 2*x+3
17+
x = sample(10,0.0,10.,SobolSample())
18+
y = obj_1D.(x);
19+
my_model = Chain(Dense(1,1), first)
20+
my_loss(x, y) = Flux.mse(my_model(x), y)
21+
my_opt = Descent(0.01)
22+
n_echos = 1
23+
my_neural = NeuralSurrogate(x,y,a,b,model = my_model,loss = my_loss,opt = my_opt,n_echos=1)
24+
my_neural_kwargs = NeuralSurrogate(x,y,a,b)
25+
add_point!(my_neural,8.5,20.0)
26+
add_point!(my_neural,[3.2,3.5],[7.4,8.0])
27+
val = my_neural(5.0)
28+
29+
#ND
30+
31+
lb = [0.0,0.0]
32+
ub = [5.0,5.0]
33+
x = sample(5,lb,ub, SobolSample())
34+
obj_ND_neural(x) = x[1]*x[2];
35+
y = obj_ND_neural.(x)
36+
my_model = Chain(Dense(2,1), first)
37+
my_loss(x, y) = Flux.mse(my_model(x), y)
38+
my_opt = Descent(0.01)
39+
n_echos = 1
40+
my_neural = NeuralSurrogate(x,y,lb,ub,model=my_model,loss=my_loss,opt=my_opt,n_echos=1)
41+
my_neural_kwargs = NeuralSurrogate(x,y,lb,ub)
42+
my_neural((3.5, 1.49))
43+
my_neural([3.4,1.4])
44+
add_point!(my_neural,(3.5,1.4),4.9)
45+
add_point!(my_neural,[(3.5,1.4),(1.5,1.4),(1.3,1.2)],[1.3,1.4,1.5])
46+
47+
# Multi-output #98
48+
f = x -> [x^2, x]
49+
lb = 1.0
50+
ub = 10.0
51+
x = sample(5, lb, ub, SobolSample())
52+
push!(x, 2.0)
53+
y = f.(x)
54+
my_model = Chain(Dense(1,2))
55+
my_loss(x, y) = Flux.mse(my_model(x), y)
56+
surrogate = NeuralSurrogate(x,y,lb,ub,model = my_model,loss = my_loss ,opt=my_opt,n_echos=1)
57+
surr_kwargs = NeuralSurrogate(x,y,lb,ub)
58+
59+
f = x -> [x[1], x[2]^2]
60+
lb = [1.0, 2.0]
61+
ub = [10.0, 8.5]
62+
x = sample(20, lb, ub, SobolSample())
63+
push!(x, (1.0, 2.0))
64+
y = f.(x)
65+
my_model = Chain(Dense(2,2))
66+
my_loss(x, y) = Flux.mse(my_model(x), y)
67+
surrogate = NeuralSurrogate(x,y,lb,ub,model=my_model,loss=my_loss,opt=my_opt,n_echos=1)
68+
surrogate_kwargs = NeuralSurrogate(x,y,lb,ub)
69+
surrogate((1.0, 2.0))
70+
x_new = (2.0, 2.0)
71+
y_new = f(x_new)
72+
add_point!(surrogate, x_new, y_new)
73+
74+
#Optimization
75+
lb = [1.0,1.0]
76+
ub = [6.0,6.0]
77+
x = sample(5,lb,ub,SobolSample())
78+
objective_function_ND = z -> 3*norm(z)+1
79+
y = objective_function_ND.(x)
80+
model = Chain(Dense(2,1), first)
81+
loss(x, y) = Flux.mse(model(x), y)
82+
opt = Descent(0.01)
83+
n_echos = 1
84+
my_neural_ND_neural = NeuralSurrogate(x,y,lb,ub)
85+
surrogate_optimize(objective_function_ND,SRBF(),lb,ub,my_neural_ND_neural,SobolSample(),maxiters=15)
86+
87+
# AD Compatibility
88+
lb = 0.0
89+
ub = 3.0
90+
n = 10
91+
x = sample(n,lb,ub,SobolSample())
92+
f = x -> x^2
93+
y = f.(x)
94+
#NN
95+
@testset "NN" begin
96+
my_model = Chain(Dense(1,1), first)
97+
my_loss(x, y) = Flux.mse(my_model(x), y)
98+
my_opt = Descent(0.01)
99+
n_echos = 1
100+
my_neural = NeuralSurrogate(x,y,lb,ub,model=my_model,loss=my_loss,opt=my_opt,n_echos=1)
101+
g = x->my_neural'(x)
102+
g(3.4)
103+
end
104+
105+
lb = [0.0,0.0]
106+
ub = [10.0,10.0]
107+
n = 5
108+
x = sample(n,lb,ub,SobolSample())
109+
f = x -> x[1]*x[2]
110+
y = f.(x)
111+
112+
#NN
113+
@testset "NN ND" begin
114+
my_model = Chain(Dense(2,1), first)
115+
my_loss(x, y) = Flux.mse(my_model(x), y)
116+
my_opt = Descent(0.01)
117+
n_echos = 1
118+
my_neural = NeuralSurrogate(x,y,lb,ub,model=my_model,loss=my_loss,opt=my_opt,n_echos=1)
119+
g = x -> Zygote.gradient(my_neural, x)
120+
g((2.0,5.0))
121+
end
122+
123+
# ###### ND -> ND ######
124+
125+
lb = [0.0, 0.0]
126+
ub = [10.0, 2.0]
127+
n = 5
128+
x = sample(n,lb,ub,SobolSample())
129+
f = x -> [x[1]^2, x[2]]
130+
y = f.(x)
131+
132+
#NN
133+
@testset "NN ND -> ND" begin
134+
my_model = Chain(Dense(2,2))
135+
my_loss(x, y) = Flux.mse(my_model(x), y)
136+
my_opt = Descent(0.01)
137+
n_echos = 1
138+
my_neural = NeuralSurrogate(x,y,lb,ub,model=my_model,loss=my_loss,opt=my_opt,n_echos=1)
139+
Zygote.gradient(x -> sum(my_neural(x)), (2.0, 5.0))
140+
141+
my_rad = RadialBasis(x,y,lb,ub,rad = linearRadial())
142+
Zygote.gradient(x -> sum(my_rad(x)), (2.0, 5.0))
143+
144+
my_p = 1.4
145+
my_inverse = InverseDistanceSurrogate(x,y,lb,ub,p=my_p)
146+
my_inverse((2.0, 5.0))
147+
Zygote.gradient(x -> sum(my_inverse(x)), (2.0, 5.0))
148+
149+
my_second = SecondOrderPolynomialSurrogate(x,y,lb,ub)
150+
Zygote.gradient(x -> sum(my_second(x)), (2.0, 5.0))
151+
end
152+
end
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
name = "SurrogatesPolyChaos"
2+
uuid = "50679fc6-c85c-4a6e-ac63-dc3c8bd8cb1c"
3+
authors = ["Ranjan Anantharaman <[email protected]>"]
4+
version = "0.1.0"
5+
6+
[deps]
7+
PolyChaos = "8d666b04-775d-5f6e-b778-5ac7c70f65a3"
8+
Surrogates = "6fc51010-71bc-11e9-0e15-a3fcc6593c49"
9+
10+
[extras]
11+
SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"
12+
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
13+
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"
14+
15+
[targets]
16+
test = ["SafeTestsets", "Test", "Zygote"]

0 commit comments

Comments
 (0)