1
我想寫一個代碼分類的數據。我嘗試實現一個sigmoid函數,然後嘗試在計算成本時使用該函數。我不斷收到錯誤,並且我有一種感覺,那是因爲sigmoid函數。我希望sigmoid函數返回一個vector.But它不斷返回一個標量。Matlab錯誤錯誤使用 - 矩陣尺寸必須一致
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));
m=ones(size(z));
% ====================== YOUR CODE HERE ======================
% Instructions: Compute the sigmoid of each value of z (z can be a matrix,
% vector or scalar).
g=1/(m+exp(-z));
這是我的成本函數:
m = length(y); % number of training examples
% You need to return the following variables correctly
grad=(1/m)*((X*(sigmoid(X*theta)-y)));//this is the derivative in gradient descent
J=(1/m)*(-(transpose(y)*log(sigmoid((X*theta))))-(transpose(1-y)*log(sigmoid((X*theta)))));//this is the cost function
X的尺寸爲100,4; θ是4,1; y是100,1。
謝謝你。
錯誤:
Program paused. Press enter to continue.
sigmoid answer: 0.500000Error using -
Matrix dimensions must agree.
Error in costFunction (line 11)
grad=(1/m)*((X*(sigmoid(X*theta)-y)));
Error in ex2 (line 69)
[cost, grad] = costFunction(initial_theta, X, y);
它仍然給我相同的答案取代
g=1/(m+exp(-z));
與g=1./(m+exp(-z));
。 – LoveMeow我試過上面的代碼,它爲我工作。我試過標量,矢量和矩陣,沒有發現問題。 – User1551892
在我的成本函數和sigmoid中存在一個問題。所以我看到了你所做的改變的不同。謝謝 – LoveMeow