[julia-users] Re: Something wrong with Optim?
So I am not terribly sure why creating a closure should should mess with the scoping of the variables. I'm not sure this is the desired behaviour. Maybe there is a good reason. Clearly in this scenario, the compiler is confused by what z is. Its just that printing a variable without implicitly or explicitly declaring it first in a lexical scope seemed like a code smell to me. That was the first thought on debugging this. Regards - Avik On Wednesday, 29 April 2015 05:57:09 UTC+1, Pooya wrote: Ah! Thank you. I had not heard of closure before. Now, I have heard of it, but am not sure if I can completely understand it! I guess this might be worth being explained in the manual. One thing that is still kind of confusing is that in your example, z is defined after the closure is created until the end of that iteration. So, z is printed once below, but not the second time in the beginning of the second iteration! julia for i=1:10 if i=2; println(z); end z=2 g()=2z println(z) end 2 ERROR: z not defined in anonymous at no file:2 On Tuesday, April 28, 2015 at 10:17:17 PM UTC-4, Avik Sengupta wrote: Yes, sorry I jumped the gun. Thanks for clarifying. But it still does not have anything to do with Optim :) The problem is due to defining an inline function (line 43) that creates closure over the x_previous variable. To test this, just comment that line (and adjust the Optim.optimize call), the problem goes away. A simpler version of the code that fails is as follows: julia function f() for i=1:10 if i2; println(z); end z=2 g() = 2z end end f (generic function with 1 method) julia f() ERROR: z not defined in f at none:3 A fix to get it to work is to declare the variable at the start of your function. Similarly, adding a local x_previous at the top of your function makes it work correctly. Remember, variables in Julia are lexical in scope. julia function f() local z for i=1:10 if i2; println(z); end z=2 g()=2z end end f (generic function with 1 method) julia f() 2 2 2 2 2 2 2 2 On Wednesday, 29 April 2015 02:23:28 UTC+1, Pooya wrote: If you comment out lines 42-49, you will see that it works fine! On Tuesday, April 28, 2015 at 9:20:49 PM UTC-4, Pooya wrote: Thanks, but I think if iter 2 (line 21) makes sure that x_previous is defined in the previous iteration. Just to be clear, the condition to check here was g_norm g_norm_old, but I changed it to get there as early as the second iteration. On Tuesday, April 28, 2015 at 9:13:49 PM UTC-4, Avik Sengupta wrote: I'm seeing the error in line 22 of your gist where you are trying to print the current value of x_previous. However, x_previous is first defined in line 38 of your gist, and so the error is correct and doesnt have anything to do with Optim, as far as I can see. On Wednesday, 29 April 2015 01:39:02 UTC+1, Pooya wrote: Hi all, I have a problem that has made me scratch my head for many hours now! It might be something obvious that I am missing. I have a Newton-Raphson code to solve a system of nonlinear equations. The error that I get here does not have anything to do with the algorithm, but just to be clear, I need to find the best possible solution if the equations are not solvable, so I am trying to stop simulation when the direction found by Newton-Raphson is not correct! In order to do that I put an if-loop in the beginning of the main loop to take x from the previous iteration (x_previous), but I get x_previous not defined! I am using the Optim package to do a line search after the direction has been found by Newton-Raphson. If Optim is not used, things work perfectly (I tried by commenting out those lines of code). Otherwise I get the error I mentioned. My code is here: https://gist.github.com/prezaei85/372bde76012472865a94, which solves a simple one-variable quadratic equation. Any thoughts are very much appreciated. Thanks, Pooya
[julia-users] Re: Something wrong with Optim?
Thanks, but I think if iter 2 (line 21) makes sure that x_previous is defined in the previous iteration. Just to be clear, the condition to check here was g_norm g_norm_old, but I changed it to get there as early as the second iteration. On Tuesday, April 28, 2015 at 9:13:49 PM UTC-4, Avik Sengupta wrote: I'm seeing the error in line 22 of your gist where you are trying to print the current value of x_previous. However, x_previous is first defined in line 38 of your gist, and so the error is correct and doesnt have anything to do with Optim, as far as I can see. On Wednesday, 29 April 2015 01:39:02 UTC+1, Pooya wrote: Hi all, I have a problem that has made me scratch my head for many hours now! It might be something obvious that I am missing. I have a Newton-Raphson code to solve a system of nonlinear equations. The error that I get here does not have anything to do with the algorithm, but just to be clear, I need to find the best possible solution if the equations are not solvable, so I am trying to stop simulation when the direction found by Newton-Raphson is not correct! In order to do that I put an if-loop in the beginning of the main loop to take x from the previous iteration (x_previous), but I get x_previous not defined! I am using the Optim package to do a line search after the direction has been found by Newton-Raphson. If Optim is not used, things work perfectly (I tried by commenting out those lines of code). Otherwise I get the error I mentioned. My code is here: https://gist.github.com/prezaei85/372bde76012472865a94, which solves a simple one-variable quadratic equation. Any thoughts are very much appreciated. Thanks, Pooya
[julia-users] Re: Something wrong with Optim?
If you comment out lines 42-49, you will see that it works fine! On Tuesday, April 28, 2015 at 9:20:49 PM UTC-4, Pooya wrote: Thanks, but I think if iter 2 (line 21) makes sure that x_previous is defined in the previous iteration. Just to be clear, the condition to check here was g_norm g_norm_old, but I changed it to get there as early as the second iteration. On Tuesday, April 28, 2015 at 9:13:49 PM UTC-4, Avik Sengupta wrote: I'm seeing the error in line 22 of your gist where you are trying to print the current value of x_previous. However, x_previous is first defined in line 38 of your gist, and so the error is correct and doesnt have anything to do with Optim, as far as I can see. On Wednesday, 29 April 2015 01:39:02 UTC+1, Pooya wrote: Hi all, I have a problem that has made me scratch my head for many hours now! It might be something obvious that I am missing. I have a Newton-Raphson code to solve a system of nonlinear equations. The error that I get here does not have anything to do with the algorithm, but just to be clear, I need to find the best possible solution if the equations are not solvable, so I am trying to stop simulation when the direction found by Newton-Raphson is not correct! In order to do that I put an if-loop in the beginning of the main loop to take x from the previous iteration (x_previous), but I get x_previous not defined! I am using the Optim package to do a line search after the direction has been found by Newton-Raphson. If Optim is not used, things work perfectly (I tried by commenting out those lines of code). Otherwise I get the error I mentioned. My code is here: https://gist.github.com/prezaei85/372bde76012472865a94, which solves a simple one-variable quadratic equation. Any thoughts are very much appreciated. Thanks, Pooya
[julia-users] Re: Something wrong with Optim?
Yes, sorry I jumped the gun. Thanks for clarifying. But it still does not have anything to do with Optim :) The problem is due to defining an inline function (line 43) that creates closure over the x_previous variable. To test this, just comment that line (and adjust the Optim.optimize call), the problem goes away. A simpler version of the code that fails is as follows: julia function f() for i=1:10 if i2; println(z); end z=2 g() = 2z end end f (generic function with 1 method) julia f() ERROR: z not defined in f at none:3 A fix to get it to work is to declare the variable at the start of your function. Similarly, adding a local x_previous at the top of your function makes it work correctly. Remember, variables in Julia are lexical in scope. julia function f() local z for i=1:10 if i2; println(z); end z=2 g()=2z end end f (generic function with 1 method) julia f() 2 2 2 2 2 2 2 2 On Wednesday, 29 April 2015 02:23:28 UTC+1, Pooya wrote: If you comment out lines 42-49, you will see that it works fine! On Tuesday, April 28, 2015 at 9:20:49 PM UTC-4, Pooya wrote: Thanks, but I think if iter 2 (line 21) makes sure that x_previous is defined in the previous iteration. Just to be clear, the condition to check here was g_norm g_norm_old, but I changed it to get there as early as the second iteration. On Tuesday, April 28, 2015 at 9:13:49 PM UTC-4, Avik Sengupta wrote: I'm seeing the error in line 22 of your gist where you are trying to print the current value of x_previous. However, x_previous is first defined in line 38 of your gist, and so the error is correct and doesnt have anything to do with Optim, as far as I can see. On Wednesday, 29 April 2015 01:39:02 UTC+1, Pooya wrote: Hi all, I have a problem that has made me scratch my head for many hours now! It might be something obvious that I am missing. I have a Newton-Raphson code to solve a system of nonlinear equations. The error that I get here does not have anything to do with the algorithm, but just to be clear, I need to find the best possible solution if the equations are not solvable, so I am trying to stop simulation when the direction found by Newton-Raphson is not correct! In order to do that I put an if-loop in the beginning of the main loop to take x from the previous iteration (x_previous), but I get x_previous not defined! I am using the Optim package to do a line search after the direction has been found by Newton-Raphson. If Optim is not used, things work perfectly (I tried by commenting out those lines of code). Otherwise I get the error I mentioned. My code is here: https://gist.github.com/prezaei85/372bde76012472865a94, which solves a simple one-variable quadratic equation. Any thoughts are very much appreciated. Thanks, Pooya
[julia-users] Re: Something wrong with Optim?
I'm seeing the error in line 22 of your gist where you are trying to print the current value of x_previous. However, x_previous is first defined in line 38 of your gist, and so the error is correct and doesnt have anything to do with Optim, as far as I can see. On Wednesday, 29 April 2015 01:39:02 UTC+1, Pooya wrote: Hi all, I have a problem that has made me scratch my head for many hours now! It might be something obvious that I am missing. I have a Newton-Raphson code to solve a system of nonlinear equations. The error that I get here does not have anything to do with the algorithm, but just to be clear, I need to find the best possible solution if the equations are not solvable, so I am trying to stop simulation when the direction found by Newton-Raphson is not correct! In order to do that I put an if-loop in the beginning of the main loop to take x from the previous iteration (x_previous), but I get x_previous not defined! I am using the Optim package to do a line search after the direction has been found by Newton-Raphson. If Optim is not used, things work perfectly (I tried by commenting out those lines of code). Otherwise I get the error I mentioned. My code is here: https://gist.github.com/prezaei85/372bde76012472865a94, which solves a simple one-variable quadratic equation. Any thoughts are very much appreciated. Thanks, Pooya
[julia-users] Re: Something wrong with Optim?
Ah! Thank you. I had not heard of closure before. Now, I have heard of it, but am not sure if I can completely understand it! I guess this might be worth being explained in the manual. One thing that is still kind of confusing is that in your example, z is defined after the closure is created until the end of that iteration. So, z is printed once below, but not the second time in the beginning of the second iteration! julia for i=1:10 if i=2; println(z); end z=2 g()=2z println(z) end 2 ERROR: z not defined in anonymous at no file:2 On Tuesday, April 28, 2015 at 10:17:17 PM UTC-4, Avik Sengupta wrote: Yes, sorry I jumped the gun. Thanks for clarifying. But it still does not have anything to do with Optim :) The problem is due to defining an inline function (line 43) that creates closure over the x_previous variable. To test this, just comment that line (and adjust the Optim.optimize call), the problem goes away. A simpler version of the code that fails is as follows: julia function f() for i=1:10 if i2; println(z); end z=2 g() = 2z end end f (generic function with 1 method) julia f() ERROR: z not defined in f at none:3 A fix to get it to work is to declare the variable at the start of your function. Similarly, adding a local x_previous at the top of your function makes it work correctly. Remember, variables in Julia are lexical in scope. julia function f() local z for i=1:10 if i2; println(z); end z=2 g()=2z end end f (generic function with 1 method) julia f() 2 2 2 2 2 2 2 2 On Wednesday, 29 April 2015 02:23:28 UTC+1, Pooya wrote: If you comment out lines 42-49, you will see that it works fine! On Tuesday, April 28, 2015 at 9:20:49 PM UTC-4, Pooya wrote: Thanks, but I think if iter 2 (line 21) makes sure that x_previous is defined in the previous iteration. Just to be clear, the condition to check here was g_norm g_norm_old, but I changed it to get there as early as the second iteration. On Tuesday, April 28, 2015 at 9:13:49 PM UTC-4, Avik Sengupta wrote: I'm seeing the error in line 22 of your gist where you are trying to print the current value of x_previous. However, x_previous is first defined in line 38 of your gist, and so the error is correct and doesnt have anything to do with Optim, as far as I can see. On Wednesday, 29 April 2015 01:39:02 UTC+1, Pooya wrote: Hi all, I have a problem that has made me scratch my head for many hours now! It might be something obvious that I am missing. I have a Newton-Raphson code to solve a system of nonlinear equations. The error that I get here does not have anything to do with the algorithm, but just to be clear, I need to find the best possible solution if the equations are not solvable, so I am trying to stop simulation when the direction found by Newton-Raphson is not correct! In order to do that I put an if-loop in the beginning of the main loop to take x from the previous iteration (x_previous), but I get x_previous not defined! I am using the Optim package to do a line search after the direction has been found by Newton-Raphson. If Optim is not used, things work perfectly (I tried by commenting out those lines of code). Otherwise I get the error I mentioned. My code is here: https://gist.github.com/prezaei85/372bde76012472865a94, which solves a simple one-variable quadratic equation. Any thoughts are very much appreciated. Thanks, Pooya