Caffe2 - C++ API
A deep learning, cross platform ML framework
piecewise_linear_transform_op.cc
1 
17 #include "caffe2/operators/piecewise_linear_transform_op.h"
18 
19 namespace caffe2 {
20 
21 REGISTER_CPU_OPERATOR(
22  PiecewiseLinearTransform,
23  PiecewiseLinearTransformOp<float, CPUContext>);
24 
25 OPERATOR_SCHEMA(PiecewiseLinearTransform)
26  .NumInputs(1, 4)
27  .NumOutputs(1)
28  .SetDoc(R"DOC(
29 PiecewiseLinearTransform takes inputs -- predictions, a 2-D or 1-D tensor
30 (Tensor<float>) of size (batch_size x prediction_dimensions). The piecewise
31 linear functions are stored in bounds, slopes and intercepts. The output tensor
32 has the same shape of input `predictions` and contains the predictions
33 transformed by the piecewise linear functions. Each column of predictions has
34 its own piecewise linear transformation functions. Therefore the size of
35 piecewise function parameters are pieces x prediction_dimensions, except for
36 binary predictions where only the positive prediction needs them. Note that in
37 each piece, low bound is excluded while high bound is included. Also the
38 piecewise linear function must be continuous.
39 
40 Notes
41 - If the input is binary predictions (Nx2 or Nx1 tensor), set the binary arg
42 to true so that one group of piecewise linear functions is needed (see
43 details below).
44 - The transform parameters (bounds, slopes, intercepts) can be passed either
45 through args or through input blobs.
46 - If we have multiple groups of piecewise linear functions, each group has the
47 same number of pieces.
48 - If a prediction is out of the bounds, it is capped to the smallest or largest
49 bound.
50 )DOC")
51  .Arg(
52  "bounds",
53  "1-D vector of size (prediction_dimensions x (pieces+1)) contain the "
54  "upper bounds of each piece of linear function. One special case is "
55  "the first bound is the lower bound of whole piecewise function and we "
56  "treat it the same as the left most functions. (bounds, slopes, "
57  "intercepts) can be passed through either arg or input blobs.")
58  .Arg(
59  "slopes",
60  "1-D vector of size (prediction_dimensions x pieces) containing the "
61  "slopes of linear function")
62  .Arg(
63  "intercepts",
64  "1-D vector of size (prediction_dimensions x pieces) containing the "
65  "intercepts of linear function")
66  .Arg(
67  "binary",
68  "If set true, we assume the input is a Nx1 or Nx2 tensor. If it is Nx1 "
69  "tensor, it is positive predictions. If the input is Nx2 tensor, its "
70  "first column is negative predictions and second column is positive "
71  "and negative + positive = 1. We just need one group of piecewise "
72  "linear functions for the positive predictions.")
73  .Input(
74  0,
75  "predictions",
76  "2-D tensor (Tensor<float>) of size "
77  "(num_batches x num_classes) containing scores")
78  .Input(
79  1,
80  "bounds (optional)",
81  "See bounds in Arg. (bounds, slopes, intercepts) can be passed through "
82  "either arg or input blobs.")
83  .Input(
84  2,
85  "slopes (optional)",
86  "See slopes in Arg. (bounds, slopes, intercepts) can be passed through "
87  "either arg or input blobs.")
88  .Input(
89  3,
90  "intercepts (optional)",
91  "See intercepts in Arg. (bounds, slopes, intercepts) can be passed "
92  "through either arg or input blobs.")
93  .Output(
94  0,
95  "transforms",
96  "2-D tensor (Tensor<float>) of size (num_batches x num_classes) "
97  "containing transformed predictions");
98 
99 SHOULD_NOT_DO_GRADIENT(PiecewiseLinearTransform);
100 } // namespace caffe2
Copyright (c) 2016-present, Facebook, Inc.