Question

I am trying to write a code for classification of data. I try to implement a sigmoid function and then I try to use that function in calculation the cost.I keep getting errors and I have a feeling that it is because of the sigmoid function.I would like the sigmoid function to return a vector.But it keeps returning a scalar.

function g = sigmoid(z)
%SIGMOID Compute sigmoid functoon
%   J = SIGMOID(z) computes the sigmoid of z.

% You need to return the following variables correctly 

g=zeros(size(z));
m=ones(size(z));
% ====================== YOUR CODE HERE ======================
% Instructions: Compute the sigmoid of each value of z (z can be a matrix,
%               vector or scalar).


g=1/(m+exp(-z));

This is my cost function:

m = length(y); % number of training examples

% You need to return the following variables correctly 
grad=(1/m)*((X*(sigmoid(X*theta)-y)));//this is the derivative in gradient descent
J=(1/m)*(-(transpose(y)*log(sigmoid((X*theta))))-(transpose(1-y)*log(sigmoid((X*theta)))));//this is the cost function

the dimension of X are 100,4; of theta are 4,1;y is 100,1.

THank you.

Errors:

Program paused. Press enter to continue.

 sigmoid answer: 0.500000Error using  - 
Matrix dimensions must agree.

Error in costFunction (line 11)
grad=(1/m)*((X*(sigmoid(X*theta)-y)));

Error in ex2 (line 69)
[cost, grad] = costFunction(initial_theta, X, y);
Was it helpful?

Solution

Please replace g=1/(m+exp(-z)); with g=1./(m+exp(-z)); in your method sigmoid

z = [2,3,4;5,6,7] ;
%SIGMOID Compute sigmoid functoon
%   J = SIGMOID(z) computes the sigmoid of z.

% You need to return the following variables correctly 

g=zeros(size(z));
m=ones(size(z));
% ====================== YOUR CODE HERE ======================
% Instructions: Compute the sigmoid of each value of z (z can be a matrix,
%               vector or scalar).


g=1./(m+exp(-z));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top