Tensorflow C ++:将数组用于feed_dict

时间:2018-02-13 15:55:17

标签: c++ tensorflow

我在Tensorflow中有一个C ++代码,如下所示,它涉及使用占位符对矩阵进行乘法运算:

#include <stdio.h>
#include <stdlib.h>
#include <ctime> 
#include <iostream>
#include "tensorflow/cc/client/client_session.h"
#include "tensorflow/cc/ops/standard_ops.h"
#include "tensorflow/core/framework/tensor.h"

int main(int argc, char const *argv[]){
  using namespace tensorflow;
  using namespace tensorflow::ops;
  Scope root = Scope::NewRootScope();

  auto alpha = Const(root, 2.0, {1, 1});
  auto beta = Const(root, 3.0, {1, 1});

  auto A = Placeholder(root, DT_FLOAT);
  auto B = Placeholder(root, DT_FLOAT);
  auto C = Placeholder(root, DT_FLOAT);
  auto temp1 = MatMul(root, A, B);
  auto temp2 = Mul(root, alpha, temp1);
  auto temp3 = Mul(root, beta, C);
  auto D = Add(root.WithOpName("D"), temp1, temp3);


  std::vector<Tensor> outputs;
  ClientSession session(root);

  int num_size = 2;
  for(int step = 1; step < num_size; step++){
    /*Allocating arrays*/
    int array_size = pow(10, step);
    float **a, **b, **c;
    a = (float **)malloc(sizeof(float)*array_size);
    b = (float **)malloc(sizeof(float)*array_size);
    c = (float **)malloc(sizeof(float)*array_size);
    for(int i = 0; i < array_size; i++){
      a[i] = (float *)malloc(sizeof(float)*array_size);
      b[i] = (float *)malloc(sizeof(float)*array_size);
      c[i] = (float *)malloc(sizeof(float)*array_size);
    }

    srand((unsigned)time(0)); 
    for(int i = 0; i < array_size; i++){
      for(int j = 0; j < array_size; j++){
        a[i][j] = (rand()%100)+1;
        b[i][j] = (rand()%200)+1;
        c[i][j] = (rand()%300)+1;
      }
    }

    for(int num = 0; num < 10; num++){
      Status s = session.Run({{A, a}, {B, b}, {C, c}}, {D}, &outputs);
      if(s.ok())
         c = outputs[0];
      else
         printf("Error\n");
    }
  }

  return 0;
}

但是,在link中显示了在C ++中向占位符发送值的格式。 C ++中使用的feed类型为here

我很困惑如何将我的2D数组修改为feeddict格式,以便提供'session.Run()'。

谢谢。

修改1

问题的最小代表如下 -

考虑以下代码片段:

Scope root = Scope::NewRootScope();
auto a = Placeholder(root, DT_INT32);
// [3 3; 3 3]
auto b = Const(root, 3, {2, 2});
auto c = Add(root, a, b);
ClientSession session(root);
std::vector<Tensor> outputs;

// Feed a <- [1 2; 3 4]
int feed_a[2][2] = {{1, 2}, {3, 4}}; 
session.Run({ {a, feed_a} }, {c}, &outputs);
// The working code is - session.Run({ {a, { {1, 2}, {3, 4} } } }, {c}, &outputs);
// outputs[0] == [4 5; 6 7]

如何在显示的情况下使此代码工作,其中'feed_a'数组是从单独的函数接收的,并且需要使用它来设置占位符'a'的值。

1 个答案:

答案 0 :(得分:1)

您需要创建一个c数组并将数据放在那里而不是使用锯齿状数组。

#include "tensorflow/cc/client/client_session.h"
#include "tensorflow/cc/ops/standard_ops.h"
#include "tensorflow/core/framework/tensor.h"

int main() {
  using namespace tensorflow;
  using namespace tensorflow::ops;

  Scope root = Scope::NewRootScope();
  // [3 3; 3 3]
  auto b = Const(root, {{3.f, 3.f}, {3.f, 3.f}});

  ClientSession session(root);
  std::vector<Tensor> outputs;

  // just print b
  TF_CHECK_OK(session.Run({}, {b}, &outputs));
  LOG(INFO) << "b = ";
  LOG(INFO) << outputs[0].matrix<float>();


  // just print c = a + b
  float *a_data = new float[4];
  for (int i = 0; i < 4; ++i)
    a_data[i] = 1.f;
  auto a_shape = TensorShape({2, 2});
  auto a_init = Input::Initializer(*a_data, a_shape);
  auto a_plhdr = Placeholder(root, DT_FLOAT);
  auto c = Add(root, a_plhdr, b);

  TF_CHECK_OK(session.Run({{a_plhdr, a_init}}, {c}, &outputs));


  LOG(INFO) << "a + b";
  LOG(INFO) << outputs[0].matrix<float>();
  return 0;
}

给了我

2018-02-14 22:45:47.469766: I tensorflow/cc/example/example.cc:20] b = 
2018-02-14 22:45:47.469800: I tensorflow/cc/example/example.cc:21] 3 3
3 3
2018-02-14 22:45:47.473519: I tensorflow/cc/example/example.cc:36] a + b
2018-02-14 22:45:47.473543: I tensorflow/cc/example/example.cc:37] 4 4
4 4

请注意,出于某种原因

int32 *a_data = new int32[4];
for (int i = 0; i < 4; ++i)
  a_data[i] = 1;
auto a_shape = TensorShape({2, 2});
auto a_init = Input::Initializer(*a_data, a_shape);
auto a_plhdr = Placeholder(root, DT_INT32);

产生失败(无输出):

Check failed: dtype() == expected_dtype (1 vs. 3)

无法通过

解决
auto a_casted = Cast(root, a_plhdr, DT_FLOAT)
auto c = Add(root, a_casted, b);