简体   繁体   English

计算sigmoid函数

[英]Calculate the sigmoid function

I am learning about machine learning from coursera.我正在从coursera学习机器学习。 I am trying to calculate the sigmoid function and i have the below code:我正在尝试计算 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

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

in octave it looks like在八度它看起来像

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

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() .您在除法的第二个函数和 exp() 的第一个函数中遗漏了点运算符(.)。

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 和 z 大小相同,下面的代码应该返回 Sigmoid 函数。

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".首先,它将零矩阵或标量转换为一个“1”。 Then it divides each element with (1 + e z ) for each corresponding element.然后它用 (1 + e z ) 为每个对应元素划分每个元素。

.^ 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) ./ 可能(尽管您可能需要制作一些 1 的 1 矩阵)

您需要使用 for 循环将 sigmoid 函数应用于向量或矩阵的每个元素。

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.在后一种情况下,您试图将多维矩阵与 1 相乘(除法的逆),1 是一个逐一矩阵。 So it will result in a 'Matrix dimensions must agree' error for a matrix having more than one column.因此,对于具有多于一列的矩阵,它会导致“矩阵维度必须一致”错误。

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

Following is the implementation in Octave:以下是 Octave 中的实现:

Please add following code in file name sigmoid.m请在文件名sigmoid.m添加以下代码

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

Following is Vector example from above implementation:以下是上述实现中的Vector示例:

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

   1
   2
   3

>> sigmoid(A)
ans =

   0.73106
   0.88080
   0.95257

Following is Scalar example from above implementation:以下是上述实现的Scalar示例:

>> sigmoid(0)
ans =  0.50000

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.将 1 变成一个维度为 z/g 的数组,然后计算 sigmoid。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM