Wolfram.com
WolframAlpha.com
WolframCloud.com
Wolfram Language
Example Repository
Ready-to-use examples for the Wolfram Language
Primary Navigation
Categories
Astronomy
Audio Processing
Calculus
Cellular Automata
Chemistry
Complex Systems
Computer Science
Computer Vision
Control Systems
Creative Arts
Data Science
Engineering
Finance & Economics
Finite Element Method
Food & Nutrition
Geography
Geometry
Graphs & Networks
Image Processing
Life Sciences
Machine Learning
Mathematics
Optimization
Physics
Puzzles and Recreation
Quantum Computation
Signal Processing
Social Sciences
System Modeling
Text & Language Processing
Time-Related Computation
Video Processing
Visualization & Graphics
Alphabetical List
Submit a New Resource
Learn More about
Wolfram Language
Related Pages
Related Symbols
FunctionLayer
NetGraph
ChessboardDistance
ManhattanDistance
EuclideanDistance
BrayCurtisDistance
HammingDistance
Related Categories
Machine Learning
Create a Neural Network Layer from Commonly Used Distance Functions
Example Notebook
Open in Cloud
Download Notebook
Write a function to compute the distance between two vectors:
I
n
[
1
]
:
=
f
=
F
u
n
c
t
i
o
n
[
M
o
d
u
l
e
[
{
s
q
u
a
r
e
d
i
f
f
,
m
e
a
n
}
,
s
q
u
a
r
e
d
i
f
f
=
(
#
a
-
#
b
)
^
2
;
m
e
a
n
=
S
q
r
t
@
M
e
a
n
[
s
q
u
a
r
e
d
i
f
f
]
]
]
;
Apply the function to a set of vectors:
I
n
[
2
]
:
=
f
[
"
a
"
{
1
.
,
2
.
}
,
"
b
"
{
2
.
,
4
.
}
]
O
u
t
[
2
]
=
1
.
5
8
1
1
4
Use
F
u
n
c
t
i
o
n
L
a
y
e
r
to convert the function to a custom neural network layer:
I
n
[
3
]
:
=
n
e
t
=
F
u
n
c
t
i
o
n
L
a
y
e
r
[
f
]
O
u
t
[
3
]
=
F
u
n
c
t
i
o
n
L
a
y
e
r
a
:
a
r
r
a
y
b
:
a
r
r
a
y
O
u
t
p
u
t
:
a
r
r
a
y
Check that the layer gives the same values as the function:
I
n
[
4
]
:
=
n
e
t
[
"
a
"
{
1
.
,
2
.
}
,
"
b
"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
e
a
l
Create a normalization layer:
I
n
[
6
]
:
=
n
o
r
m
a
l
i
z
a
t
i
o
n
L
a
y
e
r
=
F
u
n
c
t
i
o
n
L
a
y
e
r
[
N
o
r
m
a
l
i
z
e
]
O
u
t
[
6
]
=
F
u
n
c
t
i
o
n
L
a
y
e
r
I
n
p
u
t
:
a
r
r
a
y
O
u
t
p
u
t
:
a
r
r
a
y
Check the corresponding
N
e
t
G
r
a
p
h
:
I
n
[
7
]
:
=
n
o
r
m
a
l
i
z
a
t
i
o
n
L
a
y
e
r
/
/
N
e
t
G
r
a
p
h
O
u
t
[
7
]
=
N
e
t
G
r
a
p
h
I
n
p
u
t
P
o
r
t
I
n
p
u
t
:
a
r
r
a
y
O
u
t
p
u
t
P
o
r
t
O
u
t
p
u
t
:
a
r
r
a
y
See Also
CosDiceLossLayer
LogSumExpLayer
Related Symbols
FunctionLayer
NetGraph
ChessboardDistance
ManhattanDistance
EuclideanDistance
BrayCurtisDistance
HammingDistance
Publisher Information
Contributed by:
Wolfram Staff