Sum in function object

18 Ansichten (letzte 30 Tage)
Stefan Nastase
Stefan Nastase am 29 Mai 2021
Bearbeitet: Star Strider am 29 Mai 2021
I am trying to write a function object for the next function:
but I can't seem to use the + operator when working with function objs.
Tried writing it like this:
loss = @(W, x, y) log(sum(W'*x)) - (y(10)*W(:,10)'*x + y(9)*W(:,9)'*x + y(8)*W(:,8)'*x + ...
y(7)*W(:,7)'*x + y(6)*W(:,6)'*x + y(5)*W(:,5)'*x+ y(4)*W(:,4)'*x + y(3)*W(:,3)'*x +y(2)*W(:,2)'*x + y(1)*W(:,1)'*x);
lossW = @(W) 0;
for i=1:n
lossW = @(W) lossW + loss(W, X(:, i), Y(i, :));
end
I need to minimize with respect to the W matrix and I can't find a way to do that.

Akzeptierte Antwort

Star Strider
Star Strider am 29 Mai 2021
Bearbeitet: Star Strider am 29 Mai 2021
It is not possible to operate on funciton handles themselves. It is necessary to evaluate the functions —
loss = @(W, x, y) log(sum(W'*x)) - (y(10)*W(:,10)'*x + y(9)*W(:,9)'*x + y(8)*W(:,8)'*x + ...
y(7)*W(:,7)'*x + y(6)*W(:,6)'*x + y(5)*W(:,5)'*x+ y(4)*W(:,4)'*x + y(3)*W(:,3)'*x +y(2)*W(:,2)'*x + y(1)*W(:,1)'*x);
for i=1:n
lossW = @(W) lossW(W) + loss(W, X(:, i), Y(i, :));
end
In the loop, ‘lossW’ now has an argument.
The probllem is that ‘lossW’ now re-defines and refers to itself. This is called function recursion and is to be avoided. This code will lilkely not work without some significant changes.

Weitere Antworten (0)

Kategorien

Mehr zu Entering Commands finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by