-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathl2Norm.m
More file actions
59 lines (51 loc) · 1.63 KB
/
l2Norm.m
File metadata and controls
59 lines (51 loc) · 1.63 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
function L2 = l2Norm(X, params)
%L2NORM Compute L2 norm on a grid.
% L2 = L2NORM(X) computes the L2 norm of the input array X
% with default parameters.
%
% L2 = L2NORM(X, Name=Value) specifies additional options using
% one or more name-value arguments:
%
% Reduction - Method for reducing the norm across batch.
% Options are 'mean', 'sum', or 'none'.
% The default value is 'mean'.
%
% SquareRoot - If false, returns the squared L2 norm.
% If true, returns the L2 norm. The default
% value is false.
%
% Normalize - If true, divides output by C*prod(S1, S2, ...).
% The default value is false.
%
% Input X must be a numeric array of size [S1, S2, ..., SD, C, B]
% where S1...SD are spatial dimensions, C is number of channels,
% and B is batch size.
%
% Example:
% B=2; C=1; S1=64; S2=64;
% X = randn(S1,S2,C,B);
% L2 = l2Norm(X);
% Copyright 2026 The MathWorks, Inc.
arguments
X dlarray {mustBeNumeric}
params.Reduction (1,1) string {mustBeMember(params.Reduction, {'mean', 'sum', 'none'})} = "mean"
params.SquareRoot (1,1) logical = false
params.Normalize (1,1) logical = false
end
sz = size(X);
B = sz(end);
% Reshape to [prod(S*C), B]
X = reshape(X, [], B);
L2 = sum(abs(X.^2), 1); % [1, B]
if params.SquareRoot
L2 = sqrt(L2);
end
if params.Reduction == "mean"
L2 = mean(L2);
elseif params.Reduction == "sum"
L2 = sum(L2);
end
if params.Normalize
L2 = L2 / prod(sz(1:end-1));
end
end