0

I am learning about machine learning from coursera. I am trying to calculate the sigmoid function and i have the below code:

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));

% ====================== YOUR CODE HERE ======================

% Instructions: Compute the sigmoid of each value of z (z can be a matrix,
%               vector or scalar).



g = (1 + exp(-1 * z)) .^ -1;
 g = 1/(1+ (1/exp(z)))


% my question is why the first g calculation works for matrix(say 100*2) however the second only works for (100*1) as both are trying to do the same this.


% =============================================================

end
desertnaut
  • 57,590
  • 26
  • 140
  • 166
user6658936
  • 111
  • 1
  • 3
  • 10

9 Answers9

8

Sigmoid function g(z)=1/(1+e^(-z))

in octave it looks like

g = 1./(1 + exp(-z));
Pouria Moosavi
  • 662
  • 7
  • 22
6

Correct answer

rt=-z;  %changing sign of z
rt=rt';  %transposing matrix

g=1./(1+e.^(rt));   %you need to use dot(.) while dividing and also while finding power to apply those operation for every element in the matrix.

Answer for your question

1.g = (1 + exp(-1 * z)) .^ -1;
2.g = 1/(1+ (1/exp(z)))           

you have missed dot operator(.) in second function for division and first function for exp() .

Ajith
  • 367
  • 2
  • 6
  • 15
3

What you may want to try is to leverage the element operations (more info from Octave official documentation here).

Note that with the element operations:

When you have two matrices of the same size, you can perform element by element operations on them

So as defined g and z are of the same size, the code below should return the Sigmoid function.

g = (g.+1)./(1 + e.^-z);

So essentially, it does 2 simple things. First, it turns the zeros matrix or scalar into one with ones "1". Then it divides each element with (1 + ez) for each corresponding element.

0

.^ works for each element in the matrix. / does not. ./ might (though you might need to make some of the 1's matrices of 1's)

Jeremy Kahan
  • 3,796
  • 1
  • 10
  • 23
0

you need to use for loops to apply sigmoid function to each element of vector or matrix.

Al X
  • 19
  • 6
0

In the latter case, you are trying to multiply (inverse of division) a multidimensional matrix with 1 which is literally a one-by-one matrix. So it will result in a 'Matrix dimensions must agree' error for a matrix having more than one column.

anothernode
  • 5,100
  • 13
  • 43
  • 62
Din Alamin
  • 13
  • 3
0
function g = sigmoid(z)
%SIGMOID Compute sigmoid function
%   g = SIGMOID(z) computes the sigmoid of z.

% You need to return the following variables correctly 
g = zeros(length(z),1);

for  i = 1:100,
  g(i) = 1/(1 + exp(-z(i)));

end
Adrian W
  • 4,563
  • 11
  • 38
  • 52
0

Following is the implementation in Octave:

Please add following code in file name sigmoid.m

function g = sigmoid(z)
g = 1 ./ (1+((e).^(-z)));
end

Following is Vector example from above implementation:

>> A = [1;2;3]
A =

   1
   2
   3

>> sigmoid(A)
ans =

   0.73106
   0.88080
   0.95257

Following is Scalar example from above implementation:

>> sigmoid(0)
ans =  0.50000
Anshul Singhal
  • 1,983
  • 20
  • 25
0

Well, you can make it like this:

g = ones(size(z)) ./ (ones(size(z)) + exp(-1 * z));

Turn 1 into a array with the dimension of z/g, then compute the sigmoid.