计算S形函数

use*_*936 -1 matlab machine-learning sigmoid

我正在从Coursera学习机器学习。我正在尝试计算sigmoid函数,我有以下代码:

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
Run Code Online (Sandbox Code Playgroud)

Aji*_*ith 6

正确答案

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.
Run Code Online (Sandbox Code Playgroud)

回答你的问题

1.g = (1 + exp(-1 * z)) .^ -1;
2.g = 1/(1+ (1/exp(z)))           
Run Code Online (Sandbox Code Playgroud)

您在除法的第二个函数和 exp() 的第一个函数中遗漏了点运算符(.)。


小智 5

S形函数g(z)= 1 /(1 + e ^(-z))

在八度看起来

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