How can i fix this error?

1 次查看(过去 30 天)
I am trying to solve the gradient of the logistic regression function using the following formula but getting the error "MATRIX DIMENSIONS MUST AGREE".
gradf =(-Y*exp(-Y.*X.'*w).*X)./1+exp(-Y.*X.'*w);
how to fix the error?
  2 个评论
Walter Roberson
Walter Roberson 2021-5-4
What is size(X) ? What is size(Y) ? What is size(w) ?
Why are you dividing by 1 ? Does your actual code have () around the 1+ ?
Why are you calculating exp(-Y.*X.'*w) twice instead of calculating it once and assigning to a variable and using the variable twice?
Hp2609
Hp2609 2021-5-4
size(X)=5000*784
size(Y)=5000*1
size(w)=784*1
I am calculating twice because its the derivative for the sigmoid function.

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2021-5-4
size(X)=5000*784
size(Y)=5000*1
size(w)=784*1
OKay, so Y.*X.' would be (5000 x 1) .* (784 x 5000) which would be an error.
If you just happened to instead have
Y.*(X*w)
then that would be (5000 x 1) .* (5000 x 784 * 784 x 1) -> (5000 x 1) .* (5000 x 1) -> 5000 x 1
I am calculating twice because its the derivative for the sigmoid function.
So?
If you had
gradf =(-Y*exp(-Y.*X.'*w).*X)./1+exp(-Y.*X.'*w);
and supposing that was correct code, then
temp = exp(-Y.*X.'*w);
gradf = (-Y*temp.*X)./1 + temp
However since division by 1 is mostly useless, I suspect you are thinking of
temp = exp(-Y.*X.'*w);
gradf = (-Y*temp.*X)./(1 + temp)

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Multiobjective Optimization 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by