Caffe2 - C++ API
A deep learning, cross platform ML framework
sinusoid_position_encoding_op.cc
1 
17 #include "caffe2/operators/sinusoid_position_encoding_op.h"
18 
19 namespace caffe2 {
20 REGISTER_CPU_OPERATOR(
21  SinusoidPositionEncoding,
22  SinusoidPositionEncodingOp<CPUContext>);
23 
24 OPERATOR_SCHEMA(SinusoidPositionEncoding)
25  .NumInputs(1)
26  .NumOutputs(1)
27  .SetDoc(R"DOC(
28 Calculates a sinusoid position encoding tensor as described
29 in https://arxiv.org/abs/1706.03762. Takes a 2-D tensor
30 (of size M x K) of positions as input, the embedding size
31 as an argument, and outputs a position encoding tensor of
32 size (M x K x embedding_size). Here M is typically the max
33 sequence length and K is typically the batch size.
34 The input tensor must satisfy input[m, 0] == input[m, k] for all k.
35 
36 Encoded as amplitude * SIN(pos/alpha^(i/embedding_size)) if i is even,
37 else amplitude * COS(pos/alpha^(i/embedding_size)). Here, pos is the position,
38 alpha and amplitude are tuning parameters, i is the current dimension for
39 the embedding, and embedding_size is the number of total dimensions in
40 the embedding.
41 )DOC")
42  .Arg(
43  "embedding_size",
44  "Desired embedding size/number of dimensions -- defaults to 100")
45  .Arg("alpha", "Sinusoid tuning parameter -- defaults to 10000")
46  .Arg("amplitude", "Amplitude of Sin/Cos output")
47  .Input(0, "positions", "2-D tensor of positions to be encoded")
48  .Output(0, "output", "3-D tensor representing the positional encoding");
49 
50 } // namespace caffe2
Copyright (c) 2016-present, Facebook, Inc.