How to create a custom loss function with a Riemann-sum approximation of the integral #358
Replies: 5 comments 1 reply
-
This looks okay to me. Some notes:
if i > 0:
cur_trapezoid_area = (cur_expr_val + prev_expr_val) * / (2.0 * num_rectangles)
norm_constant += cur_trapezoid_area you should write if i > 0
cur_trapezoid_area = (cur_expr_val + prev_expr_val) / (2.0 * num_rectangles)
norm_constant += cur_trapezoid_area
end (also there's an error in the expression) Similarly for the for loop.
|
Beta Was this translation helpful? Give feedback.
-
Hi, I just ran this objective function:
The printed output of these three prints (before prediction_on_evenly_spaced_numbers .* prediction_on_evenly_spaced_numbers)
is
Why is the "prediction_on_evenly_spaced_numbers" output of eval_tree_array(tree, evenly_spaced_numbers, options) only have 1 element? Thanks. |
Beta Was this translation helpful? Give feedback.
-
This is a common gotcha. In Julia, similar to Fortran, arrays are stored in column-major order. This means that columns come first, rows come second (i.e., functions operating on matrices usually expect data to come in this format). So for the function eval_tree_array(tree, X, options)
What you wrote is evenly_spaced_numbers = reshape([LinRange(0, 1, num_rectangles + 1);], num_rectangles + 1, 1) which looks to be (the backend should really try to automatically catch when users do this and throw an error.) |
Beta Was this translation helpful? Give feedback.
-
Oh okay, thanks for this suggestion. I fixed the order so now the code is "reshape([LinRange(0, 1, num_rectangles + 1);], 1, num_rectangles + 1)", but now the issue is that when I call model.fit(X, y), it takes forever (more than 10 minutes). This was the only change I did, but just for your reference, the objective function is now
What are some possible reasons why model.fit(X, y) takes forever? |
Beta Was this translation helpful? Give feedback.
-
Not sure I 100% understand the question but a few things look to be slowing it down:
|
Beta Was this translation helpful? Give feedback.
-
Hi,
I am creating a custom objective function and I want to calculate an approximation of the value of the integral of the guessed function over the domain x=0 to x=1.
Purpose of doing this is so that I can scale my function's values by this amount so that the definite integral of the re-scaled guessed function over the domain x=0 to x=1 becomes 1.
Assume there is only one independent variable, x.
Is this close to how you would approximate the definite integral of the guessed function (the guessed function is represented by the tree "tree" here) in a custom Julia objective function?
Thank you, very much appreciated.
Beta Was this translation helpful? Give feedback.
All reactions