14 #ifndef ETEQ_OPERATOR_HPP 15 #define ETEQ_OPERATOR_HPP 22 return std::all_of(shape.
begin() + 2, shape.
end(),
44 template <
typename OP,
size_t N,
typename T>
45 using ReduceOutT = Eigen::TensorReductionOp<OP,
53 inline std::array<teq::RankT,N>
dim_copy (std::vector<teq::RankT> d)
55 std::array<teq::RankT,N> out;
57 std::copy(it, it + N, out.begin());
61 #define _ETEQ_INTERNAL_V2A_CASE(N, PROCESS, RED)\ 62 case N: return make_eigentensor<T,ReduceOutT<RED,N,T>,TensMapT<T>>(\ 63 shape_convert(outshape), [vdims](TensMapT<T>& in) {\ 64 return in.PROCESS(::eteq::internal::dim_copy<N>(vdims)); },\ 65 make_tensmap(in.data_, in.shape_)); 67 #define _ETEQ_INTERNAL_V2A(PROCESS, RED) {\ 68 assert(nullptr != in.coorder_);\ 70 in.coorder_->forward(coord.begin(), coord.begin());\ 71 std::vector<teq::RankT> vdims;\ 72 std::copy_if(coord.begin(), coord.end(), std::back_inserter(vdims),\ 73 [](teq::RankT d) { return d < teq::rank_cap; });\ 74 switch (vdims.size()) {\ 75 _ETEQ_INTERNAL_V2A_CASE(0, PROCESS, RED)\ 76 _ETEQ_INTERNAL_V2A_CASE(1, PROCESS, RED)\ 77 _ETEQ_INTERNAL_V2A_CASE(2, PROCESS, RED)\ 78 _ETEQ_INTERNAL_V2A_CASE(3, PROCESS, RED)\ 79 _ETEQ_INTERNAL_V2A_CASE(4, PROCESS, RED)\ 80 _ETEQ_INTERNAL_V2A_CASE(5, PROCESS, RED)\ 81 _ETEQ_INTERNAL_V2A_CASE(6, PROCESS, RED)\ 82 _ETEQ_INTERNAL_V2A_CASE(7, PROCESS, RED)\ 84 } return make_eigentensor<T,ReduceOutT<RED,8,T>,TensMapT<T>>(\ 85 shape_convert(outshape), [vdims](TensMapT<T>& in) {\ 86 return in.PROCESS(::eteq::internal::dim_copy<8>(vdims));\ 87 }, make_tensmap(in.data_, in.shape_));\ 102 template <
typename T>
107 template <
typename T>
112 template <
typename T>
124 return in.broadcast(coord);
129 template <
typename T>
135 if (
is_2d(outshape) && reorder[0] == 1 && reorder[1] == 0)
138 return make_eigenmatrix<T,Eigen::Transpose<MatMapT<T>>,
143 return in.transpose();
151 return in.shuffle(reorder);
156 template <
typename T>
164 std::fill(offset.begin(), offset.end(), 0);
167 offset[dimension] = slicing[0];
168 extent[dimension] = slicing[1];
177 return in.slice(offset, extent);
182 template <
typename T>
188 std::array<std::pair<teq::DimT,teq::DimT>,
teq::rank_cap> paddings;
191 paddings[i] = std::make_pair(0, 0);
193 paddings[padding[2]] = std::make_pair(padding[0], padding[1]);
195 const std::array<std::pair<teq::DimT,teq::DimT>,
teq::rank_cap>,
202 return in.pad(paddings);
207 template <
typename T>
213 Eigen::array<Eigen::DenseIndex,teq::rank_cap> incrs;
214 std::copy(incrs_tmp.begin(), incrs_tmp.end(), incrs.begin());
216 const Eigen::array<Eigen::DenseIndex,teq::rank_cap>,
223 return in.stride(incrs);
229 template <
typename T>
236 Eigen::internal::scalar_abs_op<T>,
const MatMapT<T>>,
240 return in.cwiseAbs();
244 Eigen::internal::scalar_abs_op<T>,
const TensMapT<T>>,
254 template <
typename T>
261 Eigen::internal::scalar_opposite_op<T>,
const MatMapT<T>>,
269 Eigen::internal::scalar_opposite_op<T>,
const TensMapT<T>>,
279 template <
typename T>
282 #ifdef __cpp_if_constexpr 283 if constexpr(!std::is_integral<T>::value)
289 typename Eigen::ArrayWrapper<MatMapT<T>>::SinReturnType,
293 return in.array().sin();
303 return in.unaryExpr(std::function<T(
const T&)>(
313 template <
typename T>
316 #ifdef __cpp_if_constexpr 317 if constexpr(!std::is_integral<T>::value)
323 typename Eigen::ArrayWrapper<MatMapT<T>>::CosReturnType,
327 return in.array().cos();
337 return in.unaryExpr(std::function<T(
const T&)>(
347 template <
typename T>
354 typename Eigen::ArrayWrapper<MatMapT<T>>::TanReturnType,
358 return in.array().tan();
366 return in.unaryExpr(std::function<T(
const T&)>(
376 template <
typename T>
383 typename Eigen::ArrayWrapper<MatMapT<T>>::ExpReturnType,
387 return in.array().exp();
391 Eigen::internal::scalar_exp_op<T>,
const TensMapT<T>>,
401 template <
typename T>
408 typename Eigen::ArrayWrapper<MatMapT<T>>::LogReturnType,
412 return in.array().log();
416 Eigen::internal::scalar_log_op<T>,
const TensMapT<T>>,
426 template <
typename T>
433 Eigen::internal::scalar_sqrt_op<T>,
const MatMapT<T>>,
437 return in.cwiseSqrt();
441 Eigen::internal::scalar_sqrt_op<T>,
const TensMapT<T>>,
451 template <
typename T>
458 typename Eigen::ArrayWrapper<MatMapT<T>>::RoundReturnType,
462 return in.array().round();
466 Eigen::internal::scalar_round_op<T>,
const TensMapT<T>>,
474 template <
typename T>
481 Eigen::internal::scalar_sigmoid_op<T>,
const MatMapT<T>>,
485 return in.unaryExpr(Eigen::internal::scalar_sigmoid_op<T>());
489 Eigen::internal::scalar_sigmoid_op<T>,
const TensMapT<T>>,
497 template <
typename T>
504 Eigen::CwiseBinaryOp<Eigen::internal::scalar_product_op<T>,
505 const Eigen::CwiseUnaryOp<Eigen::internal::scalar_sigmoid_op<T>,
507 const Eigen::CwiseUnaryOp<Eigen::internal::bind1st_op<
508 Eigen::internal::scalar_difference_op<T>>,
509 const Eigen::CwiseUnaryOp<Eigen::internal::scalar_sigmoid_op<T>,
514 auto out = in.unaryExpr(Eigen::internal::scalar_sigmoid_op<T>());
515 return out.cwiseProduct(out.unaryExpr(
516 Eigen::internal::bind1st_op<Eigen::internal::scalar_difference_op<T>>(1)));
520 Eigen::TensorCwiseBinaryOp<Eigen::internal::scalar_product_op<T>,
521 const Eigen::TensorCwiseUnaryOp<Eigen::internal::scalar_sigmoid_op<T>,
523 const Eigen::TensorCwiseUnaryOp<Eigen::internal::bind1st_op<
524 Eigen::internal::scalar_difference_op<T>>,
525 const Eigen::TensorCwiseUnaryOp<Eigen::internal::scalar_sigmoid_op<T>,
530 auto out = in.sigmoid();
531 return out * (1 - out);
535 template <
typename T>
542 Eigen::internal::scalar_tanh_op<T>,
const MatMapT<T>>,
546 return in.unaryExpr(Eigen::internal::scalar_tanh_op<T>());
550 Eigen::internal::scalar_tanh_op<T>,
const TensMapT<T>>,
558 template <
typename T>
565 Eigen::internal::scalar_square_op<T>,
const MatMapT<T>>,
569 return in.unaryExpr(Eigen::internal::scalar_square_op<T>());
573 Eigen::internal::scalar_square_op<T>,
const TensMapT<T>>,
581 template <
typename T>
588 Eigen::internal::scalar_cube_op<T>,
const MatMapT<T>>,
592 return in.unaryExpr(Eigen::internal::scalar_cube_op<T>());
596 Eigen::internal::scalar_cube_op<T>,
const TensMapT<T>>,
607 template <
typename T>
614 std::function<T(const T&,const T&)>,
617 [](std::vector<MatMapT<T>>&
args)
620 std::function<T(
const T&,
const T&)>(
621 [](
const T& a,
const T& b) -> T
630 std::function<T(const T&,const T&)>,
633 [](std::vector<TensMapT<T>>&
args)
636 std::function<T(
const T&,
const T&)>(
637 [](
const T& a,
const T& b) -> T
646 template <
typename T>
653 Eigen::internal::scalar_sum_op<T>,
656 [](std::vector<MatMapT<T>>&
args)
664 Eigen::internal::scalar_sum_op<T>,
667 [](std::vector<TensMapT<T>>&
args)
678 template <
typename T>
685 Eigen::internal::scalar_difference_op<T>,
688 [](std::vector<MatMapT<T>>&
args)
696 Eigen::internal::scalar_difference_op<T>,
699 [](std::vector<TensMapT<T>>&
args)
707 template <
typename T>
714 Eigen::internal::scalar_product_op<T>,
717 [](std::vector<MatMapT<T>>&
args)
719 return args[0].cwiseProduct(
args[1]);
725 Eigen::internal::scalar_product_op<T>,
728 [](std::vector<TensMapT<T>>&
args)
739 template <
typename T>
746 Eigen::internal::scalar_quotient_op<T>,
749 [](std::vector<MatMapT<T>>&
args)
751 return args[0].cwiseQuotient(
args[1]);
757 Eigen::internal::scalar_quotient_op<T>,
760 [](std::vector<TensMapT<T>>&
args)
771 template <
typename T>
778 std::function<T(const T&,const T&)>,
781 [](std::vector<MatMapT<T>>&
args)
784 std::function<T(
const T&,
const T&)>(
785 [](
const T& a,
const T& b) -> T
794 std::function<T(const T&,const T&)>,
797 [](std::vector<TensMapT<T>>&
args)
800 std::function<T(
const T&,
const T&)>(
801 [](
const T& a,
const T& b) -> T
813 template <
typename T>
820 std::function<T(const T&,const T&)>,
823 [](std::vector<MatMapT<T>>&
args)
826 std::function<T(
const T&,
const T&)>(
827 [](
const T& a,
const T& b) -> T
836 std::function<T(const T&,const T&)>,
839 [](std::vector<TensMapT<T>>&
args)
842 std::function<T(
const T&,
const T&)>(
843 [](
const T& a,
const T& b) -> T
855 template <
typename T>
862 std::function<T(const T&,const T&)>,
865 [](std::vector<MatMapT<T>>&
args)
868 std::function<T(
const T&,
const T&)>(
869 [](
const T& a,
const T& b) -> T
878 std::function<T(const T&,const T&)>,
881 [](std::vector<TensMapT<T>>&
args)
884 std::function<T(
const T&,
const T&)>(
885 [](
const T& a,
const T& b) -> T
897 template <
typename T>
904 std::function<T(const T&,const T&)>,
907 [](std::vector<MatMapT<T>>&
args)
910 std::function<T(
const T&,
const T&)>(
911 [](
const T& a,
const T& b) -> T
920 std::function<T(const T&,const T&)>,
923 [](std::vector<TensMapT<T>>&
args)
926 std::function<T(
const T&,
const T&)>(
927 [](
const T& a,
const T& b) -> T
938 template <
typename T>
945 Eigen::internal::scalar_min_op<T,T>,
948 [](std::vector<MatMapT<T>>&
args)
956 Eigen::internal::scalar_min_op<T>,
959 [](std::vector<TensMapT<T>>&
args)
969 template <
typename T>
976 Eigen::internal::scalar_max_op<T,T>,
979 [](std::vector<MatMapT<T>>&
args)
987 Eigen::internal::scalar_max_op<T>,
990 [](std::vector<TensMapT<T>>&
args)
1001 template <
typename T>
1004 if (
is_2d(outshape))
1008 std::function<T(const T&,const T&)>,
1011 [](std::vector<MatMapT<T>>&
args)
1014 std::function<T(
const T&,
const T&)>(unif<T>));
1020 std::function<T(const T&,const T&)>,
1023 [](std::vector<TensMapT<T>>&
args)
1026 std::function<T(
const T&,
const T&)>(unif<T>));
1035 template <
typename T>
1040 if (
is_2d(outshape))
1055 Eigen::TensorSelectOp<const TensMapT<T>,
1069 template <
typename T>
1072 assert(
is_2d(outshape));
1073 return make_eigenmatrix<T,Eigen::Product<MatMapT<T>,
MatMapT<T>>,
1075 [](std::vector<MatMapT<T>>&
args)
1084 template <
typename T>
1087 assert(
nullptr != kernel.
coorder_);
1091 std::copy(kernel_dims.begin(), kernel_dims.end(), dims.begin());
1097 [&](std::vector<TensMapT<T>>&
args)
1099 return args[0].convolve(
args[1], dims);
1106 template <
typename T>
1111 Eigen::TensorReductionOp<Eigen::internal::SumReducer<T>,
1113 const Eigen::TensorCwiseBinaryOp<
1114 Eigen::internal::scalar_product_op<T,T>,
1115 const Eigen::TensorBroadcastingOp<
1116 const std::array<teq::DimT,teq::rank_cap+1>,
1117 const Eigen::TensorReshapingOp<
1118 const std::array<teq::DimT,teq::rank_cap+1>,
1119 Eigen::TensorReverseOp<
1120 const std::array<bool,teq::rank_cap>,
1125 const Eigen::TensorPatchOp<
1127 const Eigen::TensorPaddingOp<
1135 [&](std::vector<TensMapT<T>>&
args)
1137 auto& outshape = super_composite.
shape_;
1140 std::copy(outshape.begin(), outshape.end(), patch_dims.begin());
1144 int paddsize = outshape.at(i) - 1;
1145 paddings[i] = std::make_pair(paddsize, paddsize);
1147 auto patched =
args[0].pad(paddings)
1148 .extract_patches(patch_dims);
1150 std::array<bool,teq::rank_cap> revflags;
1151 std::fill(revflags.begin(), revflags.end(),
true);
1152 std::array<teq::DimT,teq::rank_cap+1> pshape;
1153 std::copy(outshape.begin(), outshape.end(), pshape.begin());
1155 std::array<teq::DimT,teq::rank_cap+1> expansion;
1156 std::fill(expansion.begin(), expansion.end(), 1);
1158 auto partial =
args[1]
1161 .broadcast(expansion) * patched;
1164 std::iota(shapespace.begin(), shapespace.end(), 0);
1165 return partial.sum(shapespace);
1172 template <
typename T>
1177 Eigen::TensorReductionOp<Eigen::internal::SumReducer<T>,
1179 const Eigen::TensorCwiseBinaryOp<
1180 Eigen::internal::scalar_product_op<T,T>,
1181 const Eigen::TensorBroadcastingOp<
1182 const std::array<teq::DimT,teq::rank_cap+1>,
1183 const Eigen::TensorReshapingOp<
1184 const std::array<teq::DimT,teq::rank_cap+1>,
1188 const Eigen::TensorPatchOp<
1195 [&](std::vector<TensMapT<T>>&
args)
1197 auto& outshape = super_composite.
shape_;
1200 std::copy(outshape.begin(), outshape.end(), patch_dims.begin());
1201 auto patched =
args[0].extract_patches(patch_dims);
1203 std::array<teq::DimT,teq::rank_cap+1> pshape;
1204 std::copy(outshape.begin(), outshape.end(), pshape.begin());
1206 std::array<teq::DimT,teq::rank_cap+1> expansion;
1207 std::fill(expansion.begin(), expansion.end(), 1);
1209 auto partial =
args[1]
1211 .broadcast(expansion) * patched;
1214 std::iota(shapespace.begin(), shapespace.end(), 0);
1215 return partial.sum(shapespace);
1223 #endif // ETEQ_OPERATOR_HPP std::array< CDimT, rank_cap > CoordT
Definition: shape.hpp:56
EigenptrT< T > gt(teq::Shape &outshape, const OpArg< T > &a, const OpArg< T > &b)
Definition: operator.hpp:898
const RankT rank_cap
Number of dimsensions in a shape/coordinate.
Definition: shape.hpp:47
args
Definition: csv_to_png.py:105
EigenptrT< T > round(teq::Shape &outshape, const OpArg< T > &in)
Definition: operator.hpp:452
EigenptrT< T > sin(teq::Shape &outshape, const OpArg< T > &in)
Definition: operator.hpp:280
teq::Shape shape_
Shape of the data.
Definition: operator.hpp:37
CoordptrT extend(teq::RankT rank, std::vector< teq::DimT > ext)
Return CoordMap wrapper of extension parameters.
EigenptrT< T > tan(teq::Shape &outshape, const OpArg< T > &in)
Definition: operator.hpp:348
EigenptrT< T > square(teq::Shape &outshape, const OpArg< T > &in)
Definition: operator.hpp:559
EigenptrT< T > add(teq::Shape &outshape, const OpArg< T > &a, const OpArg< T > &b)
Definition: operator.hpp:647
Eigen::TensorMap< TensorT< T > > TensMapT
Eigen Tensor Map (reference)
Definition: eigen.hpp:39
Definition: constant.hpp:17
EigenptrT< T > convolution_image_grad(teq::Shape &imageshape, const OpArg< T > &kernel, const OpArg< T > &super_composite)
Applies the gradient of convolution with respect to image.
Definition: operator.hpp:1107
TensMapT< T > make_tensmap(T *data, const teq::Shape &shape)
Return Eigen Tensor given raw data and teq Shape.
Definition: eigen.hpp:201
EigenptrT< T > neq(teq::Shape &outshape, const OpArg< T > &a, const OpArg< T > &b)
Definition: operator.hpp:814
EigenptrT< T > sqrt(teq::Shape &outshape, const OpArg< T > &in)
Definition: operator.hpp:427
uint8_t RankT
Type used for shape rank.
Definition: shape.hpp:23
OpArg(T *data, teq::Shape shape, CoordMap *coorder)
Definition: operator.hpp:30
T * data_
Raw data argument.
Definition: operator.hpp:34
EigenptrT< T > max(teq::Shape &outshape, const OpArg< T > &a, const OpArg< T > &b)
Definition: operator.hpp:970
EigenptrT< T > div(teq::Shape &outshape, const OpArg< T > &a, const OpArg< T > &b)
Definition: operator.hpp:740
EigenptrT< T > make_eigentensor(DimensionsT dims, std::function< EigenSource(EigenArgs &)> make_base, EigenArgs args)
Definition: eigen.hpp:160
EigenptrT< T > stride(teq::Shape &outshape, const OpArg< T > &in)
Return Eigen data object representing strided view of in.
Definition: operator.hpp:208
Eigen::Map< MatrixT< T > > MatMapT
Eigen Matrix Map (reference)
Definition: eigen.hpp:31
std::shared_ptr< iEigen< T > > EigenptrT
Smart point of generic Eigen data object.
Definition: eigen.hpp:94
CoordMap * coorder_
Transformation argument, null denotes no argument.
Definition: operator.hpp:40
EigenptrT< T > cube(teq::Shape &outshape, const OpArg< T > &in)
Definition: operator.hpp:582
Eigen::TensorReductionOp< OP, const std::array< teq::RankT, N >, const TensMapT< T > > ReduceOutT
Generic Eigen reduction operator.
Definition: operator.hpp:46
EigenptrT< T > eq(teq::Shape &outshape, const OpArg< T > &a, const OpArg< T > &b)
Definition: operator.hpp:772
EigenptrT< T > select(teq::Shape &outshape, const OpArg< T > &condition, const OpArg< T > &then, const OpArg< T > &otherwise)
Definition: operator.hpp:1036
Eigen transformation wrapper implementation of iCoordMap.
Definition: coord.hpp:18
EigenptrT< T > sigmoid_grad(teq::Shape &outshape, const OpArg< T > &in)
Definition: operator.hpp:498
EigenptrT< T > sigmoid(teq::Shape &outshape, const OpArg< T > &in)
Definition: operator.hpp:475
EigenptrT< T > abs(teq::Shape &outshape, const OpArg< T > &in)
Definition: operator.hpp:230
void forward(teq::CoordT::iterator out, teq::CoordT::const_iterator in) const override
Implementation of iCoordMap.
Definition: coord.hpp:30
EigenptrT< T > sub(teq::Shape &outshape, const OpArg< T > &a, const OpArg< T > &b)
Definition: operator.hpp:679
CoordptrT permute(std::vector< teq::RankT > dims)
Return CoordMap wrapper of permute indices.
EigenptrT< T > slice(teq::Shape &outshape, const OpArg< T > &in)
Return Eigen data object representing data slicing of dimensions.
Definition: operator.hpp:157
EigenptrT< T > exp(teq::Shape &outshape, const OpArg< T > &in)
Definition: operator.hpp:377
EigenptrT< T > tanh(teq::Shape &outshape, const OpArg< T > &in)
Definition: operator.hpp:536
std::array< teq::RankT, N > dim_copy(std::vector< teq::RankT > d)
Return array of input vector.
Definition: operator.hpp:53
uint16_t DimT
Type used for shape dimension.
Definition: shape.hpp:31
Raw data, shape, and transformation argument struct.
Definition: operator.hpp:28
EigenptrT< T > convolution_kernel_grad(teq::Shape &kernelshape, const OpArg< T > &image, const OpArg< T > &super_composite)
Applies the gradient of convolution with respect to kernel.
Definition: operator.hpp:1173
EigenptrT< T > pad(teq::Shape &outshape, const OpArg< T > &in)
Return Eigen data object representing data zero padding.
Definition: operator.hpp:183
EigenptrT< T > cos(teq::Shape &outshape, const OpArg< T > &in)
Definition: operator.hpp:314
EigenptrT< T > log(teq::Shape &outshape, const OpArg< T > &in)
Definition: operator.hpp:402
std::array< DimT, rank_cap > ShapeT
Array type used to hold dimension info in Shape.
Definition: shape.hpp:50
EigenptrT< T > reduce_sum(teq::Shape &outshape, const OpArg< T > &in) template< typename T > EigenptrT< T > reduce_prod(teq
Return Eigen data object representing reduction where aggregation is sum.
Definition: operator.hpp:94
EigenptrT< T > min(teq::Shape &outshape, const OpArg< T > &a, const OpArg< T > &b)
Definition: operator.hpp:939
static bool is_2d(teq::Shape shape)
Definition: operator.hpp:20
EigenptrT< T > lt(teq::Shape &outshape, const OpArg< T > &a, const OpArg< T > &b)
Definition: operator.hpp:856
MatMapT< T > make_matmap(T *data, const teq::Shape &shape)
Return Eigen Matrix given raw data and teq Shape.
Definition: eigen.hpp:190
EigenptrT< T > make_eigenmatrix(DimensionsT dims, std::function< EigenSource(EigenArgs &)> make_base, EigenArgs args)
Definition: eigen.hpp:170
iterator end(void)
Return end iterator of internal array.
Definition: shape.hpp:162
iterator begin(void)
Return begin iterator of internal array.
Definition: shape.hpp:156
EigenptrT< T > convolution(teq::Shape &outshape, const OpArg< T > &input, const OpArg< T > &kernel)
Apply convolution of kernel across input.
Definition: operator.hpp:1085
EigenptrT< T > mul(teq::Shape &outshape, const OpArg< T > &a, const OpArg< T > &b)
Definition: operator.hpp:708
#define _ETEQ_INTERNAL_V2A(PROCESS, RED)
Definition: operator.hpp:67
NElemT n_elems(void) const
Return the total number of elements represented by the shape.
Definition: shape.hpp:118
EigenptrT< T > rand_uniform(teq::Shape &outshape, const OpArg< T > &a, const OpArg< T > &b)
Definition: operator.hpp:1002
EigenptrT< T > neg(teq::Shape &outshape, const OpArg< T > &in)
Definition: operator.hpp:255
DimensionsT shape_convert(teq::Shape shape)
Return Eigen shape of teq Shape.
EigenptrT< T > pow(teq::Shape &outshape, const OpArg< T > &a, const OpArg< T > &b)
Definition: operator.hpp:608
EigenptrT< T > matmul(teq::Shape &outshape, const OpArg< T > &a, const OpArg< T > &b)
Definition: operator.hpp:1070