forked from omrysendik/DCor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CalcACorrErrorLoss.m
31 lines (23 loc) · 1.15 KB
/
CalcACorrErrorLoss.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
function [ACorrErrorLoss, ACorrGrads] = CalcACorrErrorLoss(TargetFeatures, SourceACorrs, params)
numFeatLayers = length(params.ACorrMatchLayerInds);
ACorrGrads = cell(numFeatLayers,1);
ACorrErrorLoss = 0;
for k=1:numFeatLayers
TargetACorr = CalcACorrTensor(TargetFeatures{k});
SourceACorr = SourceACorrs{k};
ACorrDiff = TargetACorr-SourceACorr;
ACorrDiffWeight = params.ACorrFeatureWeights(k);
ACorrErrorLoss = ACorrErrorLoss+ACorrDiffWeight*(sum((ACorrDiff(:)).^2));
ACorrGrads{k} = ACorrDiffWeight*CalcACorrGrad(TargetFeatures{k},ACorrDiff);
end
end
function [ACorrGrad] = CalcACorrGrad(featMat,ACorrDiff)
NumOfFeatures = size(featMat,3);
SizeOfFeatures = [size(featMat,1) size(featMat,2)];
ACorrGrad = zeros(size(featMat));
WeightsOutX = [round(size(featMat,1)/2):size(featMat,1) size(featMat,1)-1:-1:round(size(featMat,1)/2)];
[WeightsOutX,WeightsOutY] = meshgrid(WeightsOutX,WeightsOutX);
for IndN = 1:NumOfFeatures
ACorrGrad(:,:,IndN) = 4*conv2(double(flipud(fliplr(ACorrDiff(:,:,IndN))))./(WeightsOutX.*WeightsOutY),double(featMat(:,:,IndN)),'same');
end
end