Spaces:
Running
Running
AutonLabTruth
commited on
Commit
•
a9184d1
1
Parent(s):
502bd82
Refactored till errors
Browse files- julia/constants.jl +9 -0
- julia/errors.jl +37 -0
- julia/sr.jl +3 -45
- main.py +5 -1
julia/constants.jl
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
const maxdegree = 2
|
3 |
+
const actualMaxsize = maxsize + maxdegree
|
4 |
+
const len = size(X)[1]
|
5 |
+
|
6 |
+
const nuna = size(unaops)[1]
|
7 |
+
const nbin = size(binops)[1]
|
8 |
+
const nops = nuna + nbin
|
9 |
+
const nvar = size(X)[2];
|
julia/errors.jl
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Sum of square error between two arrays
|
2 |
+
function SSE(x::Array{Float32}, y::Array{Float32})::Float32
|
3 |
+
diff = (x - y)
|
4 |
+
return sum(diff .* diff)
|
5 |
+
end
|
6 |
+
function SSE(x::Nothing, y::Array{Float32})::Float32
|
7 |
+
return 1f9
|
8 |
+
end
|
9 |
+
|
10 |
+
# Sum of square error between two arrays, with weights
|
11 |
+
function SSE(x::Array{Float32}, y::Array{Float32}, w::Array{Float32})::Float32
|
12 |
+
diff = (x - y)
|
13 |
+
return sum(diff .* diff .* w)
|
14 |
+
end
|
15 |
+
function SSE(x::Nothing, y::Array{Float32}, w::Array{Float32})::Float32
|
16 |
+
return Nothing
|
17 |
+
end
|
18 |
+
|
19 |
+
# Mean of square error between two arrays
|
20 |
+
function MSE(x::Nothing, y::Array{Float32})::Float32
|
21 |
+
return 1f9
|
22 |
+
end
|
23 |
+
|
24 |
+
# Mean of square error between two arrays
|
25 |
+
function MSE(x::Array{Float32}, y::Array{Float32})::Float32
|
26 |
+
return SSE(x, y)/size(x)[1]
|
27 |
+
end
|
28 |
+
|
29 |
+
# Mean of square error between two arrays
|
30 |
+
function MSE(x::Nothing, y::Array{Float32}, w::Array{Float32})::Float32
|
31 |
+
return 1f9
|
32 |
+
end
|
33 |
+
|
34 |
+
# Mean of square error between two arrays
|
35 |
+
function MSE(x::Array{Float32}, y::Array{Float32}, w::Array{Float32})::Float32
|
36 |
+
return SSE(x, y, w)/sum(w)
|
37 |
+
end
|
julia/sr.jl
CHANGED
@@ -2,49 +2,10 @@ import Optim
|
|
2 |
import Printf: @printf
|
3 |
import Random: shuffle!, randperm
|
4 |
|
5 |
-
const maxdegree = 2
|
6 |
-
const actualMaxsize = maxsize + maxdegree
|
7 |
|
|
|
8 |
|
9 |
-
|
10 |
-
function SSE(x::Array{Float32}, y::Array{Float32})::Float32
|
11 |
-
diff = (x - y)
|
12 |
-
return sum(diff .* diff)
|
13 |
-
end
|
14 |
-
function SSE(x::Nothing, y::Array{Float32})::Float32
|
15 |
-
return 1f9
|
16 |
-
end
|
17 |
-
|
18 |
-
# Sum of square error between two arrays, with weights
|
19 |
-
function SSE(x::Array{Float32}, y::Array{Float32}, w::Array{Float32})::Float32
|
20 |
-
diff = (x - y)
|
21 |
-
return sum(diff .* diff .* w)
|
22 |
-
end
|
23 |
-
function SSE(x::Nothing, y::Array{Float32}, w::Array{Float32})::Float32
|
24 |
-
return Nothing
|
25 |
-
end
|
26 |
-
|
27 |
-
# Mean of square error between two arrays
|
28 |
-
function MSE(x::Nothing, y::Array{Float32})::Float32
|
29 |
-
return 1f9
|
30 |
-
end
|
31 |
-
|
32 |
-
# Mean of square error between two arrays
|
33 |
-
function MSE(x::Array{Float32}, y::Array{Float32})::Float32
|
34 |
-
return SSE(x, y)/size(x)[1]
|
35 |
-
end
|
36 |
-
|
37 |
-
# Mean of square error between two arrays
|
38 |
-
function MSE(x::Nothing, y::Array{Float32}, w::Array{Float32})::Float32
|
39 |
-
return 1f9
|
40 |
-
end
|
41 |
-
|
42 |
-
# Mean of square error between two arrays
|
43 |
-
function MSE(x::Array{Float32}, y::Array{Float32}, w::Array{Float32})::Float32
|
44 |
-
return SSE(x, y, w)/sum(w)
|
45 |
-
end
|
46 |
-
|
47 |
-
const len = size(X)[1]
|
48 |
|
49 |
if weighted
|
50 |
const avgy = sum(y .* weights)/sum(weights)
|
@@ -59,10 +20,7 @@ function id(x::Float32)::Float32
|
|
59 |
x
|
60 |
end
|
61 |
|
62 |
-
|
63 |
-
const nbin = size(binops)[1]
|
64 |
-
const nops = nuna + nbin
|
65 |
-
const nvar = size(X)[2];
|
66 |
|
67 |
function debug(verbosity, string...)
|
68 |
verbosity > 0 ? println(string...) : nothing
|
|
|
2 |
import Printf: @printf
|
3 |
import Random: shuffle!, randperm
|
4 |
|
|
|
|
|
5 |
|
6 |
+
include("constants.jl")
|
7 |
|
8 |
+
include("errors.jl")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
if weighted
|
11 |
const avgy = sum(y .* weights)/sum(weights)
|
|
|
20 |
x
|
21 |
end
|
22 |
|
23 |
+
|
|
|
|
|
|
|
24 |
|
25 |
function debug(verbosity, string...)
|
26 |
verbosity > 0 ? println(string...) : nothing
|
main.py
CHANGED
@@ -1,15 +1,19 @@
|
|
1 |
import numpy as np
|
2 |
from pysr import pysr, best, get_hof
|
|
|
3 |
|
4 |
# Dataset
|
5 |
X = 2*np.random.randn(100, 5)
|
6 |
y = 2*np.cos(X[:, 3]) + X[:, 0]**2 - 2
|
7 |
|
|
|
8 |
# Learn equations
|
|
|
9 |
equations = pysr(X, y, niterations=5,
|
10 |
binary_operators=["plus", "mult"],
|
11 |
unary_operators=["cos", "exp", "sin"])
|
12 |
|
13 |
... # (you can use ctl-c to exit early)
|
14 |
|
15 |
-
print(best(equations))
|
|
|
|
1 |
import numpy as np
|
2 |
from pysr import pysr, best, get_hof
|
3 |
+
import time
|
4 |
|
5 |
# Dataset
|
6 |
X = 2*np.random.randn(100, 5)
|
7 |
y = 2*np.cos(X[:, 3]) + X[:, 0]**2 - 2
|
8 |
|
9 |
+
|
10 |
# Learn equations
|
11 |
+
start = time.time()
|
12 |
equations = pysr(X, y, niterations=5,
|
13 |
binary_operators=["plus", "mult"],
|
14 |
unary_operators=["cos", "exp", "sin"])
|
15 |
|
16 |
... # (you can use ctl-c to exit early)
|
17 |
|
18 |
+
print(best(equations))
|
19 |
+
print(f"Took {time.time()-start} seconds")
|