c++ - Tensorflow C++ 占位符初始化
问题描述
我试图在 cpp 上使用 tensorflow 执行图像字幕。我按照如何使用 tf.estimator 导入保存的 Tensorflow 模型训练并预测输入数据的方向进行操作并尝试了
int main(int argc, char**) {
....
auto x = tf::Tensor(tf::DT_UINT8, tf::TensorShape({height, width, channel}));
auto matrix = x.matrix<char>();
....
}
当我编译程序时,代码
> In file included from /usr/local/include/tensorflow/core/framework/tensor.h:26:0,
from /usr/local/include/tensorflow/core/public/session.h:23,
from /usr/local/include/tensorflow/cc/saved_model/loader.h:26,
from server.cpp:22:
/usr/local/include/tensorflow/core/framework/types.h: In instantiation of ‘struct tensorflow::DataTypeToEnum<char>’:
/usr/local/include/tensorflow/core/framework/tensor.h:530:46: required from ‘typename tensorflow::TTypes<T, NDIMS>::Tensor tensorflow::Tensor::tensor() [with T = char; long unsigned int NDIMS = 2ul; typename tensorflow::TTypes<T, NDIMS>::Tensor = Eigen::TensorMap<Eigen::Tensor<char, 2, 1, long int>, 16, Eigen::MakePointer>]’
/usr/local/include/tensorflow/core/framework/tensor.h:240:25: required from ‘typename tensorflow::TTypes<T>::Matrix tensorflow::Tensor::matrix() [with T = char; typename tensorflow::TTypes<T>::Matrix = Eigen::TensorMap<Eigen::Tensor<char, 2, 1, long int>, 16, Eigen::MakePointer>]’
server.cpp:121:34: required from here
/usr/local/include/tensorflow/core/framework/types.h:138:3: error: static assertion failed: Specified Data Type not supported
static_assert(IsValidDataType<T>::value, "Specified Data Type not supported");In file included from /usr/local/include/tensorflow/core/public/session.h:23:0,
from /usr/local/include/tensorflow/cc/saved_model/loader.h:26,
from server.cpp:22:
/usr/local/include/tensorflow/core/framework/tensor.h: In instantiation of ‘typename tensorflow::TTypes<T, NDIMS>::Tensor tensorflow::Tensor::tensor() [with T = char; long unsigned int NDIMS = 2ul; typename tensorflow::TTypes<T, NDIMS>::Tensor = Eigen::TensorMap<Eigen::Tensor<char, 2, 1, long int>, 16, Eigen::MakePointer>]’:
/usr/local/include/tensorflow/core/framework/tensor.h:240:25: required from ‘typename tensorflow::TTypes<T>::Matrix tensorflow::Tensor::matrix() [with T = char; typename tensorflow::TTypes<T>::Matrix = Eigen::TensorMap<Eigen::Tensor<char, 2, 1, long int>, 16, Eigen::MakePointer>]’
server.cpp:121:34: required from here
/usr/local/include/tensorflow/core/framework/tensor.h:530:46: error: ‘v’ is not a member of ‘tensorflow::DataTypeToEnum<char>’
CheckTypeAndIsAligned(DataTypeToEnum<T>::v());
有没有人遇到过同样的问题?
解决方案
即使您TensorShape
使用初始化列表声明您的,您仍然应该指定矩阵的维数,因为它无法在 C++ 中的编译时推断出来。
auto x = tf::Tensor(tf::DT_UINT8, tf::TensorShape({height, width, channel}));
auto matrix = x.matrix<char, 3>();
推荐阅读
- java - 将 Kotlin EJB 注入 Java POJO
- shell - Replace Double quotes with space
- java - 在 Marathon 测试工具/JRuby 中查找文本并使用其位置
- python - 计算 Python 列表中的不同值
- c++ - C++ 工厂模式中的运行时错误
- php - PayPal API GetVerifiedStatus 不起作用
- javascript - 如何在循环中停止 setInterval
- python - pytorch减去不同维度的变量
- python - QMDISubWindow 在 QMenuBar 下
- regex - 正则表达式:我将如何使用 Negative Lookahead 排除可能出现在搜索词之前或之后的特定字符