Sum in function object

18 vues (au cours des 30 derniers jours)
Stefan Nastase
Stefan Nastase le 29 Mai 2021
Modifié(e) : Star Strider le 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.

Réponse acceptée

Star Strider
Star Strider le 29 Mai 2021
Modifié(e) : Star Strider le 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.
  1 commentaire
Stefan Nastase
Stefan Nastase le 29 Mai 2021
Thank you!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Entering Commands dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by