Caffe2 - C++ API
A deep learning, cross platform ML framework
is_empty_op.cc
1 #include "is_empty_op.h"
2 namespace caffe2 {
3 
4 REGISTER_CPU_OPERATOR(IsEmpty, IsEmptyOp<CPUContext>);
5 
6 OPERATOR_SCHEMA(IsEmpty)
7  .NumInputs(1)
8  .NumOutputs(1)
9  .SetDoc(R"DOC(
10 The *IsEmpty* op accepts a single input $tensor$, and produces a single boolean output $is\_empty$. The output is *True* if and only if $tensor$ has size == 0.
11 
12 Github Links:
13 
14 - https://github.com/caffe2/caffe2/blob/master/caffe2/operators/utility_ops.cc
15 - https://github.com/caffe2/caffe2/blob/master/caffe2/operators/utility_ops.h
16 
17 
18 <details>
19 
20 <summary> <b>Example</b> </summary>
21 
22 **Code**
23 
24 ```
25 
26 workspace.ResetWorkspace()
27 
28 op = core.CreateOperator(
29  "IsEmpty",
30  ["tensor"],
31  ["is_empty"],
32 )
33 
34 // Use a not-empty tensor
35 workspace.FeedBlob("tensor", np.random.randn(2, 2).astype(np.float32))
36 print("tensor:\n", workspace.FetchBlob("tensor"))
37 
38 workspace.RunOperatorOnce(op)
39 print("is_empty: ", workspace.FetchBlob("is_empty"),"\n")
40 
41 // Use an empty tensor
42 workspace.FeedBlob("tensor", np.empty(0))
43 print("tensor:\n", workspace.FetchBlob("tensor"))
44 
45 workspace.RunOperatorOnce(op)
46 print("is_empty: ", workspace.FetchBlob("is_empty"))
47 
48 ```
49 
50 **Result**
51 
52 ```
53 
54 tensor:
55  [[ 0.26018378 0.6778789 ]
56  [-1.3097627 -0.40083608]]
57 is_empty: False
58 
59 tensor:
60  []
61 is_empty: True
62 
63 ```
64 
65 </details>
66 
67 )DOC")
68  .ScalarType(::caffe2::TensorProto_DataType::TensorProto_DataType_BOOL)
69  .Input(0, "tensor", "Input data tensor to check if empty.")
70  .Output(
71  0,
72  "is_empty",
73  "Output scalar boolean tensor. True if input has size == 0.");
74 
75 } // namespace caffe2
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13