2016-07-31 876 views
-2

我正在学习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 

回答

0

^适用于矩阵中的每个元素。/才不是。 ./可能(虽然你可能需要使1的一个1的矩阵)

0

你需要使用循环将sigmoid函数应用到矢量或矩阵的每个元素。

2

正确答案

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. 

回答您为师和第一功能EXP第二个函数问题

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

你已经错过了点运算符()()。

2

您可能想要尝试的是利用元素操作(来自Octave官方文档here的更多信息)。

注意与元素的操作:

当你有相同尺寸的两个矩阵,则可以通过元素的操作对他们

因此,作为自定义的G和Z执行元件是下面的代码应该返回Sigmoid函数。

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

所以基本上,它确实有两个简单的事情。首先,它将零点矩阵或标量转换为一个“1”。然后它将每个元素除以每个对应元素的(1 + e z)。