重塑C ++中的张量 [英] Reshaping tensors in C++

查看:130
本文介绍了重塑C ++中的张量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TensorFlow的C ++接口似乎没有重塑方法。有谁知道如何转换例如 [A,B,C,D] 转换为 [A * B,C,D] ?看起来唯一的方法就是使用Eigen?但是,那里的文档非常苗条,并且代码是模板地狱,不容易解析。

The C++ interface to TensorFlow doesn't seem to have a reshape method. Does anyone have an idea how to convert e.g. [A,B,C,D] into [A*B,C,D]? It looks like the only way to do this is to use Eigen? However, the documentation there is very slim and the code is template hell and not easy to parse.

推荐答案

解决方案,检查是否重塑张量与源张量的元素数量相同:

Solution with checking whether reshaped tensor has the same number of elements of the source tensor:

// Extracted image features from MobileNet_224
tensorflow::Tensor image_features(tensorflow::DT_FLOAT,
                                  tensorflow::TensorShape({1, 14, 14, 512}));

tensorflow::Tensor image_features_reshaped(tensorflow::DT_FLOAT,
                                           tensorflow::TensorShape({1, 196, 512}));

// Reshape tensor from [1, 14, 14, 512] to [1, 196, 512]
if(!image_features_reshaped.CopyFrom(image_features, tensorflow::TensorShape({1, 196, 512})))
{
  LOG(ERROR) << "Unsuccessfully reshaped image features tensor [" << image_features.DebugString() << "] to [1, 196, 512]";
  return false;
}

LOG(INFO) << "Reshaped features tensor: " << image_features_reshaped.DebugString();

这篇关于重塑C ++中的张量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆