mxnet
one_hot.h
Go to the documentation of this file.
1 
7 #ifndef MSHADOW_EXTENSION_ONE_HOT_H_
8 #define MSHADOW_EXTENSION_ONE_HOT_H_
9 
10 #include "../extension.h"
11 
12 
13 namespace mshadow {
14 namespace expr {
20 template<typename IndexExp, typename DType>
22  public Exp<OneHotEncodeExp<IndexExp, DType>,
23  DType, type::kChainer> {
25  const IndexExp &index_;
29  OneHotEncodeExp(const IndexExp &index, index_t num_choices)
30  : index_(index), num_choices_(num_choices) {}
31 };
32 
33 template<typename IndexExp,
34  typename IDType, int e1>
36 one_hot_encode(const Exp<IndexExp, IDType, e1> &index, index_t num_choices) {
38  ::Error_Expression_Does_Not_Meet_Dimension_Req();
39  return OneHotEncodeExp<IndexExp, default_real_t>(index.self(), num_choices);
40 }
41 
42 //----------------------
43 // Execution plan
44 //----------------------
45 template<typename IndexExp, typename DType>
46 struct Plan<OneHotEncodeExp<IndexExp, DType>, DType> {
47  public:
49  : index_(MakePlan(e.index_)) {
50  }
51  MSHADOW_XINLINE DType Eval(index_t y, index_t x) const {
52  index_t idx = static_cast<index_t>(index_.Eval(0, y));
53  return static_cast<DType>(x == idx);
54  }
55 
56  private:
58 };
59 
60 template<typename IndexExp, typename DType>
63  return Plan<OneHotEncodeExp<IndexExp, DType>, DType>(exp);
64 }
65 
66 template<int dim, typename IndexExp, typename DType>
67 struct ShapeCheck<dim, OneHotEncodeExp<IndexExp, DType> > {
68  inline static Shape<dim>
70  CHECK(dim == 2)
71  << "OneHotEncodeExp only support 2 dimension output";
73  Shape<dim> ret;
74  ret[0] = shape[0];
75  ret[1] = t.num_choices_;
76  return ret;
77  }
78 };
79 
80 template<typename IndexExp, typename DType>
81 struct ExpInfo<OneHotEncodeExp<IndexExp, DType> > {
82  static const int kDim = 2;
83  static const int kDevMask = ExpInfo<IndexExp>::kDevMask;
84 };
85 } // namespace expr
86 } // namespace mshadow
87 #endif // MSHADOW_EXTENSION_ONE_HOT_H_
index_t num_choices_
number of choices we can have.
Definition: one_hot.h:27
Definition: expr_engine-inl.h:40
used to help static type check
Definition: expr_engine-inl.h:312
static Shape< dim > Check(const OneHotEncodeExp< IndexExp, DType > &t)
Definition: one_hot.h:69
Create a one-hot indicator array.
Definition: one_hot.h:21
static Shape< dim > Check(const E &t)
#define MSHADOW_XINLINE
Definition: base.h:204
static type inference template, used to get the dimension of each expression, if ExpInfo<E>::kDim == ...
Definition: expr_engine-inl.h:244
int32_t index_t
type that will be used for index
Definition: base.h:291
runtime shape checking template get the shape of an expression, report error if shape mismatch ...
Definition: expr_engine-inl.h:346
OneHotEncodeExp(const IndexExp &index, index_t num_choices)
constructor
Definition: one_hot.h:29
defines how expression exp can be evaluated and stored into dst
Definition: expression.h:61
const SubType & self(void) const
Definition: expression.h:64
Plan< BinaryMapExp< OP, TA, TB, DType, etype >, DType > MakePlan(const BinaryMapExp< OP, TA, TB, DType, etype > &e)
Definition: expr_engine-inl.h:221
MSHADOW_XINLINE DType Eval(index_t y, index_t x) const
Definition: one_hot.h:51
const IndexExp & index_
index operand
Definition: one_hot.h:25
namespace for mshadow
Definition: base.h:282
Plan(const OneHotEncodeExp< IndexExp, DType > &e)
Definition: one_hot.h:48
OneHotEncodeExp< IndexExp, default_real_t > one_hot_encode(const Exp< IndexExp, IDType, e1 > &index, index_t num_choices)
Definition: one_hot.h:36