mxnet
one_hot.h
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
26 #ifndef MSHADOW_EXTENSION_ONE_HOT_H_
27 #define MSHADOW_EXTENSION_ONE_HOT_H_
28 
29 #include "../extension.h"
30 
31 
32 namespace mshadow {
33 namespace expr {
39 template<typename IndexExp, typename DType>
41  public Exp<OneHotEncodeExp<IndexExp, DType>,
42  DType, type::kChainer> {
44  const IndexExp &index_;
48  OneHotEncodeExp(const IndexExp &index, index_t num_choices)
49  : index_(index), num_choices_(num_choices) {}
50 };
51 
52 template<typename IndexExp,
53  typename IDType, int e1>
55 one_hot_encode(const Exp<IndexExp, IDType, e1> &index, index_t num_choices) {
57  ::Error_Expression_Does_Not_Meet_Dimension_Req();
58  return OneHotEncodeExp<IndexExp, default_real_t>(index.self(), num_choices);
59 }
60 
61 //----------------------
62 // Execution plan
63 //----------------------
64 template<typename IndexExp, typename DType>
65 struct Plan<OneHotEncodeExp<IndexExp, DType>, DType> {
66  public:
68  : index_(MakePlan(e.index_)) {
69  }
70  MSHADOW_XINLINE DType Eval(index_t y, index_t x) const {
71  index_t idx = static_cast<index_t>(index_.Eval(0, y));
72  return static_cast<DType>(x == idx);
73  }
74 
75  private:
77 };
78 
79 template<typename IndexExp, typename DType>
82  return Plan<OneHotEncodeExp<IndexExp, DType>, DType>(exp);
83 }
84 
85 template<int dim, typename IndexExp, typename DType>
86 struct ShapeCheck<dim, OneHotEncodeExp<IndexExp, DType> > {
87  inline static Shape<dim>
89  CHECK(dim == 2)
90  << "OneHotEncodeExp only support 2 dimension output";
92  Shape<dim> ret;
93  ret[0] = shape[0];
94  ret[1] = t.num_choices_;
95  return ret;
96  }
97 };
98 
99 template<typename IndexExp, typename DType>
100 struct ExpInfo<OneHotEncodeExp<IndexExp, DType> > {
101  static const int kDim = 2;
102  static const int kDevMask = ExpInfo<IndexExp>::kDevMask;
103 };
104 } // namespace expr
105 } // namespace mshadow
106 #endif // MSHADOW_EXTENSION_ONE_HOT_H_
index_t num_choices_
number of choices we can have.
Definition: one_hot.h:46
Definition: expr_engine-inl.h:59
used to help static type check
Definition: expr_engine-inl.h:331
static Shape< dim > Check(const OneHotEncodeExp< IndexExp, DType > &t)
Definition: one_hot.h:88
Create a one-hot indicator array.
Definition: one_hot.h:40
static Shape< dim > Check(const E &t)
#define MSHADOW_XINLINE
Definition: base.h:223
static type inference template, used to get the dimension of each expression, if ExpInfo<E>::kDim == ...
Definition: expr_engine-inl.h:263
int32_t index_t
type that will be used for index
Definition: base.h:336
runtime shape checking template get the shape of an expression, report error if shape mismatch ...
Definition: expr_engine-inl.h:365
OneHotEncodeExp(const IndexExp &index, index_t num_choices)
constructor
Definition: one_hot.h:48
defines how expression exp can be evaluated and stored into dst
Definition: expression.h:80
const SubType & self(void) const
Definition: expression.h:83
Plan< BinaryMapExp< OP, TA, TB, DType, etype >, DType > MakePlan(const BinaryMapExp< OP, TA, TB, DType, etype > &e)
Definition: expr_engine-inl.h:240
MSHADOW_XINLINE DType Eval(index_t y, index_t x) const
Definition: one_hot.h:70
const IndexExp & index_
index operand
Definition: one_hot.h:44
overloaded + operator between half_t and bf16_t
Definition: base.h:327
Plan(const OneHotEncodeExp< IndexExp, DType > &e)
Definition: one_hot.h:67
OneHotEncodeExp< IndexExp, default_real_t > one_hot_encode(const Exp< IndexExp, IDType, e1 > &index, index_t num_choices)
Definition: one_hot.h:55