I am brand new to Scheme and am working on an assignment to implement stochastic gradient descent. So far I believe I have the structure of the program correct however my procedure that takes the derivative of a function f(x) is giving me some trouble. In my "try" loop at the bottom of the code I recursively call (try (func-eval guess))
where (func-eval guess)
calculates the next guess of my function's local minimal with the formula *x - alpha*f'(x)* where alpha = 0.1.
I seem to be getting an error when calculating the derivative... I am using Dr.Racket IDE and it has highlighted this following line as being problematic: (f (+ x dx))
... which is the second line in my local derivative procedure:
(define (local-minimal first-guess)
;A way to check a guess
(define(good-enough? val1 val2)
(<(abs(- val1 val2)) 0.00001))
; x_new = x_old - alpha*f'(x) /// f(x)=(x+1)^2+2 //// alpha = 0.1
(define (func-eval x)
(- x (* 0.1((derivative (+ 2(expt (+ x 1) 2)) 0.00001)x))))
(define (derivative f dx)
(lambda (x)
(/ (- (f (+ x dx)) (f x))
dx)))
; trys the guess
(define (try guess)
(if (good-enough? guess -1)
guess
(try (func-eval guess))))
(try first-guess))
I am getting an error saying:
application: not a procedure;
expected a procedure that can be applied to arguments
given: 3
arguments...:
-1.99999
Is this a syntax error? I thought that I would be able to say f(x+dx) by using (f (+ x dx))
.... does this mean that I need to put an operator before the f in those parenthesis?
The highlighting and error message are together telling you something useful: the thing derivative
is receiving as its first argument f
isn't a function, which it needs to be to called in (f (+ x dx))
. Where does the argument come from? We could run DrRacket's debugger, but here we can just look at the code -- the only place derivative
is called from is the first line of func-eval
, so that's where we must have passed a number instead of a function. Sure enough, (+ 2 (expt (+ x 1) 2))
(with x
bound) is just a number, and trying to apply this gives an error.
When calling derivative
, the first argument has to be a function . In the following procedure call, the expression in the first argument gets evaluated to a number, not a function:
(derivative (+ 2 (expt (+ x 1) 2)) 0.00001)
To fix it, pack the expression inside a lambda
, which makes it an actual function:
(derivative (lambda (x) (+ 2 (expt (+ x 1) 2))) 0.00001)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.