title: Replicating TensorFlows Conv2D Operating using Eigen Tensors
body: <p>I'm trying to implement a lightweight (minimal library dependencies) version of a TensorFlow graph in c++ and I'm trying to use Eigen Tensor objects to perform the graphs operations. Right now I'm stuck trying to use the Eigen <code>Tensor.convolve()</code> method to try and replicate the behaviour of TensorFlow's Conv2D operation. To keep things simple my initial Conv2D operation has no padding and strides of one. </p>
  
  <p>The input to convolutional layer is a 51x51x1 tensor which is being convolved with a filter bank of size 3x3x1x16. In tensorflow this generates an output tensor of size 49x49x16. Setting up this same operation in C++ using the Eigen code below only populates the first channel of the output tensor, so the top 49x49x1 cells contain the correct values, but the remaining 1-15 channels are not populated.</p>
  
  <pre><code>  Eigen::TensorMap&lt;Eigen::Tensor&lt;float,4&gt; &gt; filter(filter, 3, 3, 1, 16 );
    Eigen::TensorMap&lt;Eigen::Tensor&lt;float,3&gt; &gt; input(inputBuffer, 51, 51, 1 );
    Eigen::TensorMap&lt;Eigen::Tensor&lt;float,3&gt; &gt; output(outputBuffer, 49, 49, 16);
  
    Eigen::array&lt;ptrdiff_t, 2&gt; convDims({0, 1});
    output = input.convolve(filter, convDims);
  </code></pre>
  
  <p>I assume that I'm miss-understanding what these functions do and that they are not performing the same operation. To get my implementation working I've tried to loop through the 16 filter channels and apply the convolution method individually to each one, but I'm getting compiler errors which I don't understand with the code below:</p>
  
  <pre><code>  for (int s=0; s&lt;16; ++s)
    {
      Eigen::array&lt;int, 4&gt; fOffset = {0, 0, 0, s};
      Eigen::array&lt;int, 4&gt; fExtent = {3, 3, 1, 1};
  
      Eigen::array&lt;int, 3&gt; oOffset = {0, 0, s};
      Eigen::array&lt;int, 3&gt; oExtent = {49, 49, 1};
  
      auto filterSlice = filter.slice(fOffset, fExtent);
  
      output.slice(oOffset, oExtent) = input.convolve(filterSlice, convDims);
    }
  </code></pre>
  
  <p>This code produces the following error from somewhere within the Eigen Tensor code, It may have something to do with the assignment to the results of the slice method but I'm not sure. If the result is assigned to an auto type then it compiles, but not if the result is later evaluated.</p>
  
  <p>If anyone knows how to resolve this error or more generally how I can replicated the Conv2D operation using Eigen Tensors that would be a great help.</p>
  
  <pre><code>/home/user/tensorflow_xla/bcc-2.0.2-gcc/sparc-gaisler-elf/include/unsupported/Eigen/CXX11/src/Tensor/TensorConvolution.h: In instantiation of 'void Eigen::TensorEvaluator&lt;const Eigen::TensorConvolutionOp&lt;Dimensions, InputXprType, KernelXprType&gt;, Device&gt;::preloadKernel() [with Indices = const std::array&lt;int, 2&gt;; InputArgType = const Eigen::TensorMap&lt;Eigen::Tensor&lt;float, 3&gt; &gt;; KernelArgType = const Eigen::TensorSlicingOp&lt;const std::array&lt;int, 4&gt;, const std::array&lt;int, 4&gt;, Eigen::TensorMap&lt;Eigen::Tensor&lt;float, 4&gt; &gt; &gt;; Device = Eigen::DefaultDevice]':
  /home/user/tensorflow_xla/bcc-2.0.2-gcc/sparc-gaisler-elf/include/unsupported/Eigen/CXX11/src/Tensor/TensorConvolution.h:383:18:   required from 'bool Eigen::TensorEvaluator&lt;const Eigen::TensorConvolutionOp&lt;Dimensions, InputXprType, KernelXprType&gt;, Device&gt;::evalSubExprsIfNeeded(Eigen::TensorEvaluator&lt;const Eigen::TensorConvolutionOp&lt;Dimensions, InputXprType, KernelXprType&gt;, Device&gt;::Scalar*) [with Indices = const std::array&lt;int, 2&gt;; InputArgType = const Eigen::TensorMap&lt;Eigen::Tensor&lt;float, 3&gt; &gt;; KernelArgType = const Eigen::TensorSlicingOp&lt;const std::array&lt;int, 4&gt;, const std::array&lt;int, 4&gt;, Eigen::TensorMap&lt;Eigen::Tensor&lt;float, 4&gt; &gt; &gt;; Device = Eigen::DefaultDevice; Eigen::TensorEvaluator&lt;const Eigen::TensorConvolutionOp&lt;Dimensions, InputXprType, KernelXprType&gt;, Device&gt;::Scalar = float]'
  /home/user/tensorflow_xla/bcc-2.0.2-gcc/sparc-gaisler-elf/include/unsupported/Eigen/CXX11/src/Tensor/TensorAssign.h:146:62:   required from 'bool Eigen::TensorEvaluator&lt;const Eigen::TensorAssignOp&lt;LhsXprType, RhsXprType&gt;, Device&gt;::evalSubExprsIfNeeded(Eigen::TensorEvaluator&lt;const Eigen::TensorAssignOp&lt;LhsXprType, RhsXprType&gt;, Device&gt;::Scalar*) [with LeftArgType = Eigen::TensorSlicingOp&lt;const std::array&lt;int, 3&gt;, const std::array&lt;int, 3&gt;, Eigen::TensorMap&lt;Eigen::Tensor&lt;float, 3&gt; &gt; &gt;; RightArgType = const Eigen::TensorConvolutionOp&lt;const std::array&lt;int, 2&gt;, const Eigen::TensorMap&lt;Eigen::Tensor&lt;float, 3&gt; &gt;, const Eigen::TensorSlicingOp&lt;const std::array&lt;int, 4&gt;, const std::array&lt;int, 4&gt;, Eigen::TensorMap&lt;Eigen::Tensor&lt;float, 4&gt; &gt; &gt; &gt;; Device = Eigen::DefaultDevice; Eigen::TensorEvaluator&lt;const Eigen::TensorAssignOp&lt;LhsXprType, RhsXprType&gt;, Device&gt;::Scalar = float]'
  /home/user/tensorflow_xla/bcc-2.0.2-gcc/sparc-gaisler-elf/include/unsupported/Eigen/CXX11/src/Tensor/TensorExecutor.h:45:16:   required from 'static void Eigen::internal::TensorExecutor&lt;Expression, Device, Vectorizable, Tileable&gt;::run(const Expression&amp;, const Device&amp;) [with Expression = const Eigen::TensorAssignOp&lt;Eigen::TensorSlicingOp&lt;const std::array&lt;int, 3&gt;, const std::array&lt;int, 3&gt;, Eigen::TensorMap&lt;Eigen::Tensor&lt;float, 3&gt; &gt; &gt;, const Eigen::TensorConvolutionOp&lt;const std::array&lt;int, 2&gt;, const Eigen::TensorMap&lt;Eigen::Tensor&lt;float, 3&gt; &gt;, const Eigen::TensorSlicingOp&lt;const std::array&lt;int, 4&gt;, const std::array&lt;int, 4&gt;, Eigen::TensorMap&lt;Eigen::Tensor&lt;float, 4&gt; &gt; &gt; &gt; &gt;; Device = Eigen::DefaultDevice; bool Vectorizable = false; bool Tileable = false]'
  /home/user/tensorflow_xla/bcc-2.0.2-gcc/sparc-gaisler-elf/include/unsupported/Eigen/CXX11/src/Tensor/TensorMorphing.h:448:65:   required from 'Eigen::TensorSlicingOp&lt;StartIndices, Sizes, XprType&gt;&amp; Eigen::TensorSlicingOp&lt;StartIndices, Sizes, XprType&gt;::operator=(const OtherDerived&amp;) [with OtherDerived = Eigen::TensorConvolutionOp&lt;const std::array&lt;int, 2&gt;, const Eigen::TensorMap&lt;Eigen::Tensor&lt;float, 3&gt; &gt;, const Eigen::TensorSlicingOp&lt;const std::array&lt;int, 4&gt;, const std::array&lt;int, 4&gt;, Eigen::TensorMap&lt;Eigen::Tensor&lt;float, 4&gt; &gt; &gt; &gt;; StartIndices = const std::array&lt;int, 3&gt;; Sizes = const std::array&lt;int, 3&gt;; XprType = Eigen::TensorMap&lt;Eigen::Tensor&lt;float, 3&gt; &gt;]'
  ../tfmin_generated/terrain_model.cpp:215:92:   required from here
  /home/user/tensorflow_xla/bcc-2.0.2-gcc/sparc-gaisler-elf/include/unsupported/Eigen/CXX11/src/Tensor/TensorConvolution.h:527:52: error: 'Eigen::TensorEvaluator&lt;const Eigen::TensorSlicingOp&lt;const std::array&lt;int, 4&gt;, const std::array&lt;int, 4&gt;, Eigen::TensorMap&lt;Eigen::Tensor&lt;float, 4&gt; &gt; &gt;, Eigen::DefaultDevice&gt;::Dimensions {aka const struct std::array&lt;int, 4&gt;}' has no member named 'TotalSize'
         size_t kernel_sz = m_kernelImpl.dimensions().TotalSize() * sizeof(Scalar);
  </code></pre>