I can`t solve out this problem, there is always Output argument "varargout{2}" (and possibly others) not assigned a value in the execution with "dlarray/dlgradient" function.

4 次查看(过去 30 天)
function [netG, stateG, lossG] = modelGStep(netG, wrappedImage, realImage, stateG, learningRate, beta1, beta2)
% insure GPU dlarray
if ~isa(wrappedImage, 'dlarray')
wrappedImage = dlarray(gpuArray(wrappedImage), 'SSCB');
elseif ~strcmp(underlyingType(wrappedImage), 'gpuArray')
wrappedImage = dlarray(gpuArray(extractdata(wrappedImage)), 'SSCB');
end
if ~isa(realImage, 'dlarray')
realImage = dlarray(gpuArray(realImage), 'SSCB');
elseif ~strcmp(underlyingType(realImage), 'gpuArray')
realImage = dlarray(gpuArray(extractdata(realImage)), 'SSCB');
end
wrappedImage = dlarray(gpuArray(wrappedImage), 'SSCB');
realImage = dlarray(gpuArray(realImage), 'SSCB');
% insure dlfeval use dlgradient
[gradG, lossG] = dlfeval(@dlgradient, lossG, netG.Learnables);
fakeImage = predict(netG, wrappedImage);
lossG = mean((fakeImage - realImage).^2, 'all');
[gradG, lossG] = dlgradient(lossG, netG.Learnables);
[netG, stateG] = adamupdate(netG, gradG, stateG, learningRate, beta1, beta2);
return
end
  • this is my function.
  • below is my code
for epoch = 1:epochs
for i = 1:size(unwrapImages, 4)
realImage = unwrapImages(:,:,:,i);
wrappedImage = wrappedImages(:,:,:,i);
[netG, stateG, lossG] = modelGStep(netG, wrappedImage, realImage, stateG, learningRate, beta1, beta2);
[lossD, gradD] = modelDStep(netD, realImage, wrappedImage, netG);
[netD, stateD] = adamupdate(netD, gradD, stateD, learningRate, beta1, beta2);
gLosses(epoch) = gLosses(epoch) + double(gather(extractdata(lossG)));
dLosses(epoch) = dLosses(epoch) + double(gather(extractdata(lossD)));
end
gLosses(epoch) = gLosses(epoch) / size(unwrapImages, 4);
dLosses(epoch) = dLosses(epoch) / size(unwrapImages, 4);
fprintf('Epoch %d, Generator Loss: %.4f, Discriminator Loss: %.4f\n', ...
epoch, gLosses(epoch), dLosses(epoch));
end
what should i do to solve this,thanks!

采纳的回答

Joss Knight
Joss Knight 2024-4-18

Your mistake is dlfeval(@dlgradient,...). You need to put your code that computes the loss and the loss gradients into a function and then pass that to dlfeval.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Time Series Objects 的更多信息

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by