cxx11_tensor_convolution.cpp
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2014 Benoit Steiner <benoit.steiner.goog@gmail.com>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #include "main.h"
11 
12 #include <Eigen/CXX11/Tensor>
13 
14 using Eigen::Tensor;
16 
17 template <int DataLayout>
18 static void test_evals()
19 {
20  Tensor<float, 2, DataLayout> input(3, 3);
22 
23  input.setRandom();
24  kernel.setRandom();
25 
26  Tensor<float, 2, DataLayout> result(2,3);
27  result.setZero();
29 
30  typedef TensorEvaluator<decltype(input.convolve(kernel, dims3)), DefaultDevice> Evaluator;
31  Evaluator eval(input.convolve(kernel, dims3), DefaultDevice());
32  eval.evalTo(result.data());
33  EIGEN_STATIC_ASSERT(Evaluator::NumDims==2ul, YOU_MADE_A_PROGRAMMING_MISTAKE);
34  VERIFY_IS_EQUAL(eval.dimensions()[0], 2);
35  VERIFY_IS_EQUAL(eval.dimensions()[1], 3);
36 
37  VERIFY_IS_APPROX(result(0,0), input(0,0)*kernel(0) + input(1,0)*kernel(1)); // index 0
38  VERIFY_IS_APPROX(result(0,1), input(0,1)*kernel(0) + input(1,1)*kernel(1)); // index 2
39  VERIFY_IS_APPROX(result(0,2), input(0,2)*kernel(0) + input(1,2)*kernel(1)); // index 4
40  VERIFY_IS_APPROX(result(1,0), input(1,0)*kernel(0) + input(2,0)*kernel(1)); // index 1
41  VERIFY_IS_APPROX(result(1,1), input(1,1)*kernel(0) + input(2,1)*kernel(1)); // index 3
42  VERIFY_IS_APPROX(result(1,2), input(1,2)*kernel(0) + input(2,2)*kernel(1)); // index 5
43 }
44 
45 template <int DataLayout>
46 static void test_expr()
47 {
48  Tensor<float, 2, DataLayout> input(3, 3);
49  Tensor<float, 2, DataLayout> kernel(2, 2);
50  input.setRandom();
51  kernel.setRandom();
52 
53  Tensor<float, 2, DataLayout> result(2,2);
55  dims[0] = 0;
56  dims[1] = 1;
57  result = input.convolve(kernel, dims);
58 
59  VERIFY_IS_APPROX(result(0,0), input(0,0)*kernel(0,0) + input(0,1)*kernel(0,1) +
60  input(1,0)*kernel(1,0) + input(1,1)*kernel(1,1));
61  VERIFY_IS_APPROX(result(0,1), input(0,1)*kernel(0,0) + input(0,2)*kernel(0,1) +
62  input(1,1)*kernel(1,0) + input(1,2)*kernel(1,1));
63  VERIFY_IS_APPROX(result(1,0), input(1,0)*kernel(0,0) + input(1,1)*kernel(0,1) +
64  input(2,0)*kernel(1,0) + input(2,1)*kernel(1,1));
65  VERIFY_IS_APPROX(result(1,1), input(1,1)*kernel(0,0) + input(1,2)*kernel(0,1) +
66  input(2,1)*kernel(1,0) + input(2,2)*kernel(1,1));
67 }
68 
69 template <int DataLayout>
70 static void test_modes() {
73  input(0) = 1.0f;
74  input(1) = 2.0f;
75  input(2) = 3.0f;
76  kernel(0) = 0.5f;
77  kernel(1) = 1.0f;
78  kernel(2) = 0.0f;
79 
81  dims[0] = 0;
83 
84  // Emulate VALID mode (as defined in
85  // http://docs.scipy.org/doc/numpy/reference/generated/numpy.convolve.html).
86  padding[0] = std::make_pair(0, 0);
88  valid = input.pad(padding).convolve(kernel, dims);
89  VERIFY_IS_EQUAL(valid.dimension(0), 1);
90  VERIFY_IS_APPROX(valid(0), 2.5f);
91 
92  // Emulate SAME mode (as defined in
93  // http://docs.scipy.org/doc/numpy/reference/generated/numpy.convolve.html).
94  padding[0] = std::make_pair(1, 1);
96  same = input.pad(padding).convolve(kernel, dims);
97  VERIFY_IS_EQUAL(same.dimension(0), 3);
98  VERIFY_IS_APPROX(same(0), 1.0f);
99  VERIFY_IS_APPROX(same(1), 2.5f);
100  VERIFY_IS_APPROX(same(2), 4.0f);
101 
102  // Emulate FULL mode (as defined in
103  // http://docs.scipy.org/doc/numpy/reference/generated/numpy.convolve.html).
104  padding[0] = std::make_pair(2, 2);
106  full = input.pad(padding).convolve(kernel, dims);
107  VERIFY_IS_EQUAL(full.dimension(0), 5);
108  VERIFY_IS_APPROX(full(0), 0.0f);
109  VERIFY_IS_APPROX(full(1), 1.0f);
110  VERIFY_IS_APPROX(full(2), 2.5f);
111  VERIFY_IS_APPROX(full(3), 4.0f);
112  VERIFY_IS_APPROX(full(4), 1.5f);
113 }
114 
115 template <int DataLayout>
116 static void test_strides() {
119  input.setRandom();
120  kernel.setRandom();
121 
123  dims[0] = 0;
124  Eigen::array<ptrdiff_t, 1> stride_of_3;
125  stride_of_3[0] = 3;
126  Eigen::array<ptrdiff_t, 1> stride_of_2;
127  stride_of_2[0] = 2;
128 
130  result = input.stride(stride_of_3).convolve(kernel, dims).stride(stride_of_2);
131 
132  VERIFY_IS_EQUAL(result.dimension(0), 2);
133  VERIFY_IS_APPROX(result(0), (input(0)*kernel(0) + input(3)*kernel(1) +
134  input(6)*kernel(2)));
135  VERIFY_IS_APPROX(result(1), (input(6)*kernel(0) + input(9)*kernel(1) +
136  input(12)*kernel(2)));
137 }
138 
140 {
141  CALL_SUBTEST(test_evals<ColMajor>());
142  CALL_SUBTEST(test_evals<RowMajor>());
143  CALL_SUBTEST(test_expr<ColMajor>());
144  CALL_SUBTEST(test_expr<RowMajor>());
145  CALL_SUBTEST(test_modes<ColMajor>());
146  CALL_SUBTEST(test_modes<RowMajor>());
147  CALL_SUBTEST(test_strides<ColMajor>());
148  CALL_SUBTEST(test_strides<RowMajor>());
149 }
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index dimension(std::size_t n) const
Definition: Tensor.h:101
static void test_expr()
static int f(const TensorMap< Tensor< int, 3 > > &tensor)
void test_cxx11_tensor_convolution()
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Tensor< Scalar_, NumIndices_, Options_, IndexType_ > & setRandom()
Definition: TensorBase.h:848
#define EIGEN_STATIC_ASSERT(CONDITION, MSG)
Definition: StaticAssert.h:122
static void test_strides()
static void test_modes()
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar * data()
Definition: Tensor.h:104
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const TensorStridingOp< const Strides, const Tensor< Scalar_, NumIndices_, Options_, IndexType_ > > stride(const Strides &strides) const
Definition: TensorBase.h:986
static void test_evals()
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Tensor< Scalar_, NumIndices_, Options_, IndexType_ > & setZero()
Definition: TensorBase.h:840
The tensor class.
Definition: Tensor.h:63


hebiros
Author(s): Xavier Artache , Matthew Tesch
autogenerated on Thu Sep 3 2020 04:08:08