Tob*_*ann 5 c++ eigen eigen3 conv-neural-network tensor
将形状的输入张量(3, 20, 30)
(通道优先表示法)与8
形状的滤波器进行卷积(3, 5, 7)
应会得到形状的张量(8, 24, 16)
。我正在尝试使用实现此功能Eigen::Tensor::convolve
,但最终的形状是(1, 24, 16)
。因此,似乎仅应用了一个过滤器,而不是所有过滤器8
。
这是一个最小的示例:
#include <cassert>
#include <iostream>
#include <eigen3/unsupported/Eigen/CXX11/Tensor>
int main() {
int input_height = 20;
int input_width = 30;
int input_channels = 3;
int kernels_height = 5;
int kernels_width = 7;
int kernels_channels = 3;
int kernel_count = 8;
assert(kernels_channels == input_channels);
int expected_output_height = input_height + 1 - kernels_height;
int expected_output_width = input_width + 1 - kernels_width;
int expected_output_channels = kernel_count;
Eigen::Tensor<float, 3> input(input_channels, input_width, input_height);
Eigen::Tensor<float, 4> filters(kernels_channels, kernels_width, kernels_height, kernel_count);
Eigen::array<ptrdiff_t, 3> dims({0, 1, 2});
Eigen::Tensor<float, 3> output = input.convolve(filters, dims);
const Eigen::Tensor<float, 3>::Dimensions& d = output.dimensions();
std::cout << "Expected output shape: (" << expected_output_channels << ", " << expected_output_width << ", " << expected_output_height << ")" << std::endl;
std::cout << "Actual shape: (" << d[0] << ", " << d[1] << ", " << d[2] << ")" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
及其输出:
Expected output shape: (8, 24, 16)
Actual shape: (1, 24, 16)
Run Code Online (Sandbox Code Playgroud)
当然,一个人可以一个接一个地遍历过滤器,并.convolve
为每个过滤器调用,但这
所以我想我在使用Eigen库时做错了什么。如何正确完成?
It doesn't support convolution with several kernels at once (docs):
The dimension size for dimensions of the output tensor which were part of the convolution will be reduced by the formula: output_dim_size = input_dim_size - kernel_dim_size + 1 (requires: input_dim_size >= kernel_dim_size). The dimension sizes for dimensions that were not part of the convolution will remain the same.
According to above expected_output_channels
should be equal to 1 = 3 - 3 + 1
.
I don't think it should be possible to do as you wish, because convolution operation is a mathematical one and well defined, so it would be strange if it wouldn't follow math definition.
我没有检查,但我相信下一个代码会按照您的意愿产生输出:
Eigen::Tensor<float, 3> input(input_channels, input_width, input_height);
Eigen::Tensor<float, 4> filters(kernels_channels, kernels_width, kernels_height, kernel_count);
Eigen::Tensor<float, 3> output(kernel_count, expected_output_width, expected_output_height);
Eigen::array<ptrdiff_t, 3> dims({0, 1, 2});
for (int i = 0; i < kernel_count; ++i){
output.chip(i, 0) = input.convolve(filters.chip(i, 3), dims).chip(0, 0);
}
Run Code Online (Sandbox Code Playgroud)
可以看到,第一个问题和第三个问题并不是什么大问题。希望你幸运,这部分代码不会成为你的瓶颈:)