The gradient of mini batches

4 ビュー (過去 30 日間)
MAHSA YOUSEFI
MAHSA YOUSEFI 2020 年 11 月 23 日
コメント済み: Mahesh Taparia 2020 年 12 月 21 日
Hi there.
I need your confimation or rejection for this question...
In following code, if the minibatch size is h,
[grad,loss] = dlfeval(@modelGradients,dlnet,dlX_miniBatch,Y_miniBatch);
the grad is the average of gradients of loss over h samples? Does it calculate dradients automatically and at the end with:
grad = 1/h * sum_i=1:h (\nabla loss(y_i,yHat_i)) ??
Following this question, for computing the total loss and geadient (for a full batch), does we should take avarage of losses and averages of gradients (averaging with the number of batches, say 1000 batches each with h size)??

採用された回答

Mahesh Taparia
Mahesh Taparia 2020 年 12 月 14 日
Hi
The function dlfeval evaluate the custom deep learning models. The loss are calculated based on what has been defined in modelGradients function. So if you are calculating the average loss in this function, then it will return the averaged one. For example, consider this modelGradient function, it is calculating the average cross entropy loss, so it will return the average loss. The gradients are calculated with respect to the loss function defined in for the network.
  2 件のコメント
MAHSA YOUSEFI
MAHSA YOUSEFI 2020 年 12 月 19 日
In the example you mentioned, there is a mistake.
function [gradients, loss] = modelGradients(parameters, dlX, T)
% Forward data through the model function.
dlY = model(parameters,dlX);
% Compute loss.
loss = crossentropy(dlX,T);
% Compute gradients.
gradients = dlgradient(loss,parameters);
end
dlY must be feed to crossentropy!
Mahesh Taparia
Mahesh Taparia 2020 年 12 月 21 日
Yeah, crossentropy loss will be calculated between dlY and T. The documentation page will be updated.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSequence and Numeric Feature Data Workflows についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by