Tensorflow的complex 64和complex 128类型实际上是对std::complex的简单重定义。源码如下,

另外加入复数类型以后发现原来的打印函数不好用了。重新用模板函数特化实现了一遍,现在算是通用了。
程序结构如下,

conanfile.txt

 [requires]gtest/1.10.0glog/0.4.0protobuf/3.9.1eigen/3.4.0dataframe/1.20.0opencv/3.4.17boost/1.76.0abseil/20210324.1xtensor/0.23.10zlib/1.2.11[generators]cmake

CMakeLists.txt

cmake_minimum_required(VERSION 3.3)project(test_math_ops)set(ENV{PKG_CONFIG_PATH} "$ENV{PKG_CONFIG_PATH}:/usr/local/lib/pkgconfig/")set(CMAKE_CXX_STANDARD 17)
add_definitions(-g)include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()find_package(TensorflowCC REQUIRED)
find_package(PkgConfig REQUIRED)
pkg_search_module(PKG_PARQUET REQUIRED IMPORTED_TARGET parquet)
pkg_search_module(PKG_ARROW REQUIRED IMPORTED_TARGET arrow)
pkg_search_module(PKG_ARROW_COMPUTE REQUIRED IMPORTED_TARGET arrow-compute)
pkg_search_module(PKG_ARROW_CSV REQUIRED IMPORTED_TARGET arrow-csv)
pkg_search_module(PKG_ARROW_DATASET REQUIRED IMPORTED_TARGET arrow-dataset)
pkg_search_module(PKG_ARROW_FS REQUIRED IMPORTED_TARGET arrow-filesystem)
pkg_search_module(PKG_ARROW_JSON REQUIRED IMPORTED_TARGET arrow-json)set(ARROW_INCLUDE_DIRS ${PKG_PARQUET_INCLUDE_DIRS} ${PKG_ARROW_INCLUDE_DIRS} ${PKG_ARROW_COMPUTE_INCLUDE_DIRS} ${PKG_ARROW_CSV_INCLUDE_DIRS} ${PKG_ARROW_DATASET_INCLUDE_DIRS} ${PKG_ARROW_FS_INCLUDE_DIRS} ${PKG_ARROW_JSON_INCLUDE_DIRS})set(INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/../../include ${ARROW_INCLUDE_DIRS})set(ARROW_LIBS PkgConfig::PKG_PARQUET PkgConfig::PKG_ARROW PkgConfig::PKG_ARROW_COMPUTE PkgConfig::PKG_ARROW_CSV PkgConfig::PKG_ARROW_DATASET PkgConfig::PKG_ARROW_FS PkgConfig::PKG_ARROW_JSON)include_directories(${INCLUDE_DIRS})file( GLOB test_file_list ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp) file( GLOB APP_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/../../include/tf_/impl/tensor_testutil.cc ${CMAKE_CURRENT_SOURCE_DIR}/../../include/tf_/impl/queue_runner.cc ${CMAKE_CURRENT_SOURCE_DIR}/../../include/tf_/impl/coordinator.cc ${CMAKE_CURRENT_SOURCE_DIR}/../../include/tf_/impl/status.cc ${CMAKE_CURRENT_SOURCE_DIR}/../../include/death_handler/impl/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../../include/df/impl/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../../include/arr_/impl/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../../include/img_util/impl/*.cpp)add_library(${PROJECT_NAME}_lib SHARED ${APP_SOURCES})
target_link_libraries(${PROJECT_NAME}_lib PUBLIC ${CONAN_LIBS} TensorflowCC::TensorflowCC ${ARROW_LIBS})foreach( test_file ${test_file_list} )file(RELATIVE_PATH filename ${CMAKE_CURRENT_SOURCE_DIR} ${test_file})string(REPLACE ".cpp" "" file ${filename})add_executable(${file}  ${test_file})target_link_libraries(${file} PUBLIC ${PROJECT_NAME}_lib)
endforeach( test_file ${test_file_list})

tf_math2_test.cpp

TEST(TfArthimaticTests, Complex) {// Complex转换两个张量为一个复数张量// Refers to: https://tensorflow.google.cn/versions/r2.6/api_docs/cc/class/tensorflow/ops/complex?hl=zh-cn&authuser=0Scope root = Scope::NewRootScope();auto real_ = test::AsTensor<float>({2.25f, 3.25f}, {2});auto image_ = test::AsTensor<float>({4.75f, 5.75f}, {2});// 转换为复数auto complex_op = ops::Complex(root, real_, image_);ClientSession session(root);std::vector<Tensor> outputs;session.Run({complex_op.out}, &outputs);test::PrintTensorValue<complex64>(std::cout, outputs[0]);test::ExpectTensorEqual<complex64>(outputs[0], test::AsTensor<complex64>({{2.25f, 4.75f}, {3.25f, 5.75f}}, {2}));
}

PrintTensorValue函数

template <typename T>
std::ostream& PrintTensorValue(std::ostream& os, Tensor const& tensor) {// 打印Tensor值T const* tensor_pt = tensor.unaligned_flat<T>().data();auto size = tensor.NumElements();os << std::setprecision(std::numeric_limits<long double>::digits10 + 1);for(decltype(size) i=0; i<size; ++i) {os << tensor_pt[i] << "\n";}return os;
}template <>
std::ostream& PrintTensorValue<uint8>(std::ostream& os, Tensor const& tensor) {// 打印Tensor值uint8 const* tensor_pt = tensor.unaligned_flat<uint8>().data();auto size = tensor.NumElements();os << std::setprecision(std::numeric_limits<long double>::digits10 + 1);for(decltype(size) i=0; i<size; ++i) {os << (int)tensor_pt[i] << "\n";}return os;
}template <typename T>
std::ostream& PrintTensorValue(std::ostream& os, Tensor const& tensor, int per_line_count) {// 打印Tensor值T const* tensor_pt = tensor.unaligned_flat<T>().data();auto size = tensor.NumElements();os << std::setprecision(std::numeric_limits<long double>::digits10 + 1);for(decltype(size) i=0; i<size; ++i) {if(i!=0 && (i+1)%per_line_count == 0) {  os << tensor_pt[i] << "\n";}else {os << tensor_pt[i] << "\t";}}return os;
}template <>
std::ostream& PrintTensorValue<uint8>(std::ostream& os, Tensor const& tensor, int per_line_count) {// 打印Tensor值uint8 const* tensor_pt = tensor.unaligned_flat<uint8>().data();auto size = tensor.NumElements();os << std::setprecision(std::numeric_limits<long double>::digits10 + 1);for(decltype(size) i=0; i<size; ++i) {if(i!=0 && (i+1)%per_line_count == 0) {  os << (int)tensor_pt[i] << "\n";}else {os << (int)tensor_pt[i] << "\t";}}return os;
}

程序输出如下,

Tensorflow C++ API 生成复数算子相关推荐

  1. tensorflow函数API总结

    tensorflow函数API总结: 首推官网查询 tf.keras.Input:创建输入层 别名: tf.keras.Input tf.keras.layers.Input tf.keras.Inp ...

  2. OpenCV Tensorflow C++API Protobuf eigen3 OpenBlas 编译过程

    OpenCV Tensorflow C++API Protobuf eigen3 OpenBlas 编译过程 文章目录 OpenCV Tensorflow C++API Protobuf eigen3 ...

  3. 基于Tensorflow和DCGAN生成动漫头像实践(二)

    本篇内容为动漫头像生成的主要代码部分,第一次写这种代码,从读取数据到生成走了一个完整的流程.创建TFrecord过程可以看上一篇内容. 代码内容: #!/usr/bin/env python2 # - ...

  4. TensorFlow Keras API用法

    TensorFlow Keras API用法 Keras 是与 TensorFlow 一起使用的更高级别的作为后端的 API.添加层就像添加一行代码一样简单.在模型架构之后,使用一行代码,可以编译和拟 ...

  5. api.php t.cn,PHP通过调用新浪API生成t.cn格式短网址链接的方法详解

    本文实例讲述了PHP通过调用新浪API生成t.cn格式短网址链接的方法.分享给大家供大家参考,具体如下: 新浪提供了长链接转为短链接的API,可以把长链接转为 t.cn/xxx 这种格式的短链接. A ...

  6. Tensorflow C++ API调用Keras模型实现RGB图像语义分割

    我的实验是基于PSPNet模型实现二维图像的语义分割,下面的代码直接从得到的h5文件开始往下做... 也不知道是自己的检索能力出现了问题还是咋回事,搜遍全网都没有可以直接拿来用的语义分割代码,东拼西凑 ...

  7. 今晚直播 | 谷歌资深工程师手把手教你使用TensorFlow最新API构建学习模型

    目前,深度学习的研究和应用大受追捧,各种开源的深度学习框架层出不穷.TensorFlow 作为目前最受欢迎的深度学习框架,已经在 GitHub 上获得了 112194 个 star,受欢迎程序可见一斑 ...

  8. Ubuntu Tensorflow object_detection API 目标检测环境搭建

    Ubuntu 16.04下安装TensorFlow Object Detection API(对象检测API) Ubuntu 16.04下搭建TensorFlow运行环境(用Anaconda)     ...

  9. Tensorflow高级API的进阶--利用tf.contrib.learn建立输入函数

    正文共5958个字,预计阅读时间15分钟. 笔记整理者:王小草 笔记整理时间:2017年2月27日 笔记对应的官方文档:https://www.tensorflow.org/get_started/i ...

最新文章

  1. Go 知识点(05)— 类型别名与类型定义
  2. ios架构篇-2 国际化多语言
  3. 零基础入门学习Python(27)-文件2
  4. 文本编辑BOM标记(Byte Order Mark)
  5. 在Flex中使用HTTPService传递参数
  6. 1036 跟奥巴马一起编程 (15 分)(c++)C++
  7. 增加网格_网格交易法(期货)
  8. 微信OAuth2.0网页授权设置一个域名需多个域名使用的问题
  9. 一部后现代文学的“奇书”:《烟草经纪人》
  10. 局域网共享问题全方位解决
  11. 将整个表单设置为只读_如何将独立网站设置为制作中,阻止搜索引擎收录网站页面?...
  12. jQuery+PHP动态数字展示效果
  13. 汇编语言---乘法指令及符号扩展
  14. 分享30个应用HTML5的网站案例
  15. sql插入多条记录_如何在SQL中插入多条记录
  16. matlab 太阳系仿真,三维仿真太阳系
  17. XMind中怎么导入图标?
  18. 机器学习算法——概率类模型评估指标4(校准可靠性曲线及预测概率直方图)
  19. 硬盘提示初始化是什么意思?数据会丢失吗?
  20. 移动硬盘数据恢复该如何进行?2个方法告诉你

热门文章

  1. python 引入同一路径的类_python小课堂15 - 史上最详细的包和模块import讲解篇
  2. android 怎么播放gif图片不显示,android 播放gif动态图片
  3. 计算机网络第六版课程总结,计算机网络课程实训报告总结 | 翠格格
  4. 你真的知道什么是 Python「命名空间」吗?
  5. (兼容正点原子引脚)OV7670 FIFO 30W摄像头介绍(一) --- 整体介绍/SCCB时序
  6. 优质的10个免费icon图标网站
  7. 源代码编码问题带来的gcc编译错误解决办法
  8. 今晚《青春有你》上线啦!盘他!
  9. com.qihoo.android.float window,全民枪战360版(com.crisisfire.android.qihoo) - 3.23.1 - 游戏 - 酷安...
  10. CAD二维图转换为solidworks三维模型方法