MilesCranmer commited on
Commit
7aaa5b1
1 Parent(s): fee23e7

Do manual check for domain errors, rather than try-catch

Browse files
Files changed (1) hide show
  1. julia/sr.jl +56 -38
julia/sr.jl CHANGED
@@ -11,18 +11,34 @@ function SSE(x::Array{Float32}, y::Array{Float32})::Float32
11
  diff = (x - y)
12
  return sum(diff .* diff)
13
  end
 
 
 
14
 
15
  # Sum of square error between two arrays, with weights
16
  function SSE(x::Array{Float32}, y::Array{Float32}, w::Array{Float32})::Float32
17
  diff = (x - y)
18
  return sum(diff .* diff .* w)
19
  end
 
 
 
 
 
 
 
 
20
 
21
  # Mean of square error between two arrays
22
  function MSE(x::Array{Float32}, y::Array{Float32})::Float32
23
  return SSE(x, y)/size(x)[1]
24
  end
25
 
 
 
 
 
 
26
  # Mean of square error between two arrays
27
  function MSE(x::Array{Float32}, y::Array{Float32}, w::Array{Float32})::Float32
28
  return SSE(x, y, w)/sum(w)
@@ -261,34 +277,13 @@ function mutateConstant(
261
  end
262
 
263
  # Evaluate an equation over an array of datapoints
264
- function evalTreeArray(tree::Node)::Array{Float32, 1}
265
- if tree.degree == 0
266
- if tree.constant
267
- return fill(tree.val, len)
268
- else
269
- return copy(X[:, tree.val])
270
- end
271
- elseif tree.degree == 1
272
- cumulator = evalTreeArray(tree.l)
273
- op = unaops[tree.op]
274
- @inbounds for i=1:len
275
- cumulator[i] = op(cumulator[i])
276
- end
277
- return cumulator
278
- else
279
- op = binops[tree.op]
280
- cumulator = evalTreeArray(tree.l)
281
- array2 = evalTreeArray(tree.r)
282
- @inbounds for i=1:len
283
- cumulator[i] = op(cumulator[i], array2[i])
284
- end
285
- return cumulator
286
- end
287
  end
288
 
289
 
290
  # Evaluate an equation over an array of datapoints
291
- function evalTreeArray(tree::Node, cX::Array{Float32, 2})::Array{Float32, 1}
292
  clen = size(cX)[1]
293
  if tree.degree == 0
294
  if tree.constant
@@ -298,31 +293,53 @@ function evalTreeArray(tree::Node, cX::Array{Float32, 2})::Array{Float32, 1}
298
  end
299
  elseif tree.degree == 1
300
  cumulator = evalTreeArray(tree.l, cX)
 
 
 
301
  op = unaops[tree.op]
302
- @inbounds for i=1:clen
303
  cumulator[i] = op(cumulator[i])
304
  end
 
 
 
 
 
305
  return cumulator
306
  else
307
  op = binops[tree.op]
308
  cumulator = evalTreeArray(tree.l, cX)
 
 
 
309
  array2 = evalTreeArray(tree.r, cX)
310
- @inbounds for i=1:clen
 
 
 
 
311
  cumulator[i] = op(cumulator[i], array2[i])
312
  end
 
 
 
 
 
313
  return cumulator
314
  end
315
  end
316
 
317
  # Score an equation
318
  function scoreFunc(tree::Node)::Float32
319
- prediction = try
320
- evalTreeArray(tree)
321
- catch error
322
- if isa(error, DomainError) || isa(error, LoadError) || isa(error, TaskFailedException)
 
 
 
 
323
  return 1f9
324
- else
325
- throw(error)
326
  end
327
  end
328
  if weighted
@@ -338,13 +355,14 @@ function scoreFuncBatch(tree::Node)::Float32
338
  # batchSize
339
  batch_idx = randperm(len)[1:batchSize]
340
  batch_X = X[batch_idx, :]
341
- prediction = try
342
- evalTreeArray(tree, batch_X)
343
- catch error
344
- if isa(error, DomainError) || isa(error, LoadError) || isa(error, TaskFailedException)
 
 
 
345
  return 1f9
346
- else
347
- throw(error)
348
  end
349
  end
350
  size_adjustment = 1f0
 
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)
 
277
  end
278
 
279
  # Evaluate an equation over an array of datapoints
280
+ function evalTreeArray(tree::Node)::Union{Array{Float32, 1}, Nothing}
281
+ return evalTreeArray(tree, X)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
282
  end
283
 
284
 
285
  # Evaluate an equation over an array of datapoints
286
+ function evalTreeArray(tree::Node, cX::Array{Float32, 2})::Union{Array{Float32, 1}, Nothing}
287
  clen = size(cX)[1]
288
  if tree.degree == 0
289
  if tree.constant
 
293
  end
294
  elseif tree.degree == 1
295
  cumulator = evalTreeArray(tree.l, cX)
296
+ if cumulator == nothing
297
+ return nothing
298
+ end
299
  op = unaops[tree.op]
300
+ @inbounds @simd for i=1:clen
301
  cumulator[i] = op(cumulator[i])
302
  end
303
+ @inbounds for i=1:clen
304
+ if isinf(cumulator[i])
305
+ return nothing
306
+ end
307
+ end
308
  return cumulator
309
  else
310
  op = binops[tree.op]
311
  cumulator = evalTreeArray(tree.l, cX)
312
+ if cumulator == nothing
313
+ return nothing
314
+ end
315
  array2 = evalTreeArray(tree.r, cX)
316
+ if array2 == nothing
317
+ return nothing
318
+ end
319
+
320
+ @inbounds @simd for i=1:clen
321
  cumulator[i] = op(cumulator[i], array2[i])
322
  end
323
+ @inbounds for i=1:clen
324
+ if isinf(cumulator[i])
325
+ return nothing
326
+ end
327
+ end
328
  return cumulator
329
  end
330
  end
331
 
332
  # Score an equation
333
  function scoreFunc(tree::Node)::Float32
334
+ prediction = evalTreeArray(tree)
335
+ if prediction == nothing
336
+ return 1f9
337
+ end
338
+ for i=1:len
339
+ # Do this when using fastmath, to check for domain errors
340
+ # TODO: Is this needed?
341
+ if prediction[i] > 1f20 || prediction[i] < -1f20
342
  return 1f9
 
 
343
  end
344
  end
345
  if weighted
 
355
  # batchSize
356
  batch_idx = randperm(len)[1:batchSize]
357
  batch_X = X[batch_idx, :]
358
+ prediction = evalTreeArray(tree, batch_X)
359
+ if prediction == nothing
360
+ return 1f9
361
+ end
362
+ for i=1:batchSize
363
+ # Do this when using fastmath, to check for domain errors
364
+ if prediction[i] > 1f20 || prediction[i] < -1f20
365
  return 1f9
 
 
366
  end
367
  end
368
  size_adjustment = 1f0