I am passing my code from octave to julia, in this case a logistic regression. The gradient function takes in addition to the initial theta, X with my features and Y with the sought values.
in Octave works
function [J, grad] = costFunction(theta, X, y)
options = optimset('GradObj', 'on', 'MaxIter', 400);
[theta, cost] = fminunc(@(t)(costFunction(t, X, y)), initial_theta, options);
on Julia first try
optimize(t->CostFunction(t, X1, y), initial_theta, BFGS())
MethodError: no method matching -(::Tuple{Float64,Array{Float64,2}}, ::Tuple{Float64,Array{Float64,2}})
so I separated the function in two: Cost and Gradient
function CostFunction2(theta, X, y)
J = 0;
#m = length(y);
m = size(y,1);
grad = zeros(size(theta));
J = 1/m * sum( (-y .* log.(sigmoid(X*theta))) - ((1.0 .- y) .* log.(1.0 .- sigmoid(X*theta))) );
return J;
end
function Gradient2(X, y, theta)
grad = zeros(size(theta));
grad = (1/m) .* (sum((sigmoid(X*theta).-y) .* X, dims=1))';
return grad;
end
I put only the cost function and it worked, but I don't have the last values of theta. I don't know how to get it
optimize(t->CostFunction2(t, X1, y), initial_theta, BFGS())
I tried this but it didn't work, and I can't find any reference that says how or brings an example
optimize(t->CostFunction2(t, X1, y), Gradient2(X, y, t), initial_theta, BFGS())
UndefVarError: t not defined
How can I obtain the obtained theta values? and How can I include my own gradient function with various parameters?
I hope you can help me, thank you very much