如何从Octave中的Andrew Ng赋值编写成本函数公式? [英] How to write cost function formula from Andrew Ng assignment in Octave?

查看:118
本文介绍了如何从Octave中的Andrew Ng赋值编写成本函数公式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的实现(见下文)给出了标量值3.18,这不是正确的答案.该值应为0.693.我的代码在哪里偏离方程式?

My implementation (see below) gives the scalar value 3.18, which is not the right answer. The value should be 0.693. Where does my code deviate from the equation?

以下是解决数据以运行Octave中的成本函数方法的说明:

Here are the instructions to solve for the data to run the cost function method in Octave:

data = load('ex2data1.txt');
X = data(:, [1, 2]); y = data(:, 3);
[m, n] = size(X);
X = [ones(m, 1) X];
initial_theta = zeros(n + 1, 1);
[cost, grad] = costFunction(initial_theta, X, y);

这是ex2data上的链接,在此软件包中,有数据:

Here is the link on ex2data, in this package there is data: data link.

成本函数的公式为

这是我正在使用的代码:

Here is the code I am using:

function [J, grad] = costFunction(theta, X, y)

m = length(y); % number of training examples

% You need to return the following variables correctly 
J = 0; %#ok<NASGU>
grad = zeros(size(theta)); %#ok<NASGU>

hx = sigmoid(X * theta)';
m = length(X);

J = sum(-y' * log(hx) - (1 - y')*log(1 - hx)) / m;

grad = X' * (hx - y) / m;

end

这里是S型函数:

function g = sigmoid(z)
g = 1/(1+exp(-z));
end

推荐答案

您的sigmoid函数不正确.输入的数据类型是向量,但是您正在使用的操作正在执行矩阵除法.这需要是明智的.

Your sigmoid function is incorrect. The incoming data type is a vector but the operations you are using are performing matrix division. This needs to be element-wise.

function g = sigmoid(z)
    g = 1.0 ./ (1.0 + exp(-z));
end

通过执行1 / A,其中A是一个表达式,实际上是在计算A inverse .由于逆仅存在于平方矩阵中,因此将计算出伪逆,即绝对不是您想要的.

By doing 1 / A where A is an expression, you are in fact compute the inverse of A Since inverses only exist for square matrices, this will compute the pseudo-inverse which is definitely not what you want.

您可以将大多数costFunction代码保持与使用点乘积相同.我将摆脱sum,因为这是点积所隐含的.我将在注释中标记我的更改:

You can keep most of your costFunction code the same as you're using the dot product. I would get rid of the sum since that is implied with the dot product. I'll mark my changes with comments:

function [J, grad] = costFunction(theta, X, y)

m = length(y); % number of training examples

% You need to return the following variables correctly 
%J = 0; %#ok<NASGU> <-- Don't need to declare this as you'll create the variables later
%grad = zeros(size(theta)); %#ok<NASGU>

hx = sigmoid(X * theta);  % <-- Remove transpose
m = length(X);

J = (-y' * log(hx) - (1 - y')*log(1 - hx)) / m; % <-- Remove sum

grad = X' * (hx - y) / m;

end

这篇关于如何从Octave中的Andrew Ng赋值编写成本函数公式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆