简体   繁体   中英

How to set element of 3D tensorflow Tensor c++

I have the following 3D tensor below:

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

How would I set a value to an element in the tensor?

Additionally for the 1D tensor below how would I set a value?

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

Thanks!

UPDATE BELOW:

Here is my code:

#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;
}

I am trying to read a .pbtxt file and perform inference in C++. Here is a visualization of my .pbtxt file.

在此输入图像描述

在此输入图像描述

在此输入图像描述

在此输入图像描述

在此输入图像描述

The code is breaking right after the "Reached5" print statement. Output is as follows:

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

You can access the data as an Eigen tensor (or more precisely an Eigen::TensorMap ) through the tensor method:

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

You can also access the data through flat and shaped :

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

Figured it out- I was incorrectly accessing the Tensor inputX with the line inputX.matrix()(0,col_no) = temp; because I was calling an index outside of the 14 initialized.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM