Sum in function object
18 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Stefan Nastase
el 29 de Mayo de 2021
Editada: Star Strider
el 29 de Mayo de 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.
0 comentarios
Respuesta aceptada
Star Strider
el 29 de Mayo de 2021
Editada: Star Strider
el 29 de Mayo de 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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Entering Commands en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!