I am getting a stack over flow error when using the optimize function from Optim package

42 views
Skip to first unread message

SajeelBongale

unread,
Oct 24, 2016, 3:07:46 AM10/24/16
to julia-opt
Hello, i am new to julia. 
I am trying to implement logistic regression as per some course on Machine learning. I am using the Atom editor for running Julia. 
However i am unable to find the right arguments for using the optimize function. 
I am sending my differentiable function which is the costFunction and initial_theta as the initial parameters. What other arguments are needed? 
Which optimizer function should be used? 

I am attaching my code here.

This is my cost function:
"This function calculates the cost and gradient at a given theta"
function costFunction( initial_theta,X, y)
  m = length(y);
  J = 0;
  grad = zeros(size(initial_theta));
  z = X * initial_theta;
  h_theta = sigmoid(z);
  J = (-1/m) * ( y .* log(h_theta) + (1-y) .* log(1 - h_theta) );
  grad = (1/m) * X' * ( h_theta - y );
  return J, grad;
end


This is the snippet from my main function:
l() = Optim.DifferentiableFunction(costFunction(initial_theta,X,y))
methodToUse = Optim.GradientDescent()
options = Optim.OptimizationOptions(iterations = 400)

optimize(l,initial_theta)                  #########          when i do this, i get a stack over flow error
optimize(l,initial_theta, methodToUse,options)            ########         with this i get a Method error : no method matching finite_difference

Let me know if any additional information is needed. 
Thanks!

Patrick Kofod Mogensen

unread,
Oct 24, 2016, 4:20:35 AM10/24/16
to julia-opt
First off, can you explain your thoughts behind "I() = ..." ? You are defining a function that takes no arguments and returns a DifferentiableFunction type instance. You should just write "df = Optim.DifferentiableFunction(costFunction(initial_theta,X,y))" and then pass "df" instead of "I".

SajeelBongale

unread,
Oct 24, 2016, 5:14:27 AM10/24/16
to julia-opt
Hello Patrick,
Thank you for your suggestion. I tried what you have mentioned above. When I do this,
df = Optim.DifferentiableFunction(costFunction(initial_theta,X,y))
it throws this error
MethodError: Cannot `convert` an object of type Array{Float64,2} to an object of type Optim.DifferentiableFunction
Reply all
Reply to author
Forward
0 new messages