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 
25 #ifndef MSHADOW_EXTENSION_ONE_HOT_H_
26 #define MSHADOW_EXTENSION_ONE_HOT_H_
27 
28 #include "../extension.h"
29 
30 
31 namespace mshadow {
32 namespace expr {
38 template<typename IndexExp, typename DType>
40  public Exp<OneHotEncodeExp<IndexExp, DType>,
41  DType, type::kChainer> {
43  const IndexExp &index_;
47  OneHotEncodeExp(const IndexExp &index, index_t num_choices)
48  : index_(index), num_choices_(num_choices) {}
49 };
50 
51 template<typename IndexExp,
52  typename IDType, int e1>
53 inline OneHotEncodeExp<IndexExp, default_real_t>
54 one_hot_encode(const Exp<IndexExp, IDType, e1> &index, index_t num_choices) {
56  ::Error_Expression_Does_Not_Meet_Dimension_Req();
57  return OneHotEncodeExp<IndexExp, default_real_t>(index.self(), num_choices);
58 }
59 
60 //----------------------
61 // Execution plan
62 //----------------------
63 template<typename IndexExp, typename DType>
64 struct Plan<OneHotEncodeExp<IndexExp, DType>, DType> {
65  public:
67  : index_(MakePlan(e.index_)) {
68  }
69  MSHADOW_XINLINE DType Eval(index_t y, index_t x) const {
70  index_t idx = static_cast<index_t>(index_.Eval(0, y));
71  return static_cast<DType>(x == idx);
72  }
73 
74  private:
76 };
77 
78 template<typename IndexExp, typename DType>
79 inline Plan<OneHotEncodeExp<IndexExp, DType>, DType>
81  return Plan<OneHotEncodeExp<IndexExp, DType>, DType>(exp);
82 }
83 
84 template<int dim, typename IndexExp, typename DType>
85 struct ShapeCheck<dim, OneHotEncodeExp<IndexExp, DType> > {
86  inline static Shape<dim>
88  CHECK(dim == 2)
89  << "OneHotEncodeExp only support 2 dimension output";
91  Shape<dim> ret;
92  ret[0] = shape[0];
93  ret[1] = t.num_choices_;
94  return ret;
95  }
96 };
97 
98 template<typename IndexExp, typename DType>
99 struct ExpInfo<OneHotEncodeExp<IndexExp, DType> > {
100  static const int kDim = 2;
102 };
103 } // namespace expr
104 } // namespace mshadow
105 #endif // MSHADOW_EXTENSION_ONE_HOT_H_
mshadow::expr::ExpInfo::kDevMask
static const int kDevMask
Definition: expr_engine-inl.h:264
mshadow::expr::Exp::self
const SubType & self(void) const
Definition: expression.h:82
mshadow::expr::OneHotEncodeExp::OneHotEncodeExp
OneHotEncodeExp(const IndexExp &index, index_t num_choices)
constructor
Definition: one_hot.h:47
mshadow::expr::TypeCheckPass
used to help static type check
Definition: expr_engine-inl.h:330
mshadow::expr::Plan< OneHotEncodeExp< IndexExp, DType >, DType >::Eval
MSHADOW_XINLINE DType Eval(index_t y, index_t x) const
Definition: one_hot.h:69
MSHADOW_XINLINE
#define MSHADOW_XINLINE
Definition: base.h:228
mshadow::expr::one_hot_encode
OneHotEncodeExp< IndexExp, default_real_t > one_hot_encode(const Exp< IndexExp, IDType, e1 > &index, index_t num_choices)
Definition: one_hot.h:54
mshadow::expr::ShapeCheck
runtime shape checking template get the shape of an expression, report error if shape mismatch
Definition: expr_engine-inl.h:364
mshadow::expr::ShapeCheck::Check
static Shape< dim > Check(const E &t)
mshadow::expr::ExpInfo
static type inference template, used to get the dimension of each expression, if ExpInfo<E>::kDim == ...
Definition: expr_engine-inl.h:262
mshadow::expr::MakePlan
Plan< BinaryMapExp< OP, TA, TB, DType, etype >, DType > MakePlan(const BinaryMapExp< OP, TA, TB, DType, etype > &e)
Definition: expr_engine-inl.h:239
mshadow::expr::ExpInfo::kDim
static const int kDim
Definition: expr_engine-inl.h:263
mshadow::expr::OneHotEncodeExp::index_
const IndexExp & index_
index operand
Definition: one_hot.h:43
mshadow::index_t
int32_t index_t
type that will be used for index
Definition: base.h:328
mshadow::expr::Plan
Definition: expr_engine-inl.h:58
mshadow::expr::Exp
defines how expression exp can be evaluated and stored into dst
Definition: expression.h:79
mshadow
overloaded + operator between half_t and bf16_t
Definition: base.h:319
mshadow::expr::ShapeCheck< dim, OneHotEncodeExp< IndexExp, DType > >::Check
static Shape< dim > Check(const OneHotEncodeExp< IndexExp, DType > &t)
Definition: one_hot.h:87
mshadow::Shape< dim >
mshadow::expr::Plan< OneHotEncodeExp< IndexExp, DType >, DType >::Plan
Plan(const OneHotEncodeExp< IndexExp, DType > &e)
Definition: one_hot.h:66
mshadow::expr::OneHotEncodeExp
Create a one-hot indicator array.
Definition: one_hot.h:39
mshadow::expr::OneHotEncodeExp::num_choices_
index_t num_choices_
number of choices we can have.
Definition: one_hot.h:45