This is a simple C++ program that allows users to perform gradient descent on functions with either 2 or 3 parameters. The user can input the number of parameters, learning rate, and other required values to start the gradient descent computation.
To compile the program, run the following command: g++ main.cpp -o gradient_descent
After compiling, you can run the program by typing: ./gradient_descent
The program will ask you to enter several values:
- Number of parameters: Enter
2for a function with two parameters or3for a function with three parameters. - Learning rate: Enter the learning rate for the gradient descent (e.g.,
0.01). - Maximum number of iterations: Enter the maximum number of iterations you want the gradient descent algorithm to run (e.g.,
1000). - Tolerance: Enter the stopping tolerance for the gradient magnitude (e.g.,
1e-6).
Enter the number of parameters (2 or 3): 2 Enter the learning rate (e.g., 0.01): 0.01 Enter the maximum number of iterations: 100 Enter the tolerance for stopping (e.g., 1e-6): 1e-6 Iteration 1: Function value = 1.98 Iteration 2: Function value = 1.9602 ... Converged after 50 iterations. Optimized parameters: 0.01 0.01
You can replace the provided function definitions in the code with your own functions:
- For 2 parameters, modify the
function2Paramsfunction. - For 3 parameters, modify the
function3Paramsfunction.
- The program uses a simple quadratic function as an example (
x^2 + y^2for 2 parameters, andx^2 + y^2 + z^2for 3 parameters). - You can replace these with any other differentiable function to perform gradient descent on your custom functions.
- The initial parameter values are set to
1.0, but you can modify this in the code.
Enjoy experimenting with different functions and parameters!