Tensorflow的C++部署系列(七):网络搜集的代码整理

整理C++的tensorflow代码。

1. 对张量的值进行观测。

/*
 * inference4beginer.cpp
 * Copyright (C) 2017 fisherman
 */

#include <tensorflow/core/public/session.h>
#include <tensorflow/core/platform/env.h>
#include <tensorflow/core/framework/tensor.h>
#include <tensorflow/core/framework/graph.pb.h>

#include <glog/logging.h>

#include <string>
#include <vector>
#include <memory>
int main() {
  tensorflow::GraphDef graph;
  std::string graph_file = "beginner.pb";
  //1 读取Graph, 如果是文本形式的pb,使用ReadTextProto
  tensorflow::Status status = tensorflow::ReadBinaryProto(tensorflow::Env::Default(), graph_file, &graph);
  if (!status.ok()) {
    LOG(FATAL) << status.ToString() << " with graph_file:" << graph_file;
    return -1;
  }
  //2 创建Session
  std::unique_ptr<tensorflow::Session> sess;
  sess.reset(tensorflow::NewSession({}));
  status = sess->Create(graph);
  if (!status.ok()) {
    LOG(FATAL) << status.ToString();
    return -1;
  }
  //3 构造Tensor并赋值
  //3.1 创建tensorflow::Tensor x
  tensorflow::Tensor x(tensorflow::DT_FLOAT, tensorflow::TensorShape({}));
  //3.2 获取x的Eigen::TensorMap
  auto x_map = x.tensor<float, 0>(); // == x.scalar<float>()
  //auto ->Eigen::TensorMap<Eigen::Tensor<T, NDIMS, Eigen::RowMajor, IndexType>, Eigen::Aligned>
  //3.3 获取指针, 可以任意复制了。
  float* data = x_map.data();
  *data = 1.0f;
  std::vector<std::pair<std::string, tensorflow::Tensor>> inputs;
  inputs.emplace_back(std::make_pair("x", x));
  std::vector<std::string> output_tensor_names;
  output_tensor_names.emplace_back("y");
  std::vector<tensorflow::Tensor> y;
  // 4 Session Run
  status = sess->Run(inputs, output_tensor_names, {}, &y);
  if (!status.ok()) {
    LOG(ERROR) << status.ToString();
    return -1;
  }
  // 5 打印结果
  float y_value = *(y[0].tensor<float, 0>().data());
  LOG(INFO) << "y = a*x+b\n" << " a(0.1) b(0) in graph, x(1), y(0.1):" << y_value;
  return 0;
}

2. 加载tensorflow代码进行预测:(加载图、模型、预测)

链接: https://blog.csdn.net/heiheiya/article/details/89849737

链接:https://blog.csdn.net/yz2zcx/article/details/100406635

3. MNIST数据的C++代码案例

链接: https://blog.csdn.net/qq_25109263/article/details/81285952?utm_medium=distribute.pc_relevant.none-task-blog-baidujs-3

4. MLP模型的C++代码案例

链接: https://blog.csdn.net/MOU_IT/article/details/88070285?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-29.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-29.nonecase#2%E3%80%81%E4%BD%BF%E7%94%A8C%2B%2B%E5%8A%A0%E8%BD%BDpython%E8%AE%AD%E7%BB%83%E5%A5%BD%E7%9A%84%E6%A8%A1%E5%9E%8B%E5%B9%B6%E8%BF%9B%E8%A1%8C%E9%A2%84%E6%B5%8B

5、 C++调用tensorflow (shape)

链接:https://blog.csdn.net/koibiki/article/details/88238170

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值