필터 지우기
필터 지우기

dlgradient: Error Value to differentiate must be a traced dlarray scalar.

조회 수: 67 (최근 30일)
I want to train a deep network by Automatic Differentiation. Is these any solution?
layer2 = [
imageInputLayer([9 36 1],'Normalization','none','Name','input1-fcc')
convolution2dLayer([7,7],64,'Name','conv1-fcc')
batchNormalizationLayer('Name','bn1-fcc')
reluLayer('Name','relu1-fcc')
globalAveragePooling2dLayer('Name','pool5-fcc')
fullyConnectedLayer(1,'Name','fc1')];
lgraph = layerGraph(layer2);
dlnet = dlnetwork(lgraph);
% Input
a = rand(9,36,1,10);
a = dlarray(a,'SSCB');
a_pre = forward(dlnet,a);
% output
b = rand(1,10);
loss = mse(a_pre,b);
gradients = dlgradient(loss,dlnet.Learnables);

채택된 답변

Anshika Chaurasia
Anshika Chaurasia 2021년 1월 18일
Hi Qi Lu,
You can try following code to compute gradients that will resolve your error:
layer2 = [
imageInputLayer([9 36 1],'Normalization','none','Name','input1-fcc')
convolution2dLayer([7,7],64,'Name','conv1-fcc')
batchNormalizationLayer('Name','bn1-fcc')
reluLayer('Name','relu1-fcc')
globalAveragePooling2dLayer('Name','pool5-fcc')
fullyConnectedLayer(1,'Name','fc1')];
lgraph = layerGraph(layer2);
dlnet = dlnetwork(lgraph);
% Input
a = rand(9,36,1,10);
a = dlarray(a,'SSCB');
[loss,gradients] = dlfeval(@compute_gradient,dlnet,a);
function [loss,gradients]=compute_gradient(dlnet,a)
a_pre = forward(dlnet,a);
% output
b = rand(1,10);
loss = mse(a_pre,b);
gradients = dlgradient(dlarray(loss),dlnet.Learnables);%automatic gradient
end
Refer to the following documentation for more information on Automatic Differentiation.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Custom Training Loops에 대해 자세히 알아보기

태그

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by