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
optimize
theinitial_theta
variable should be a vector not a scalar. – Bogumił Kamińskidata = load('data.txt');
X = data(:, [1, 2]); y = data(:, 3);
[m, n] = size(X);
X = [ones(m, 1) X];
initial_theta = zeros(n + 1, 1);
function [J, grad] = costFunction(theta, X, y)
J = 0;
m = size(y,1);
grad = zeros(size(theta));
J = -sum( (y .* log(sigmoid(X*theta))) + ((1-y) .* log(1 - sigmoid(X*theta))) ) / m;
grad = (1/m) * (sum((sigmoid(X*theta)-y) .* X))';
end
options = optimset('GradObj', 'on', 'MaxIter', 400);
[theta, cost] = fminunc(@(t)(costFunction(t, X, y)), initial_theta, options);
fprintf('Cost:%f theta:%f \n',cost,theta);
– Nemachtiani