Spaces:
Running
Running
AutonLabTruth
commited on
Commit
•
a8b2d62
1
Parent(s):
b865169
Added Julia Files
Browse files- julia/truth.jl +77 -0
- julia/truthPops.jl +170 -0
julia/truth.jl
ADDED
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# *** Custom Functions
|
2 |
+
##################################################################################################################################
|
3 |
+
# *** Will somewhere need to define a list TRUTHS of all valid auxliary truths
|
4 |
+
struct Transformation
|
5 |
+
type::Integer # 1 is symmetry, 2 is zero, 3 is equality
|
6 |
+
params::Array{Int32}
|
7 |
+
Transformation(type::Integer, params::Array{Int32}) = new(type, params)
|
8 |
+
Transformation(type::Integer, params::Array{Int64}) = new(type, params)
|
9 |
+
|
10 |
+
end
|
11 |
+
struct Truth
|
12 |
+
transformation::Transformation
|
13 |
+
weights::Array{Float32}
|
14 |
+
Truth(transformation::Transformation, weights::Array{Float32}) = new(transformation, weights)
|
15 |
+
Truth(type::Int64, params::Array{Int64}, weights::Array{Float32}) = new(Transformation(type, params), weights)
|
16 |
+
Truth(transformation::Transformation, weights::Array{Float64}) = new(transformation, weights)
|
17 |
+
Truth(type::Int64, params::Array{Int64}, weights::Array{Float64}) = new(Transformation(type, params), weights)
|
18 |
+
end
|
19 |
+
# Returns a linear combination when given X of shape nxd, y of shape nx1 is f(x) and w of shape d+2x1, result is shape nx1
|
20 |
+
function LinearPrediction(cX::Array{Float32}, cy::Array{Float32}, w::Array{Float32})::Array{Float32}
|
21 |
+
preds = 0
|
22 |
+
for i in 1:ndims(cX)
|
23 |
+
preds = preds .+ cX[:,i].*w[i]
|
24 |
+
end
|
25 |
+
preds = preds .+ cy.*w[ndims(cX)+1]
|
26 |
+
return preds .+ w[ndims(cX)+2]
|
27 |
+
end
|
28 |
+
|
29 |
+
# Returns a copy of the data with the two specified columns swapped
|
30 |
+
function swapColumns(cX::Array{Float32, 2}, a::Integer, b::Integer)::Array{Float32, 2}
|
31 |
+
X1 = copy(cX)
|
32 |
+
X1[:, a] = cX[:, b]
|
33 |
+
X1[:, b] = cX[:, a]
|
34 |
+
return X1
|
35 |
+
end
|
36 |
+
|
37 |
+
# Returns a copy of the data with the specified integers in the list set to value given
|
38 |
+
function setVal(cX::Array{Float32, 2}, a::Array{Int32, 1}, val::Float32)::Array{Float32, 2}
|
39 |
+
X1 = copy(cX)
|
40 |
+
for i in 1:size(a)[1]
|
41 |
+
X1[:, a[i]] = fill!(cX[:, a[i]], val)
|
42 |
+
end
|
43 |
+
return X1
|
44 |
+
end
|
45 |
+
|
46 |
+
# Returns a copy of the data with the specified integer indices in the list set to the first item of that list
|
47 |
+
function setEq(cX::Array{Float32, 2}, a::Array{Int32, 1})::Array{Float32, 2}
|
48 |
+
X1 = copy(cX)
|
49 |
+
val = X1[:, a[1]]
|
50 |
+
for i in 1:size(a)[1]
|
51 |
+
X1[:, a[i]] = val
|
52 |
+
end
|
53 |
+
return X1
|
54 |
+
end
|
55 |
+
|
56 |
+
# Takes in a dataset and returns the transformed version of it as per the specified type and parameters
|
57 |
+
function transform(cX::Array{Float32, 2}, transformation::Transformation)::Array{Float32, 2}
|
58 |
+
if transformation.type==1 # then symmetry
|
59 |
+
a = transformation.params[1]
|
60 |
+
b = transformation.params[2]
|
61 |
+
return swapColumns(cX, a, b)
|
62 |
+
elseif transformation.type==2 # then zero condition
|
63 |
+
return setVal(cX, transformation.params, Float32(0))
|
64 |
+
elseif transformation.type == 3 # then equality condition
|
65 |
+
return setEq(cX, transformation.params)
|
66 |
+
else # Then error return X
|
67 |
+
return cX
|
68 |
+
end
|
69 |
+
end
|
70 |
+
function transform(cX::Array{Float32, 2}, truth::Truth)::Array{Float32, 2}
|
71 |
+
return transform(cX, truth.transformation)
|
72 |
+
end
|
73 |
+
|
74 |
+
# Takes in X that has been transformed and returns what the Truth projects the target values should be
|
75 |
+
function truthPrediction(X_transformed::Array{Float32, 2}, cy::Array{Float32}, truth::Truth)::Array{Float32}
|
76 |
+
return LinearPrediction(X_transformed, cy, truth.weights)
|
77 |
+
end
|
julia/truthPops.jl
ADDED
@@ -0,0 +1,170 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Returns the MSE between the predictions and the truth provided targets for the given dataset
|
2 |
+
function truthScore(member::PopMember, cX::Array{Float32, 2}, cy::Array{Float32}, truth::Truth)::Float32
|
3 |
+
transformed = transform(cX, truth)
|
4 |
+
targets = truthPrediction(transformed, cy, truth)
|
5 |
+
preds = evalTreeArray(member.tree, transformed)
|
6 |
+
return MSE(preds, targets)
|
7 |
+
end
|
8 |
+
|
9 |
+
# Assumes a dataset X, y for a given truth
|
10 |
+
function truthScore(member::PopMember, truth::Truth)::Float32
|
11 |
+
return truthScore(member, X, y, truth)
|
12 |
+
end
|
13 |
+
|
14 |
+
# Assumes a list of Truths TRUTHS is defined. Performs the truthScore function for each of them and returns the average
|
15 |
+
function truthScore(member::PopMember, cX::Array{Float32, 2}, cy::Array{Float32})::Float32
|
16 |
+
s = 0
|
17 |
+
for truth in TRUTHS
|
18 |
+
s += (truthScore(member, cX, cy, truth))/size(TRUTHS)[1]
|
19 |
+
end
|
20 |
+
return s
|
21 |
+
end
|
22 |
+
|
23 |
+
# Assumes list of Truths TRUTHS and dataset X, y are defined
|
24 |
+
function truthScore(member::PopMember)::Float32
|
25 |
+
return truthScore(member, X, y)
|
26 |
+
end
|
27 |
+
# Returns the MSE between the predictions and the truth provided targets for the given dataset
|
28 |
+
function truthScore(tree::Node, cX::Array{Float32, 2}, cy::Array{Float32}, truth::Truth)::Float32
|
29 |
+
transformed = transform(cX, truth)
|
30 |
+
targets = truthPrediction(transformed, cy, truth)
|
31 |
+
preds = evalTreeArray(tree, transformed)
|
32 |
+
return MSE(preds, targets)
|
33 |
+
end
|
34 |
+
|
35 |
+
# Assumes a dataset X, y for a given truth
|
36 |
+
function truthScore(tree::Node, truth::Truth)::Float32
|
37 |
+
return truthScore(tree, X, y, truth)
|
38 |
+
end
|
39 |
+
|
40 |
+
# Assumes a list of Truths TRUTHS is defined. Performs the truthScore function for each of them and returns the average
|
41 |
+
function truthScore(tree::Node, cX::Array{Float32, 2}, cy::Array{Float32})::Float32
|
42 |
+
s = 0
|
43 |
+
for truth in TRUTHS
|
44 |
+
s += (truthScore(tree, cX, cy, truth))/size(TRUTHS)[1]
|
45 |
+
end
|
46 |
+
return s
|
47 |
+
end
|
48 |
+
|
49 |
+
# Assumes list of Truths TRUTHS and dataset X, y are defined
|
50 |
+
function truthScore(tree::Node)::Float32
|
51 |
+
return truthScore(tree, X, y)
|
52 |
+
end
|
53 |
+
|
54 |
+
# Returns true iff Truth Score is below a given threshold i.e truth is satisfied
|
55 |
+
function testTruth(member::PopMember, truth::Truth, threshold::Float32=Float32(1.0e-8))::Bool
|
56 |
+
truthError = truthScore(member, truth)
|
57 |
+
#print(stringTree(member.tree), "\n")
|
58 |
+
#print(truth, ": ")
|
59 |
+
#print(truthError, "\n")
|
60 |
+
if truthError > threshold
|
61 |
+
#print("Returns False \n ----\n")
|
62 |
+
return false
|
63 |
+
else
|
64 |
+
#print("Returns True \n ----\n")
|
65 |
+
return true
|
66 |
+
end
|
67 |
+
end
|
68 |
+
|
69 |
+
# Returns a list of violating functions from assumed list TRUTHS
|
70 |
+
function violatingTruths(member::PopMember)::Array{Truth}
|
71 |
+
return violatingTruths(member.tree)
|
72 |
+
end
|
73 |
+
|
74 |
+
# Returns true iff Truth Score is below a given threshold i.e truth is satisfied
|
75 |
+
function testTruth(tree::Node, truth::Truth, threshold::Float32=Float32(1.0e-3))::Bool
|
76 |
+
truthError = truthScore(tree, truth)
|
77 |
+
if truthError > threshold
|
78 |
+
return false
|
79 |
+
else
|
80 |
+
return true
|
81 |
+
end
|
82 |
+
end
|
83 |
+
|
84 |
+
# Returns a list of violating functions from assumed list TRUTHS
|
85 |
+
function violatingTruths(tree::Node)::Array{Truth}
|
86 |
+
toReturn = []
|
87 |
+
#print("\n Checking Equation ", stringTree(tree), "\n")
|
88 |
+
for truth in TRUTHS
|
89 |
+
test_truth = testTruth(tree, truth)
|
90 |
+
#print("Truth: ", truth, ": " , test_truth, "\n-----\n")
|
91 |
+
if !test_truth
|
92 |
+
append!(toReturn, [truth])
|
93 |
+
end
|
94 |
+
end
|
95 |
+
return toReturn
|
96 |
+
end
|
97 |
+
|
98 |
+
function randomIndex(cX::Array{Float32, 2}, k::Integer=10)::Array{Int32, 1}
|
99 |
+
indxs = sample([Int32(i) for i in 1:size(cX)[1]], k)
|
100 |
+
return indxs
|
101 |
+
end
|
102 |
+
|
103 |
+
function randomIndex(leng::Integer, k::Integer=10)::Array{Int32, 1}
|
104 |
+
indxs = sample([Int32(i) for i in 1:leng], k)
|
105 |
+
return indxs
|
106 |
+
end
|
107 |
+
|
108 |
+
function extendedX(cX::Array{Float32, 2}, truth::Truth, indx::Array{Int32, 1})::Array{Float32, 2}
|
109 |
+
workingcX = copy(cX)
|
110 |
+
X_slice = workingcX[indx, :]
|
111 |
+
X_transformed = transform(X_slice, truth)
|
112 |
+
return X_transformed
|
113 |
+
end
|
114 |
+
function extendedX(truth::Truth, indx::Array{Int32, 1})::Union{Array{Float32, 2}, Nothing}
|
115 |
+
return extendedX(OGX, truth, indx)
|
116 |
+
end
|
117 |
+
function extendedX(cX::Array{Float32, 2}, violatedTruths::Array{Truth}, indx::Array{Int32, 1})::Union{Array{Float32, 2}, Nothing}
|
118 |
+
if length(violatedTruths) == 0
|
119 |
+
return nothing
|
120 |
+
end
|
121 |
+
workingX = extendedX(cX, violatedTruths[1], indx)
|
122 |
+
for truth in violatedTruths[2:length(violatedTruths)]
|
123 |
+
workingX = vcat(workingX, extendedX(cX, truth, indx))
|
124 |
+
end
|
125 |
+
return workingX
|
126 |
+
end
|
127 |
+
function extendedX(violatedTruths::Array{Truth}, indx::Array{Int32, 1})::Union{Array{Float32, 2}, Nothing}
|
128 |
+
return extendedX(OGX, violatedTruths, indx)
|
129 |
+
end
|
130 |
+
function extendedX(tree::Node, indx::Array{Int32, 1})::Union{Array{Float32, 2}, Nothing}
|
131 |
+
violatedTruths = violatingTruths(tree)
|
132 |
+
return extendedX(violatedTruths, indx)
|
133 |
+
end
|
134 |
+
function extendedX(member::PopMember, indx::Array{Int32, 1})::Union{Array{Float32, 2}, Nothing}
|
135 |
+
return extendedX(member.tree, indx)
|
136 |
+
end
|
137 |
+
|
138 |
+
|
139 |
+
function extendedy(cX::Array{Float32, 2}, cy::Array{Float32}, truth::Truth, indx::Array{Int32, 1})::Union{Array{Float32}, Nothing}
|
140 |
+
cy = copy(cy)
|
141 |
+
cX = copy(cX)
|
142 |
+
X_slice = cX[indx, :]
|
143 |
+
y_slice = cy[indx]
|
144 |
+
X_transformed = transform(X_slice, truth)
|
145 |
+
y_transformed = truthPrediction(X_transformed, y_slice, truth)
|
146 |
+
return y_transformed
|
147 |
+
end
|
148 |
+
function extendedy(truth::Truth, indx::Array{Int32, 1})::Union{Array{Float32}, Nothing}
|
149 |
+
return extendedy(OGX, OGy, truth, indx)
|
150 |
+
end
|
151 |
+
function extendedy(cX::Array{Float32, 2}, cy::Array{Float32}, violatedTruths::Array{Truth}, indx::Array{Int32, 1})::Union{Array{Float32}, Nothing}
|
152 |
+
if length(violatedTruths) == 0
|
153 |
+
return nothing
|
154 |
+
end
|
155 |
+
workingy = extendedy(cX, cy, violatedTruths[1], indx)
|
156 |
+
for truth in violatedTruths[2:length(violatedTruths)]
|
157 |
+
workingy = vcat(workingy, extendedy(cX, cy, truth, indx))
|
158 |
+
end
|
159 |
+
return workingy
|
160 |
+
end
|
161 |
+
function extendedy(violatedTruths::Array{Truth}, indx::Array{Int32, 1})::Union{Array{Float32}, Nothing}
|
162 |
+
return extendedy(OGX,OGy, violatedTruths, indx)
|
163 |
+
end
|
164 |
+
function extendedy(tree::Node, indx::Array{Int32, 1})::Union{Array{Float32}, Nothing}
|
165 |
+
violatedTruths = violatingTruths(tree)
|
166 |
+
return extendedy(violatedTruths, indx)
|
167 |
+
end
|
168 |
+
function extendedy(member::PopMember, indx::Array{Int32, 1})::Union{Array{Float32}, Nothing}
|
169 |
+
return extendedy(member.tree, indx)
|
170 |
+
end
|