C++从张量流::张量对象访问Eigen::张量类函数
C++ Accessing Eigen::Tensor Class function from Tensorflow::Tensor object
这可能是一个愚蠢的问题,但我很难从tensorflow中的自定义操作函数(void Compute)中访问Eigen::Tensor类函数。
Eigen::Tensor有一个名为"extract_patchs"的成员函数,我想从操作内部访问它。当我"typeid()"张量对象时,它会返回tensorflow::Tensor。如何访问底层的特征::张量?
请注意,"extract_patches"的代码直接来自Eigen文档。
#include "tensorflow/core/framework/op.h"
#include "tensorflow/core/framework/op_kernel.h"
#include "tensorflow/core/util/padding.h"
#include "tensorflow/core/framework/numeric_op.h"
#include "tensorflow/core/util/tensor_format.h"
#include <vector>
#include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor"
using namespace tensorflow;
class SauronOp : public OpKernel {
public:
explicit SauronOp(OpKernelConstruction* context) : OpKernel(context) {// Some checks}
void Compute(OpKernelContext* context) override {
//declare temporary storage
TensorShape a_shape_temp({act_in_batch, act_in_rows, act_in_cols });
Tensor a_temp;
OP_REQUIRES_OK(context, context->allocate_temp( DataTypeToEnum<float>::value,
a_shape_temp, &a_temp));
//auto a_matrix = a_temp.shaped<float,3>({{act_in_batch, act_in_rows, act_in_cols });
auto a_tensor = a_temp.tensor<float,3>();
//unrelated stuff
Eigen::Tensor<float, 4> patch;
Eigen::array<ptrdiff_t, 3> patch_dims;
patch_dims[0] = filter_height;
patch_dims[1] = filter_width;
patch_dims[2] = act_in_cols;
// THIS LINE GENERATES THIS PROBLEM (sorry for yelling)
patch = a_tensor.extract_patches(patch_dims);
// output for debug
std::cout<<"type "<< typeid(a_tensor).name()<<std::endl;
std::cout<<"type "<< typeid(a_temp).name()<<std::endl;
}
输出
type N5Eigen9TensorMapINS_6TensorIfLi3ELi1ElEELi16EEE
type N10tensorflow6TensorE
我怀疑问题在于extract_patches()
返回的是Eigen::TensorPatchOp<...>
(表示未求值的表达式),而不是Eigen::Tensor<...>
(表示求值的值)。要实现这一点,您需要(i)对.extract_patches()
的结果调用.eval()
,以及(ii)将patch
声明为行主,以获得所需类型的对象。
以下代码用Compute()
方法为我编译:
Tensor a_temp;
// Initialize `a_temp`...
const auto& a_tensor = a_temp.shaped<float, 3>(
{act_in_batch, act_in_rows, act_in_cols});
Eigen::array<ptrdiff_t, 3> patch_dims;
patch_dims[0] = filter_height;
patch_dims[1] = filter_width;
patch_dims[2] = act_in_cols;
const auto& patch_expr = a_tensor.extract_patches(patch_dims);
Eigen::Tensor<float, 4, Eigen::RowMajor> patch = patch_expr.eval();
相关文章:
- 使用Pybind11向Python公开Eigen::张量
- C++中的张量流对象检测
- 在官方张量流 resnet50 模型上运行 tflite 精度工具
- PyTorch C++将数据转换为张量失败
- 如何使用 c++ 更改张量流中的per_process_gpu_memory_fraction?
- 编译 GPU 的张量流示例自定义操作
- 在 Torch C++ API 中,如何快速写入张量的内部数据?
- 如何在C++中将一个特征张量乘以另一个特征张量的标量和?
- C++ ABI 兼容性问题/张量流
- 如何从内存缓冲区加载张量流图
- PyTorch C++ 前端向前返回多个张量
- 从 std::向量值创建张量<Tensor>
- 张量流错误 此文件需要编译器和库支持 ISO C++ 2011 标准
- 如何将自定义特征张量类存储到 std::vector 中?
- 从 Eigen::Tensor 创建张量流::张量
- 如何在张量流上使用 fp16(Eigen::half) 进行卷积
- 返回 am Eigen::来自函数的张量切片
- eigen :: Tensor,如何从张量访问矩阵
- 在C++中,这个Eigen::张量的声明是安全的,还是有缺陷的?我应该为它提交一个问题吗
- C++从张量流::张量对象访问Eigen::张量类函数