TensorFlow 卷积代码优化

TensorFlow Convolution code Optimization

我正在使用 C++ 版本的 TensorFLow 并使用以下命令成功构建 'TensorFlow for Android' 'bazel build -c opt //tensorflow/examples/android:tensorflow_demo' 如 https://github.com/tensorflow/tensorflow/tree/master/tensorflow/examples/android#bazel

中所述

我正在尝试优化卷积码。以下是面临的问题

  1. 无法找到卷积码的准确位置。 我能够将代码调试到
  2. 中的以下功能

'return选择( 条件(), kernel.reshape(kernel_dims) .contract(输入 .extract_image_patches( kernelRows, kernelCols, row_stride, col_stride, row_in_stride、col_in_stride、padding_type) .reshape(pre_contract_dims), contract_dims) .reshape(post_contract_dims), 输入 .extract_image_patches(kernelRows, kernelCols, row_stride, col_stride, row_in_stride、col_in_stride、padding_type) .reshape(pre_contract_dims) .contract(kernel.reshape(kernel_dims), contract_dims) .reshape(post_contract_dims));'

出现在https://github.com/tensorflow/tensorflow/blob/master/tensorflow/core/kernels/eigen_spatial_convolutions.h

关于上述功能我有几个问题。

1.1 上面的函数真的在进行卷积吗?如果是这样,代码在哪里?

1.2 contraction (contract function) 和卷积一样吗?如果卷积和收缩都相同,为什么对输入和核矩阵都进行收缩操作?

1.3 函数的定义在哪里——选择、重塑、收缩、提取图像块等?

2.Unable 从输入和内核矩阵中提取数据(矩阵)。这参考了上面的同一页 link

2.1 我在上面页面的第946行找到了一行代码'kern(kernel);'。可以知道上面函数的location定义吗?

2.2 我无法从相应的 4d 张量(输入和内核)中提取输入和内核矩阵作为浮点数组,因为我想尝试使用并行处理优化卷积代码。我找不到任何方法将 Tensor Matrices 从 Tensor 4D 转换为数组。

请帮我回答以上问题

它从 Linux.My 路径的缓存中的特征 TensorFlow 文件中挑选是 /.cache/bazel/_bazel_ashok/c54b442ed4139c7d8ad47f330eb538d6/external/eigen_archive

1.1) 是的,代码是 Cond() 语句之后的代码:

// If this condition is met, the first argument is chosen, if not, the second one
// is chosen, this condition checks if the input is ColMajor or RowMajor, all of
// the tests I've done result in a RowMajor but I don't know what determines this
// exactly
return choose( Cond(),

// ColMajor
kernel.reshape(kernel_dims) .contract(input 
.extract_image_patches( kernelRows, kernelCols, row_stride, col_stride, 
row_in_stride, col_in_stride, padding_type) .reshape(pre_contract_dims), 
contract_dims) .reshape(post_contract_dims),

// RowMajor
input.extract_image_patches(kernelRows, kernelCols, row_stride, col_stride, 
row_in_stride, col_in_stride, padding_type) .reshape(pre_contract_dims) 
.contract(kernel.reshape(kernel_dims),contract_dims).reshape(post_contract_dims));

1.2) 不是,收缩是对N维张量的矩阵乘法的抽象。它仅适用于其中之一,具体取决于条件

1.3) 这些都是 Eigen 函数,Eigen 在他们的 wiki operations.I 上找到 this 关于他们的 Tensor 的一个相当无用的文档,可以帮助你理解他们的作用,但不是彻底但它可以帮助您围绕操作的想法进行思考。

2.1)我也不知道在哪

2.2) 我不确定这是否可以直接完成,本征函数可能相当不直观,如果你知道 4D 张量的形状,你可以创建一个矩阵并将每个元素分配给该矩阵(我认为效率不高)

我才意识到这是一年前发布的,但我已经写下了我的答案,它可能对其他人有用,所以我就把它留在这里。