How can i fix this error?

조회 수: 1 (최근 30일)
Hp2609
Hp2609 2021년 5월 4일
답변: Walter Roberson 2021년 5월 4일
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