简体   繁体   English

如何在c ++中设置输入张量的值?

[英]How do I set the value of an input tensor in c++?

I'm trying to run a sample through a pre trained model on ios. 我正试图通过ios上经过预先训练的模型运行样本。 session->Run() takes as input a tensor to my understanding. session-> Run()将张量作为输入作为我的理解。 I have initialized a tensor, but how do i set it's value? 我已经初始化了张量,但我如何设置它的值? I don't have much experience using C++. 我没有太多使用C ++的经验。

I have successfully created a test model that accepts 3 dimensional tensor of shape {1, 1, 10}. 我已成功创建了一个接受3维张量形状{1,1,10}的测试模型。

I pulled the following line of code from Tensorflow's simple example to create the input tensor. 我从Tensorflow的简单示例中提取了以下代码行来创建输入张量。

https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/ios_examples/simple/RunModelViewController.mm#L189 https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/ios_examples/simple/RunModelViewController.mm#L189

tensorflow::Tensor input_tensor(tensorflow::DT_FLOAT, tensorflow::TensorShape({1,1,10}));

From here, I cannot figure out how I would set the data of input_tensor. 从这里,我无法弄清楚如何设置input_tensor的数据。 I would like to set the tensor to something like {{{.0, .1, .2, .3, .4, .5, .6, .7, .8, .9}}} 我想将张量设置为类似{{{。0,.1,.2,.3,.4,.5,.6,.7,.8,.9}}}的内容。

I had a similar problem and was trying to set the tensor input values in C++ for a model trained in Python. 我有一个类似的问题,并试图在C ++中为使用Python训练的模型设置张量输入值。 The model is a simple NN with one hidden layer to learn to calculate the XOR operation. 该模型是一个简单的NN,具有一个隐藏层,用于学习计算XOR运算。

I first created an output graph file with both the graph structure and the model parameters by following steps 1-4 of this nice post: https://medium.com/@hamedmp/exporting-trained-tensorflow-models-to-c-the-right-way-cf24b609d183#.j4l51ptvb . 我首先按照这个好帖子的步骤1-4创建了一个包含图形结构和模型参数的输出图形文件: https//medium.com/@hamedmp/exporting-trained-tensorflow-models-to-c- the-right-way-cf24b609d183#.j4l51ptvb

Then in C++ (the TensorFlow iOS simple example), I used the following code: 然后在C ++(TensorFlow iOS简单示例)中,我使用了以下代码:

tensorflow::Tensor input_tensor(tensorflow::DT_FLOAT, tensorflow::TensorShape({4,2}));

// input_tensor_mapped is an interface to the data of a tensor and used to copy data into the tensor
auto input_tensor_mapped = input_tensor.tensor<float, 2>();

// set the (4,2) possible input values for XOR
input_tensor_mapped(0, 0) = 0.0;
input_tensor_mapped(0, 1) = 0.0;
input_tensor_mapped(1, 0) = 0.0;
input_tensor_mapped(1, 1) = 1.0;
input_tensor_mapped(2, 0) = 1.0;
input_tensor_mapped(2, 1) = 0.0;
input_tensor_mapped(3, 0) = 1.0;
input_tensor_mapped(3, 1) = 1.0;

tensorflow::Status run_status = session->Run({{input_layer, input_tensor}},
                                             {output_layer}, {}, &outputs);

After this, GetTopN(output->flat<float>(), kNumResults, kThreshold, &top_results); 在此之后, GetTopN(output->flat<float>(), kNumResults, kThreshold, &top_results); returns the same 4 values (0.94433498, 0.94425952, 0.06565627, 0.05823805), as in my Python test code for XOR after the model is trained, in top_results. 返回相同的4个值(0.94433498,0.94425952,0.06565627,0.05823805),就像我在训练模型后的XOR测试代码中一样,在top_results中。

So if your tensor's shape is {1,1,10}, you can set the values as follows: 因此,如果张量的形状为{1,1,10},则可以按如下方式设置值:

auto input_tensor_mapped = input_tensor.tensor<float, 3>();
input_tensor_mapped(0, 0, 0) = 0.0;
input_tensor_mapped(0, 0, 1) = 0.1;
....
input_tensor_mapped(0, 0, 9) = 0.9;

Credit: the answer at How do I pass an OpenCV Mat into a C++ Tensorflow graph? Credit: 如何将OpenCV Mat传递给C ++ Tensorflow图表? is very helpful. 非常有帮助。

If you want to directly set the value of a tensor you can use few utilities functions provided by the Tensor interface. 如果要直接设置张量的值,可以使用Tensor接口提供的少量实用程序功能。 For the most common linear access you can use flat<T> . 对于最常见的线性访问,您可以使用flat<T>

From tensor_test 来自tensor_test

void ExpectClose(const Tensor& x, const Tensor& y, double atol, double rtol) {
  auto Tx = x.flat<T>();
  auto Ty = y.flat<T>();
  for (int i = 0; i < Tx.size(); ++i) {
    if (!IsClose(Tx(i), Ty(i), atol, rtol)) {
      LOG(ERROR) << "x = " << x.DebugString();
      LOG(ERROR) << "y = " << y.DebugString();
      LOG(ERROR) << "atol = " << atol << " rtol = " << rtol
                 << " tol = " << atol + rtol * std::fabs(Tx(i));
      EXPECT_TRUE(false) << i << "-th element is not close " << Tx(i) << " vs. "
                         << Ty(i);
    }
  }
}

to create a tensor you can use one of the constructors 要创建张量,您可以使用其中一个构造函数

Tensor(DT_FLOAT, new TensorShape(..))

If you want to set the value of a tensor or a placeholder at run time you need to pass it through the Run() interface: 如果要在运行时设置张量或占位符的值,则需要通过Run()接口传递它:

  Status run_status = session->Run({{input_layer, resized_tensor}},
                                   {output_layer}, {}, &outputs);
  if (!run_status.ok()) {
    LOG(ERROR) << "Running model failed: " << run_status;
    return -1;
  }

If you want to have a predefine value of a tensor you can use the Const constructor 如果要使用张量的预定义值,可以使用Const构造函数

tensorflow::ops::Const({input_height, input_width})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Tensorflow的C ++ API中,如何使用Eigen Tensor设置Tensorflow Tensor? - In Tensorflow's C++ API, how do I use an Eigen Tensor to set my Tensorflow Tensor? 如何将元素插入到作为多映射 C++ 中的值的集合 - How do I insert an element to a set that is a value in multimap C++ 如果 x 为正,我如何将 x 设置为负值,如果 x 为正,我如何将其设置为负。 c++ - How do i set x to be a negative value if x is positive, and if x is positive how do i set it to be negative. c++ 如果未输入输入,如何将值设置为 0? c++ - How to set value to 0 if input isn´t type in? c++ 如何设置3D张量流量元素Tensor c ++ - How to set element of 3D tensorflow Tensor c++ 如何使用C ++在输入标签中获取属性值的文本? - How do I get the text of the attribute value in an input tag using C++? 如何从输入读取 CSV 行并将每个值初始化为不同类型的变量? (C++) - How do I read a CSV line from input and initialize each value to variables of different types? (C++) 如何循环 C++ 中 Pytorch 张量中的每个值? - How to loop over every value in Pytorch tensor in C++? 如何使用类作为要在set :: find()上使用的值? -C ++ - How do I use a class as a value to be used on set::find()? - C++ 如何通过 C++ 中的代码设置用户输入? - How can I set User Input from Code in C++?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM