11
$\begingroup$

so given that the sigmoid function is defined as hθ(x) = g(θ^(T)x), how can I implement this funcion in Octave given that g = zeros(size(z)) ?

$\endgroup$
8
$\begingroup$

This will compute the sigmoid of a scalar, vector or matrix.

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


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

SIGMOID = @(z) 1./(1 + exp(-z));

g = SIGMOID(z);

end
$\endgroup$
3
  • $\begingroup$ Was using '/' in place of './'. My very very bad. $\endgroup$ Jun 17 '18 at 7:26
  • 6
    $\begingroup$ You could just use g = 1 ./ (1 + exp(-z)); instead of creating this SIGMOID inside the sigmoid function. $\endgroup$
    – Alisson
    Oct 11 '18 at 3:01
  • $\begingroup$ g= 1./ (1 + exp(-z)); careful about ./ divide had (dot sign) & parentheses of (1 + ...) is mandatory here, I spend mins debugging that. $\endgroup$ May 28 at 13:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.