2016-07-31 884 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)。