c++ - 如何设置 3D tensorflow Tensor c++ 的元素

标签 c++ tensorflow

我在下面有以下 3D 张量:

auto inputX = Tensor(DT_DOUBLE, TensorShape({1,1,9}));

如何为张量中的元素设置值?

此外,对于下面的一维张量,我该如何设置一个值?

auto inputY = Tensor(DT_INT32, TensorShape({1}));

谢谢!

更新如下:

这是我的代码:

#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <string.h>
#include <vector>
#include <sstream>
#include <typeinfo>
#include <sys/time.h>
#include "tensorflow/cc/client/client_session.h"
#include "tensorflow/cc/ops/standard_ops.h"
#include "tensorflow/core/framework/tensor.h"
#include "tensorflow/core/public/session.h"
#include "tensorflow/core/platform/env.h"

using namespace std;
using namespace tensorflow;
using namespace tensorflow::ops;

Session* session; // tensorflow session
Status status;  // tensorflow status

double get_prediction(Tensor a);

double get_prediction(Tensor a, Tensor b) {

  double prediction;
  vector<Tensor> outputs;

  // preparing input
  // TODO: Update this line
  std::vector<std::pair<string, tensorflow::Tensor>> inputs = {{"InputDataLayer/InputXPlaceHolder", a}, {"InputDataLayer/LabelYPlaceHolder", b}};

  // getting prediction for test data
  // TODO: Update this line
  printf("Reached5");
  status = session->Run(inputs, {"OutputLayer/outputLogits"}, {}, &outputs);

  if (!status.ok()) {
    cout<<"Error@get_prediction: "<<status.ToString()<<"\n";
    return 1l;
  }

  prediction = outputs[0].scalar<double>()(0);

  return prediction;
}

int main(int argc, char *argv[]) {

  ifstream f;
  string line = "";
  string token = "";
  double temp = 0.0;
  int temp1 = 0;
  double matches = 0.0, accuracy = 0.0;
  int row_no=0, col_no=0;
  double prediction = 0.0, actual = 0.0;

  status = NewSession(SessionOptions(), &session);
  if (!status.ok()) {
    std::cout << status.ToString() << "\n";
    return 1;
  }

  GraphDef graph_def;

  status = ReadTextProto(Env::Default(), "/home/user/Desktop/tensorflow/tensorflow/loader/models.pbtxt", &graph_def);

  if (!status.ok()) {
    std::cout << status.ToString() << "\n";
    return 1;
  }

  status = session->Create(graph_def);
  if (!status.ok()) {
    std::cout << status.ToString() << "\n";
    return 1;
  }

  session->Run({}, {}, {"init_all_vars_op"}, nullptr); //Initializes all the variables in C++

  printf("Reached1");

  auto inputX = Tensor(DT_DOUBLE, TensorShape({1,1,9}));
  auto inputY = Tensor(DT_INT32, TensorShape({1}));

  printf("Reached2");

  f.open("/home/user/Desktop/tensorflow/tensorflow/loader/signals_p.csv"); //processed csv

  while(getline(f, line)) {

    if (row_no == 0) {
        row_no += 1;
        continue;
    }

    istringstream iss(line);

    while(getline(iss, token, ',')) {

      // filling feature tensors
      if(col_no >= 6 && col_no <= 14) {
        temp = stod(token.c_str());
        printf("Reached3");
        inputX.tensor<double,3>()(0, 0,col_no) = temp;
      }

      // filling label tensor
      if(col_no == 16) {
        temp1 = stoi(token.c_str());
        actual = temp1;
        inputY.vec<int>()(col_no) = temp1;
      }

      col_no += 1;

   }

    col_no = 0;
    row_no += 1;

    printf("Reached4");
    // getting prediction
    prediction = get_prediction(inputX, inputY);

    // if actual and prediction matches, increment matches
    if(actual == prediction)
      matches += 1;
  }

  accuracy = matches / (row_no);
  cout<<"Total Rows: "<<(row_no)<<endl;
  cout<<"Accuracy: "<<accuracy<<endl;

  session->Close();
  return 0;
}

我正在尝试读取 .pbtxt 文件并在 C++ 中执行推理。这是我的 .pbtxt 文件的可视化。

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

代码在“Reached5”打印语句之后立即中断。 输出如下:

*** Error in `./ml': corrupted double-linked list: 0x0000000004fa1290 ***

最佳答案

您可以以 Eigen tensor 的形式访问数据(或者更准确地说是 Eigen::TensorMap)通过 tensor方法:

inputX.tensor<double, 3>()(0, 0, 3) = 2.5;

您还可以通过 flat 访问数据和 shaped :

inputY.flat<int32>()(0) = 5;
*inputY.shaped<int32, 1>({1}).data() = 10;

关于c++ - 如何设置 3D tensorflow Tensor c++ 的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52189676/

相关文章:

python - 尝试使用未初始化的值变量 - Tensorflow (Python)

python - 沿最后一个维度不同大小的Tensorflow张量运算

c++ - 将转换构造函数与运算符重载相结合

C++如何制作一个计数循环来确定两个输入数字中哪个最小,并从那里开始计数

C++,给vector::end赋值?

c++ - Lync 2013 - CreateAuthBrokerSession 不接受 WebTicket

python - 在 Tensorflow 中使用 RNN 预测 future 时间序列中的值

c++ - 如何在 C++ 中初始化静态 vector 数组成员

tensorflow - 如何合并keras tensorflow中的两个模型以制作一个模型

tensorflow - 了解 Tensorflow 的形状、等级和大小